ElasticSearch Java高级REST客户端:过滤文档和或查询

时间:2019-11-15 11:49:34

标签: java elasticsearch

我需要检索按“品牌”属性过滤的文档,并检索对另一个属性具有特定值的文档。示例:

所有活动的(“ active”属性为true)(标识为“ abc”,“ def”或“ ghi”值)的所有“ apple”文档(“ brand”属性)。

我正在使用Java高级REST客户端。

那是我的代码:

SearchRequest searchRequest = new SearchRequest(index);
  searchRequest.types(type);

  SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

  /* add query filters */
  MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("brand", brand);
  searchSourceBuilder.query(matchQueryBuilder);

  /* base sort is added */
  //TODO: load sort from mapping based on baseSort of customSort
  searchSourceBuilder.sort(sortBy , SortOrder.DESC);

  /* add product id */

  BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
          .filter(QueryBuilders.termQuery("active", true));

  searchSourceBuilder.query(boolQuery);
  searchRequest.source(searchSourceBuilder);

谢谢!

1 个答案:

答案 0 :(得分:0)

SearchRequest searchRequest = new SearchRequest(INDEX);
        QueryBuilder qb = QueryBuilders.queryStringQuery(searchText + "*").defaultField("companyName")
                .defaultOperator(Operator.AND);

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.boolQuery()
            .must(QueryBuilders.termQuery("categories.id", category)
            .should(QueryBuilders.termQuery("product_id", productIdsList)
           );



sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
searchRequest.source(sourceBuilder);