facets

Elasticsearch提供完整的java API用来支持facets。在查询的过程中,将需要计数的facets添加到FacetBuilders中。然后将该FacetBuilders条件到查询请求中。

SearchResponse sr = node.client().prepareSearch()
.setQuery( /* your query */ )
.addFacet( /* add a facet */ )
.execute().actionGet();

为了构建facets请求,需要用到FacetBuilders帮助类。你只需要在你的程序中导入它即可。

import org.elasticsearch.search.facet.FacetBuilders.*;

terms facet

准备一个facet请求

下面的例子新建一个facet请求

FacetBuilders.termsFacet("f")
.field("brand")
.size(10);

利用facet响应

import org.elasticsearch.search.facet.terms.*;

// sr is here your SearchResponse object
TermsFacet f = (TermsFacet) sr.getFacets().facetsAsMap().get("f"); f.getTotalCount(); // Total terms doc count
f.getOtherCount(); // Not shown terms doc count
f.getMissingCount(); // Without term doc count // For each entry
for (TermsFacet.Entry entry : f) {
entry.getTerm(); // Term
entry.getCount(); // Doc count
}

范围facet

准备一个facet请求

下面的例子新建一个facet请求

FacetBuilders.rangeFacet("f")
.field("price") // Field to compute on
.addUnboundedFrom(3) // from -infinity to 3 (excluded)
.addRange(3, 6) // from 3 to 6 (excluded)
.addUnboundedTo(6); // from 6 to +infinity

利用facet响应

import org.elasticsearch.search.facet.range.*;

// sr is here your SearchResponse object
RangeFacet f = (RangeFacet) sr.getFacets().facetsAsMap().get("f"); // For each entry
for (RangeFacet.Entry entry : f) {
entry.getFrom(); // Range from requested
entry.getTo(); // Range to requested
entry.getCount(); // Doc count
entry.getMin(); // Min value
entry.getMax(); // Max value
entry.getMean(); // Mean
entry.getTotal(); // Sum of values
}

直方图(Histogram) Facet

准备一个facet请求

下面的例子新建一个facet请求

HistogramFacetBuilder facet = FacetBuilders.histogramFacet("f")
.field("price")
.interval(1);

利用facet响应

import org.elasticsearch.search.facet.histogram.*;
// sr is here your SearchResponse object
HistogramFacet f = (HistogramFacet) sr.getFacets().facetsAsMap().get("f"); // For each entry
for (HistogramFacet.Entry entry : f) {
entry.getKey(); // Key (X-Axis)
entry.getCount(); // Doc count (Y-Axis)
}

日期直方图(Histogram) Facet

准备一个facet请求

下面的例子新建一个facet请求

FacetBuilders.dateHistogramFacet("f")
.field("date") // Your date field
.interval("year"); // You can also use "quarter", "month", "week", "day",
// "hour" and "minute" or notation like "1.5h" or "2w"

利用facet响应

import org.elasticsearch.search.facet.datehistogram.*;
// sr is here your SearchResponse object
DateHistogramFacet f = (DateHistogramFacet) sr.getFacets().facetsAsMap().get("f"); // For each entry
for (DateHistogramFacet.Entry entry : f) {
entry.getTime(); // Date in ms since epoch (X-Axis)
entry.getCount(); // Doc count (Y-Axis)
}

过滤facet(不是facet过滤)

准备一个facet请求

下面的例子新建一个facet请求

FacetBuilders.filterFacet("f",
FilterBuilders.termFilter("brand", "heineken")); // Your Filter here

利用facet响应

import org.elasticsearch.search.facet.filter.*;

// sr is here your SearchResponse object
FilterFacet f = (FilterFacet) sr.getFacets().facetsAsMap().get("f");
f.getCount(); // Number of docs that matched

查询facet

准备一个facet请求

下面的例子新建一个facet请求

FacetBuilders.queryFacet("f",
QueryBuilders.matchQuery("brand", "heineken"));

利用facet响应

import org.elasticsearch.search.facet.query.*;
// sr is here your SearchResponse object
QueryFacet f = (QueryFacet) sr.getFacets().facetsAsMap().get("f"); f.getCount(); // Number of docs that matched

