0

How to query data from elasticsearch based on the property that is present inside the actual object.

Format of data stored in elsticsearch:

{
  "principals": [
    {
      "id": 1,
      "account": {
        "account_id": 2
      }
    }
  ]
}

Search query in postman:

{
  "query": {
    "terms": {
      "account_id": [
        1
      ]
    }
  }
}

This is returning the required result in postman.

How to achieve the same in java using highlevelrestclient.

name not found
  • 622
  • 4
  • 13
  • found this [java-high-level-rest-client-elasticsearch](https://dzone.com/articles/java-high-level-rest-client-elasticsearch) with a simple google search – Omri Attiya Apr 10 '20 at 08:00
  • Can you share the mapping of this index. –  Apr 10 '20 at 10:42
  • I am not able to fecth result from ElasticSearch using your above serach query , I have indexed given document in index through this mapping { "mappings": { "properties": { "principals": { "properties": { "id": { "type": "integer" }, "account": { "properties": { "account_id": { "type": "integer" } } } } } } } }. Can you tell what mapping you have used ? –  Apr 10 '20 at 11:13

1 Answers1

0

I am not sure how your above search query worked in fetching corresponding document.

But I had indexed and searched your document through this way :

mapping:

{
  "mappings": {
    "properties": { 

      "principals": { 
        "properties": {
          "id":  { "type": "integer" },
          "account": { 
            "properties": {
              "account_id": { "type": "integer" }

            }
          }
        }
      }
    }
  }
}

search query:

 {
  "query": {
    "terms": {
      "principals.account.account_id": [2]
    }
  }
}

Search result :

"hits": [
  {
    "_index": "nestedindex",
    "_type": "_doc",
    "_id": "2",
    "_score": 1.0,
    "_source": {
      "principals": [
        {
          "id": 1,
          "account": {
            "account_id": 2
          }
        }
      ]
    }
  }
]

Search query through Elasticsearch Resthighlevelclient

SearchRequest searchRequest = new SearchRequest("testIndex"); //in place of "testIndex" you can give your index name
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); 
List<Integer> accountIds = new ArrayList<Integer>();
accountIds.add(2);
sourceBuilder.query(QueryBuilders.termsQuery("principals.account.account_id", accountIds)); 
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); 
searchRequest.source(sourceBuilder);
SearchResponse searchResponse =   
                client.search(searchRequest, RequestOptions.DEFAULT);  //client is ES client

return searchResponse; //you can read your hits through searchResponse.getHits().getHits()

ElasticSearch client can be instantiated in spring-boot application by creating configuration file in your project and autowiring the client where required:

@Configuration
@Primary
public class ElasticsearchConfig {

    private RestHighLevelClient restHighLevelClient;

    @Bean(destroyMethod = "close")
    public RestHighLevelClient client() {

        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http")));

        return client;

    }