# Unable to do match query with new Java API client

**URL:** <https://discuss.elastic.co/t/unable-to-do-match-query-with-new-java-api-client/337065>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [June 28, 2023, 10:37am UTC](https://discuss.elastic.co/t/unable-to-do-match-query-with-new-java-api-client/337065 "2023-06-28T10:37:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![p4charu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/p4charu/32/113392_2.png) [@p4charu](https://discuss.elastic.co/u/p4charu)\
**Post date:** [June 28, 2023, 10:37am UTC](https://discuss.elastic.co/t/unable-to-do-match-query-with-new-java-api-client/337065/1 "2023-06-28T10:37:29Z")

</div>

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

```auto
  "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:

```auto
		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?

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [June 28, 2023, 11:55am UTC](https://discuss.elastic.co/t/unable-to-do-match-query-with-new-java-api-client/337065/2 "2023-06-28T11:55:40Z")

</div>

Hi @p4charu

The problem is not "query" inside the match.

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

```auto
{
   "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?

> [@p4charu](#):
>
> `es.search(searchRequestBuilder)`

Should you pass the searchrequest and TDocument to the search?

---

<div class="post-metadata">

**Author:** ![p4charu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/p4charu/32/113392_2.png) [@p4charu](https://discuss.elastic.co/u/p4charu)\
**Post date:** [June 28, 2023, 12:02pm UTC](https://discuss.elastic.co/t/unable-to-do-match-query-with-new-java-api-client/337065/3 "2023-06-28T12:02:34Z")

</div>

> [@RabBit\_BR](#):
>
> that you don't indicate what the index is in the SearchRequest, but that wouldn't be a

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

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [June 28, 2023, 12:07pm UTC](https://discuss.elastic.co/t/unable-to-do-match-query-with-new-java-api-client/337065/4 "2023-06-28T12:07:55Z")

</div>

Did you do this and get results?

> [@RabBit\_BR](#):
>
> Try running this query in devTools, you will get results.
> 
> ```auto
> {
> "query": {
> "bool": {
> "must": [
> {
> "match": {
> "detected.tag": {
> "query": "chair"
> }
> }
> }
> ]
> }
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![p4charu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/p4charu/32/113392_2.png) [@p4charu](https://discuss.elastic.co/u/p4charu)\
**Post date:** [June 28, 2023, 2:37pm UTC](https://discuss.elastic.co/t/unable-to-do-match-query-with-new-java-api-client/337065/5 "2023-06-28T14:37:03Z")

</div>

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.

```auto

		// 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);

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 26, 2023, 2:37pm UTC](https://discuss.elastic.co/t/unable-to-do-match-query-with-new-java-api-client/337065/6 "2023-07-26T14:37:54Z")

</div>

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