统计

准备一个facet请求

下面的例子新建一个facet请求

FacetBuilders.statisticalFacet("f")
.field("price");

利用facet响应

import org.elasticsearch.search.facet.statistical.*;
// sr is here your SearchResponse object
StatisticalFacet f = (StatisticalFacet) sr.getFacets().facetsAsMap().get("f"); f.getCount(); // Doc count
f.getMin(); // Min value
f.getMax(); // Max value
f.getMean(); // Mean
f.getTotal(); // Sum of values
f.getStdDeviation(); // Standard Deviation
f.getSumOfSquares(); // Sum of Squares
f.getVariance(); // Variance

Terms Stats Facet

准备一个facet请求

下面的例子新建一个facet请求

FacetBuilders.termsStatsFacet("f")
.keyField("brand")
.valueField("price");

利用facet响应

// sr is here your SearchResponse object
TermsStatsFacet f = (TermsStatsFacet) sr.getFacets().facetsAsMap().get("f");
f.getTotalCount(); // Total terms doc count
f.getOtherCount(); // Not shown terms doc count
f.getMissingCount(); // Without term doc count // For each entry
for (TermsStatsFacet.Entry entry : f) {
entry.getTerm(); // Term
entry.getCount(); // Doc count
entry.getMin(); // Min value
entry.getMax(); // Max value
entry.getMean(); // Mean
entry.getTotal(); // Sum of values
}

地理距离Facet

准备一个facet请求

下面的例子新建一个facet请求

FacetBuilders.geoDistanceFacet("f")
.field("pin.location") // Field containing coordinates we want to compare with
.point(40, -70) // Point from where we start (0)
.addUnboundedFrom(10) // 0 to 10 km (excluded)
.addRange(10, 20) // 10 to 20 km (excluded)
.addRange(20, 100) // 20 to 100 km (excluded)
.addUnboundedTo(100) // from 100 km to infinity (and beyond ;-) )
.unit(DistanceUnit.KILOMETERS); // All distances are in kilometers. Can be MILES

利用facet响应

// sr is here your SearchResponse object
GeoDistanceFacet f = (GeoDistanceFacet) sr.getFacets().facetsAsMap().get("f"); // For each entry
for (GeoDistanceFacet.Entry entry : f) {
entry.getFrom(); // Distance from requested
entry.getTo(); // Distance to requested
entry.getCount(); // Doc count
entry.getMin(); // Min value
entry.getMax(); // Max value
entry.getTotal(); // Sum of values
entry.getMean(); // Mean
}

facet过滤器(不是过滤facet)

默认情况下,不管过滤器存在与否,facet都是作用在查询的结果集上。如果你需要计数带有过滤器的facet,你能够通过AbstractFacetBuilder#facetFilter(FilterBuilder)添加 过滤器到任何facet上。

FacetBuilders
.termsFacet("f").field("brand") // Your facet
.facetFilter( // Your filter here
FilterBuilders.termFilter("colour", "pale")
);

例如,你可以在你的查询中重用创建的过滤器

// A common filter
FilterBuilder filter = FilterBuilders.termFilter("colour", "pale"); TermsFacetBuilder facet = FacetBuilders.termsFacet("f")
.field("brand")
.facetFilter(filter); // We apply it to the facet SearchResponse sr = node.client().prepareSearch()
.setQuery(QueryBuilders.matchAllQuery())
.setFilter(filter) // We apply it to the query
.addFacet(facet)
.execute().actionGet();

作用域

默认情况下,facet作用在查询的结果集上。但是,不管是哪个查询,你可以用global参数去计算来自于索引中的所有文档的facet。

TermsFacetBuilder facet = FacetBuilders.termsFacet("f")
.field("brand")
.global(true);

