一尘不染

将elasticsearch从1.4.3迁移到2.4 Java代码

elasticsearch

我正在从elasticsearch1.4.3迁移到2.4,并替换了elasticsearch文档中引用的一段代码,而其他参考则需要替换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;}
}

阅读 226

收藏
2020-06-22

共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-06-22