user3105965
user3105965

Reputation: 21

How to build a search query for nested objects in spring-data elastic search?

My search query looks like this:

curl -XGET "http://localhost:9200/imagesearchservice/images    /_search?pretty=true" -d '
{
  "query": {
    "bool": {
        "must": [
           {
               "nested": {
                  "path":"tags",
                  "query": {
                    "bool": {
                      "must": [
                        {"match_all" : {} }
                      ],
              "filter" : {
                          "terms" : {
                             "tags.tagName" : ["star"]
                            }
                       }
                    }
                  }
                }
           }
    ]
      }
    }
 }'

I tried doing the following but it didnt work.

SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
                .withFilter(boolQuery().must(termsQuery("tags.tagName", tagNames)))
                .build();

I am using Elastic Search v2.x . Can someone please tell me how to build a search query for the above using nativesearchquerybuilder ?

Upvotes: 0

Views: 958

Answers (1)

Cecilia Arenas
Cecilia Arenas

Reputation: 163

I would do something like this:

  BoolQueryBuilder booleanQuery = new BoolQueryBuilder();
  booleanQuery.must(termsQuery("tags.tagName", tagNames));
  myElasticSearchRepository.search(booleanQuery)

Where myElasticSearchRepository is an interfce that extends ElasticsearchRepository

Upvotes: 1

Related Questions