elasticsearch 中文API facets(⑩)的更多相关文章

  1. elasticsearch 中文API 基于查询的删除(九)

    基于查询的删除API 基于查询的删除API允许开发者基于查询删除一个或者多个索引.一个或者多个类型.下面是一个例子. import static org.elasticsearch.index.que ...

  2. elasticsearch 中文API 记数(八)

    计数API 计数API允许开发者简单的执行一个查询,返回和查询条件相匹配的文档的总数.它可以跨多个索引以及跨多个类型执行. import static org.elasticsearch.index. ...

  3. elasticsearch 中文API bulk(六)

    bulk API bulk API允许开发者在一个请求中索引和删除多个文档.下面是使用实例. import static org.elasticsearch.common.xcontent.XCont ...

  4. elasticsearch 中文API 索引(三)

    索引API 索引API允许开发者索引类型化的JSON文档到一个特定的索引,使其可以被搜索. 生成JSON文档 有几种不同的方式生成JSON文档 利用byte[]或者作为一个String手动生成 利用一 ...

  5. elasticsearch 中文API(一)

    Java API 这节会介绍elasticsearch支持的Java API.所有的elasticsearch操作都使用Client对象执行.本质上,所有的操作都是并行执行的. 另外,Client中的 ...

  6. elasticsearch 中文API river

    river-jdbc 安装 ./bin/plugin --install jdbc --url http://xbib.org/repository/org/xbib/elasticsearch/pl ...

  7. elasticsearch 中文API 更新(五)

    更新API 你能够创建一个UpdateRequest,然后将其发送给client. UpdateRequest updateRequest = new UpdateRequest(); updateR ...

  8. elasticsearch 中文API 删除(四)

    删除API 删除api允许你通过id,从特定的索引中删除类型化的JSON文档.如下例: DeleteResponse response = client.prepareDelete("twi ...

  9. elasticsearch 中文API 获得(三)

    获取API 获取API允许你通过id从索引中获取类型化的JSON文档,如下例: GetResponse response = client.prepareGet("twitter" ...

随机推荐

  1. kaggle 实战 (1): PCA + KNN 手写数字识别

    文章目录 加载package read data PCA 降维探索 选择50维度, 拆分数据为训练集,测试机 KNN PCA降维和K值筛选 分析k & 维度 vs 精度 预测 生成提交文件 本 ...

  2. CI的session操作

    在使用session之前,要对配置文件config.php 里面的$config['encryption_key']随便赋个值,例如1234 1. 首先要加载session类,固定写法:$this-& ...

  3. redis笔记_源码_字典dict

    参考:https://redissrc.readthedocs.io/en/latest/datastruct/dict.html Expand: 条件: 新的table 大小: Rehash: 条件 ...

  4. p分位数的原理及计算

    p分位数的原理及计算 大纲>> 1.统计上的分位数概念   2.分位数的计算方法及举例 2.1首先确定p分位数的位置(依据项数分为基数.偶数情况) 2.2 求上一步确定的p分位数位置处的具 ...

  5. JQUERY 效果 遍历 事件

    效果 隐藏与显示  hide() 和 show() 淡入淡出  fadeIn(speed,callback).fadeOut(speed,callback). fadeToggle() 方法可以在 f ...

  6. 登录操作(方法二:for与else搭配)

    登录操作(方法二:for与else搭配) user_name="star"passwoed='123' count=0for i in range(3): u_username=i ...

  7. mvc和mvvm区别

    mvc和mvvm区别 MVC和MVVM的区别其实并不大.都是一种设计思想. 主要就是MVC中Controller演变成MVVM中的viewModel. MVVM主要解决了MVC中大量的DOM操作使页面 ...

  8. laravel请求处理管道例子

    例子: <?php interface Middleware{ public static function handle (Closure $next);} class VerifyCsrfT ...

  9. WEB前端使用的CSS3选择器

    首先说first-child与last-child,这两个选择器很容易明白,就是父元素下的第一个子元素和最后一个子元素.而nth-child和nth-last-child则是父元素下指定序号的子元素, ...

  10. Window中在Intellij idea开发时常用快捷键

    以下idea中的快捷键是在window 7中确认过,如果快捷键不起作用,可能是该快捷键被其它软件占用,或系统不同导致. 1.Ctrl + Z:撤回代码: 2.Ctrl + Shift + Z:恢复撤回 ...