# Java api client to implement a complex query

**URL:** <https://discuss.elastic.co/t/java-api-client-to-implement-a-complex-query/295611>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [January 27, 2022, 4:55pm UTC](https://discuss.elastic.co/t/java-api-client-to-implement-a-complex-query/295611 "2022-01-27T16:55:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![colddew](https://avatars.discourse-cdn.com/v4/letter/c/b9bd4f/32.png) [@colddew](https://discuss.elastic.co/u/colddew)\
**Post date:** [January 27, 2022, 4:55pm UTC](https://discuss.elastic.co/t/java-api-client-to-implement-a-complex-query/295611/1 "2022-01-27T16:55:04Z")

</div>

I spent much time to try the new Elasticsearch Java client 7.16.3, but it is hard for me to use it, such as `must` , `terms` , `multi_match` , `functions` , `filter` and so on. Can someone please help me in building the query as blow using the new Elasticsearch Java client, complete Java code or snippet will be better, thx

```auto
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "terms": {
                "type": [
                  1,
                  2
                ]
              }
            },
            {
              "multi_match": {
                "query": "facebook",
                "fields": [
                  "title",
                  "content",
                  "stockCodes",
                  "stockNames"
                ]
              }
            }
          ],
          "boost": 1
        }
      },
      "functions": [
        {
          "filter": {
            "terms": {
              "type": [
                1,
                2
              ]
            }
          },
          "weight": 1
        },
        {
          "filter": {
            "match": {
              "title": "facebook"
            }
          },
          "weight": 100
        },
        {
          "filter": {
            "match": {
              "stockCodes": "facebook"
            }
          },
          "weight": 10
        },
        {
          "filter": {
            "match": {
              "stockNames": "facebook"
            }
          },
          "weight": 10
        },
        {
          "filter": {
            "match": {
              "content": "facebook"
            }
          },
          "weight": 5
        }
      ],
      "score_mode": "sum",
      "boost_mode": "multiply",
      "max_boost": 120,
      "boost": 1
    }
  },
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    },
    {
      "releaseTime": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 9
}

```

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/c/bc1c043b712dca75c8e8fd820336972a695f524b.png)  
such as, i cannot find the api to set the value of terms

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [January 31, 2022, 3:52pm UTC](https://discuss.elastic.co/t/java-api-client-to-implement-a-complex-query/295611/2 "2022-01-31T15:52:40Z")

</div>

The `terms` query can be configured in two ways: using a [list of term values](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html#terms-query-ex-request), or using a [terms lookup configuration](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html#query-dsl-terms-lookup).

In your case, I guess you want to use a list of term values.

Assuming you have a variable `List<String> terms`:

```java
List<String> terms = Collections.singletonList("");
List<FieldValue> termValues = terms.stream().map(FieldValue::of).collect(Collectors.toList());
        
...
    .bool(b -> b
        .must(m -> m
            .terms(t -> t
                .field("")
                    .terms(tf -> tf
                        .value(termValues)
                    )
                )
            )
...

```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![vred](https://avatars.discourse-cdn.com/v4/letter/v/e99b99/32.png) [@vred](https://discuss.elastic.co/u/vred)\
**Post date:** [February 11, 2022, 9:20pm UTC](https://discuss.elastic.co/t/java-api-client-to-implement-a-complex-query/295611/3 "2022-02-11T21:20:19Z")

</div>

Running this same code I get this error:  
`Exception in thread "main" co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [x_content_parse_exception] [1:56] [bool] failed to parse field [must]`  
Do you know why this shows up? It seems to be a common exception I'm seeing, but it doesn't appear as a syntax error or compile error until I actually run the `client.search(...)`

```java
		List<String> terms = Collections.singletonList("");

		List<FieldValue> termValues = terms.stream().map(FieldValue::of).collect(Collectors.toList());

		Query q = new Query.Builder()
			.bool(v -> v
				.must(m -> m
					.terms(t -> t
						.field("")
							.terms(tf -> tf
								.value(termValues)
							)
						)
					)
				).build();

		SearchRequest sr = new SearchRequest.Builder()
			.index("index")
			.query(q)
			.build();

		SearchResponse<Map> search = client.search(sr, Map.class);

```

---

<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:** [March 11, 2022, 9:20pm UTC](https://discuss.elastic.co/t/java-api-client-to-implement-a-complex-query/295611/4 "2022-03-11T21:20:52Z")

</div>

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