2

ElasticSearch has GET API using which we can query on a single index for a particular document using the document Id. From Elasticsearch 5.1, GET API supports querying on documents on an alias too that can point to multiple indexes like this:

GET /my_alias_name/_search/
{
        "query": { 
        "bool": {
         "filter": {
                "term": {
                   "_id": "AUwNrOZsm6BwwrmnodbW"
                }
            }
        }
    }
}

What is the corresponding JAVA API to achieve this (using JestClient...)?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
blrguy
  • 131
  • 6

1 Answers1

2

1) Client creation:

JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig.Builder("http://localhost:9200")
                        .multiThreaded(true)
                        .build());
JestClient jestClient = factory.getObject();

2) Prepare the Search request:

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("_id", "AUwNrOZsm6BwwrmnodbW")));

Search search = new Search.Builder(searchSourceBuilder.toString())
                        .addIndex("my_alias_name") -> Add index name or an alias. 
                        .addType("my_type") -> Add index type here. 
                        .build();

3) Execute the search:

SearchResult result = jestClient.execute(search);

Note: We can add an alias name in place of index name and it works the same way.

TechnocratSid
  • 2,245
  • 1
  • 16
  • 26
  • May be I was not clear on one more thing I have in mind. I was asking specifically about GET API as I was under an assumption that SEARCH requests are costlier than GET requests (any request is on document _id only)! Correct me if I am wrong. – blrguy Sep 04 '18 at 15:37
  • To make it clear, the request you had mentioned in your question is Search API not a Get API. – TechnocratSid Sep 04 '18 at 15:58
  • Even if you perform a GET request using the alias name like: GET /my_alias_name/my_type/my_id will return you the document. – TechnocratSid Sep 04 '18 at 15:59
  • Like I mentioned, I am querying on alias that can point to multiple indexes, so there can be multiple documents matching the same document _id. And no, GET /my_alias_name/my_type/my_id errors out like this: { "error" : "ElasticsearchIllegalArgumentException[Alias [data] has more than one indices associated with it [[index_1, index_2]], can't execute a single index op]", "status" : 400 } – blrguy Sep 04 '18 at 16:08
  • Okay got your point. And yes you're right about the GET request failing with error because in your case the alias is pointing to multiple indices. I did not consider that case apologies for that. But your question says "From Elasticsearch 5.1, GET API supports querying on documents on an alias too that can point to multiple indexes like this" --the search query-- "What is the corresponding JAVA API to achieve this (using JestClient...)?" – TechnocratSid Sep 04 '18 at 17:31
  • And IMHO, you can't perform a GET request to a particular _id for alias(if it is pointing to multiple indices). – TechnocratSid Sep 04 '18 at 17:35