ElasticSearch Facets

时间:2013-05-23 21:57:39

标签: elasticsearch

我有以下弹性搜索查询

SearchResponse response = DbContext.INSTANCE.client()
            .prepareSearch(indexList)
            .setTypes("docs")
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)             
            .setQuery(queryBuilder)             
            .setFilter(FilterBuilders.andFilter(f1, f2))
            .addFacet(FacetBuilders.termsFacet("f").field("frm").size(10))
            .setFrom(pageNumber).setSize(size).execute().actionGet();

此查询的方面计数返回是全局的,如何获取搜索响应的方面?


我找到了答案:我所做的是在主查询中添加过滤器

QueryBuilder queryBuilder = QueryBuilders.boolQuery()
.must(QueryBuilders.queryString(criterion.getFullText()).field("text"))
.must(QueryBuilders.termsQuery("from", criterion.getFrom().toArray((new String[0]))))
.must(QueryBuilders.termsQuery("to", criterion.getTo().toArray((new String[0]))))
.must(QueryBuilders.rangeQuery("date").from(dtf.print(criterion.getFromDate())).to(dtf.print(criterion.getToDate())));

SearchResponse response = 
DbContext.INSTANCE.client()
.prepareSearch(indexList)
.setTypes("docs")
.setQuery(queryBuilder)
.addFacet(fb)
.addFacet(tf)
.addSort("date", SortOrder.DESC)
.setFrom(pageNumber).setSize(size).execute().actionGet();

1 个答案:

答案 0 :(得分:1)

这是我所做的,除非你想要“主要”搜索和facet有不同的搜索要求。 global=false应该为你做的伎俩

"facets": {
  "volume_by_day": {
    "date_histogram": {
      "field": "crdate",
      "interval": "day"
    },
    "global": "false"
  }
}