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("----");
}
}
