FACET
1)Terms Facet
{ "query" : { "match_all" : { } }, "facets" : { "tag" : { "terms" : { "field" : "tag", "size" : 10 } } } }
被统计(facet)的字段一般不分词(例如商品的类目字段——类目唯一),但也支持分词后term不多的字段(例如商品的标签字段)。
对应这种facet我们主要关注几点:
facet的字段(field,multi fields)
facet返回的数量(top N)
facet排序(count,term,reverse_count,reverse_term)
facet作用范围(all terms,excluding terms,regex patterns,term script)
2)Range Facets
{ "query" : { "match_all" : {} }, "facets" : { "range1" : { "range" : { "field" : "field_name", "ranges" : [ { "to" : 50 }, { "from" : 50, "to" : 70 }, { "from" : 70, "to" : 120 }, { "from" : 120 } ] } } } }
例如:
商品的价格区间。
考虑下面一种需求:
统计各个价格区间购买次数。(每个商品有个销量字段)
{ "query" : { "match_all" : {} }, "facets" : { "range1" : { "range" : { "key_field" : "price", "value_field" : "volume", "ranges" : [ { "to" : 50 }, { "from" : 50, "to" : 70 }, { "from" : 70, "to" : 120 }, { "from" : 120 } ] } } } }
3)Histogram Facet
实现直方图的效果,其实也算是range的一种。
{ "query" : { "match_all" : {} }, "facets" : { "histo1" : { "histogram" : { "field" : "field_name", "interval" : 100 } } } }
interval可以理解为步长。除了number型还有time_interval。
4)Date Histogram Facet
5)Filter Facets
{ "facets" : { "wow_facet" : { "filter" : { "term" : { "tag" : "wow" } } } } }
返回命中“指定filter”的结果数。
6)Query Facets
{ "facets" : { "wow_facet" : { "query" : { "term" : { "tag" : "wow" } } } } }
Q:FilterFacets VS. QueryFacets?
7)Statistical Facet
{ "query" : { "match_all" : {} }, "facets" : { "stat1" : { "statistical" : { "field" : "num1" } } } }
StatisticalFacet需要作用在数值型字段上面,他会统计总数、总和、最值、均值等。
8)Terms stats Facet
{ "query" : { "match_all" : { } }, "facets" : { "tag_price_stats" : { "terms_stats" : { "key_field" : "tag", "value_field" : "price" } } } }
也是一个kv的统计,例如统计某某类目下价格的分布情况(最值、均值等)。
9)GEO distance Facet
--------------------------------------------------
javaClient Demo:
public void facet() { SearchResponse sr = client.prepareSearch() .setQuery(QueryBuilders.matchAllQuery()) .addFacet(FacetBuilders.termsFacet("f1").field("price")) .execute().actionGet(); // Get your facet results TermsFacet f = (TermsFacet) sr.getFacets().facetsAsMap().get("f1"); System.out.println(f.getTotalCount()); // Total terms doc count System.out.println(f.getOtherCount()); // Not shown terms doc count System.out.println(f.getMissingCount()); // Without term doc count // For each entry for (TermsFacet.Entry entry : f) { System.out.println("t:" + entry.getTerm()); // Term System.out.println("c:" + entry.getCount()); // Doc count System.out.println("----"); } }