Java api client to implement a complex query

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

{
  "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
}


such as, i cannot find the api to set the value of terms

The terms query can be configured in two ways: using a list of term values, or using a terms lookup configuration.

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

Assuming you have a variable List<String> terms:

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.

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(...)

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

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