elasticsearch 如何将现有函数转换为TermsQuery.builder

xzabzqsa  于 2023-05-16  发布在  ElasticSearch
关注(0)|答案(1)|浏览(213)

如何将下面的接口转换为最新的TermsQuery.builder
https://www.javadoc.io/doc/org.elasticsearch/elasticsearch/7.2.0/org/elasticsearch/index/query/QueryBuilders.html#termsQuery(java.lang.String,java.util.Collection).下面给出了现有代码

List<String> ids;

    TermsQueryBuilder idsQb = QueryBuilders.termsQuery("id", ids);
    NativeSearchQuery query = new NativeSearchQueryBuilder()
            .withSourceFilter(new FetchSourceFilterBuilder()
            .withPageable(pageable).withQuery(idsQb).build();

    SearchHits<Profile> searchHits = esRestTemplate.search(query, Profile.class);

我正在尝试将上面的函数转换为下面的接口

https://javadoc.io/doc/co.elastic.clients/elasticsearch-java/latest/co/elastic/clients/elasticsearch/_types/query_dsl/TermsQuery.Builder.html

我没有看到一种方法来传递一个id的列表给这个函数

https://javadoc.io/static/co.elastic.clients/elasticsearch-java/8.7.1/co/elastic/clients/elasticsearch/_types/query_dsl/QueryBuilders.html#terms(java.util.function.Function)
66bbxpm5

66bbxpm51#

没有运行它,但应该是这样的:

import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;

QueryBuilders
  .terms()
  .field("id")
  .terms(termsBuilder -> termsBuilder
    .value(
      ids.stream()
         .map(FieldValue::of)
         .collect(Collectors.toList())));

相关问题