Unable to do match query with new Java API client

Hello!
I am trying to do a match query similar to one below:

  "query": {
    "bool": {
      "must": [
        {
          "match": {  "detected.tag": "chair" }
        }
      ]
    }
  }
}

This query gives me expected results when run using a simple GET request on ES.

Problem:
I want to translate the query using Java API client and here's what I tried to do:

		BoolQuery.Builder queryBuilder = new BoolQuery.Builder();
		FieldValue value = FieldValue.of("chair");
		Query byName = MatchQuery.of(m -> m
				.field("detected.tag")
				.query(value)
		)._toQuery();
		queryBuilder.must(byName);
		
		SearchRequest.Builder searchRequestBuilder = new SearchRequest.Builder();
		searchRequestBuilder.query(queryBuilder.build()._toQuery());

		// Execute query
		ElasticSearch es = new ElasticSearch();
		SearchResponse<Object> results = es.search(searchRequestBuilder);

		return JsonpUtils.toJsonString(results, new JacksonJsonpMapper());

{"query":{"bool":{"must":[{"match":{"detected.tag":{"query":"chair"}}}]}}}
ES client keeps adding this word "query" to the generated query and hence I do not get any results!
Can you please point out if I'm doing something wrong? Or is it a bug?

Hi @p4charu

The problem is not "query" inside the match.

Try running this query in devTools, you will get results.

{
   "query": {
     "bool": {
       "must": [
         {
           "match": {
             "detected.tag": {
               "query": "chair"
             }
           }
         }
       ]
     }
   }
}

The problem is not "query" inside the match.
I see that you don't indicate what the index is in the SearchRequest, but that wouldn't be a problem if there is "detected.tag" = "chair" in any index.

What is this?

Should you pass the searchrequest and TDocument to the search?

Thank you for your response.
es.search(searchRequestBuilder) is an internal method where I set the index, etc.

Did you do this and get results?

Yes.
I was so consumed with the queries not matching that I forgot to run the generated query to see if it was giving me correct results!

Thanks for your quick responses. I've now managed to add multiple MatchQuery to my main query like this.


		// Noun Queries 1st noun as type 2nd noun as detected.tag
		Query typeMatch = MatchQuery.of(m -> m.field("contentType").query(nouns.get(0)))._toQuery();
		Query tagMatch = MatchQuery.of(m -> m.field("detected.tag").query(nouns.get(1)))._toQuery();

		queryBuilder.must(typeMatch, tagMatch);
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.