小编典典

将弹性搜索从1.4.3迁移到2.4 Java代码

java

我正在从弹性搜索1.4.3迁移到2.4,并且替换了弹性搜索文档中引用的一段代码,而其他参考则需要替换andFilter?

码:-

  TermQueryBuilder incPlatformFilter=QueryBuilders.termQuery(IdxSchemaConstants.FULL_OFFER_ACTION_INCLUSIVE_PLATFORMS, platformId);

  //For MissingQuerybuilder
  //MissingFilterBuilder incPlatformEmptyFilter = FilterBuilders.missingFilter(IdxSchemaConstants.FULL_OFFER_ACTION_INCLUSIVE_PLATFORMS);
  BoolQueryBuilder incPlatformEmptyFilter = new BoolQueryBuilder().mustNot(new ExistsQueryBuilder(IdxSchemaConstants.FULL_OFFER_ACTION_INCLUSIVE_PLATFORMS));
  //NotFilterBuilder
  //NotFilterBuilder excPlatformFilter = FilterBuilders.notFilter(FilterBuilders.termFilter(IdxSchemaConstants.FULL_OFFER_ACTION_EXCLUSIVE_PLATFORMS, platformId));
  TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(IdxSchemaConstants.FULL_OFFER_ACTION_EXCLUSIVE_PLATFORMS, platformId);
  BoolQueryBuilder excPlatformFilter = new BoolQueryBuilder().mustNot(termQueryBuilder);

  QueryBuilder platformPartialMatched = QueryHelper.andFilterBuilders(excPlatformFilter, incPlatformEmptyFilter);

  public class QueryHelper {
    public static QueryBuilder andFilterBuilders(QueryBuilder... filterBuilders) {

     QueryBuilder filterBuilder = null;

      if (filterBuilders != null && filterBuilders.length > 0) {

  filterBuilder = QueryBuilders.andFilter(filterBuilders);
}
return filterBuilder;}
}

阅读 239

收藏
2020-11-30

共1个答案

小编典典

您可以这样做:

TermQueryBuilder incPlatformFilter = QueryBuilders.termQuery(IdxSchemaConstants.FULL_OFFER_ACTION_INCLUSIVE_PLATFORMS, platformId);

//For MissingQuerybuilder
//MissingFilterBuilder incPlatformEmptyFilter = FilterBuilders.missingFilter(IdxSchemaConstants.FULL_OFFER_ACTION_INCLUSIVE_PLATFORMS);
BoolQueryBuilder incPlatformEmptyFilter = new BoolQueryBuilder().mustNot(new ExistsQueryBuilder(IdxSchemaConstants.FULL_OFFER_ACTION_INCLUSIVE_PLATFORMS));
//NotFilterBuilder
//NotFilterBuilder excPlatformFilter = FilterBuilders.notFilter(FilterBuilders.termFilter(IdxSchemaConstants.FULL_OFFER_ACTION_EXCLUSIVE_PLATFORMS, platformId));
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(IdxSchemaConstants.FULL_OFFER_ACTION_EXCLUSIVE_PLATFORMS, platformId);
BoolQueryBuilder excPlatformFilter = new BoolQueryBuilder().mustNot(termQueryBuilder);

// ==> use a bool/filter to replace the "and" query <<==
QueryBuilder platformPartialMatched = QueryBuilders.boolQuery()
    .filter(excPlatformFilter)
    .filter(incPlatformEmptyFilter);
2020-11-30