Facet切面统计(高版本中为aggregations)
来源:互联网 发布:网络系统集成工程师 编辑:程序博客网 时间:2024/04/30 21:25
尽管官网上强调,facet在以后的版本中将会从elasticsearch中移除,推荐使用aggregations。但在工作上,自己还是使用了facet。在阅读《Mastering Elasticsearch》的时候,看到了对facet的介绍,介绍的非常的实用和易懂,于是就摘译了一部分出来,供需要的参考。
当使用ElasticSearch 刻面(faceting)机制时,需要牢记:刻面(faceting)结果仅在查询(query)结果上计算;如果你在query实体外包含过滤(filter),这样的过滤不会限制刻面统计的文档(document)
来看例子:
首先,使用以下命令往books索引内插入一些文本:
curl -XPUT 'localhost:9200/books/book/1' -d '{
"id":"1", "title":"Test book 1", "category":"book",
"price":29.99
}'
curl -XPUT 'localhost:9200/books/book/2' -d '{
"id":"2", "title":"Test book 2", "category":"book",
"price":39.99
}'
curl -XPUT 'localhost:9200/books/book/3' -d '{
"id":"3", "title":"Test comic 1","category":"comic",
"price":11.99
}'
curl -XPUT 'localhost:9200/books/book/4' -d '{
"id":"4", "title":"Test comic 2","category":"comic",
"price":15.99
}'
让我们来看看当使用查询(query)和过滤(filter)时,刻面(faceting)是如何工作的。我们将会执行一个简单的查询(query)——返回books索引上的所有文档。同样,我们会包含一个过滤来将查询结果限制仅仅属于book分类(category),以及包含一个针对price字段的范围切面,来查看有多少文档的价格低于30和有多少是高于30.整个查询如下:
{
"query": {
"match_all": {}
},
"filter": {
"term": {
"category": "book"
}
},
"facets": {
"price": {
"range": {
"field": "price",
"ranges": [
{
"to": 30
},
{
"from": 30
}
]
}
}
}
}
执行后,我们将得到以下结果:
{
…
"hits":{
"total":2,
"max_score": 1.0,
"hits": [
{
"_index": "books",
"_type": "book",
"_id": "1",
"_score": 1.0,
"_source": {
"id": "1",
"title": "Test book 1",
"category": "book",
"price": 29.99
}
},
{
"_index": "books",
"_type": "book",
"_id": "2",
"_score": 1.0,
"_source": {
"id": "2",
"title": "Test book 2",
"category": "book",
"price": 39.99
}
}
]
},
"facets": {
"price": {
"_type": "range",
"ranges": [
{
"to": 30.0,
"count": 3,
"min": 11.99,
"max": 29.99,
"total_count": 3,
"total": 57.97,
"mean": 19.323333333333334
},
{
"from": 30.0,
"count": 1,
"min": 39.99,
"max": 39.99,
"total_count": 1,
"total": 39.99,
"mean": 39.99
}
]
}
}
}
从结果可以看出,尽管filter限制只包括category字段取值为book的文档,但facet并不是只在这些文档上执行,而是在books索引上的所有文档上执行(因为match_all查询)。也就是说,刻面机制在计算的时候是不考虑filter的。但如果filter作为query的一部分呢?比如filtered查询?继续看例子。
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"term": {
"category": "book"
}
}
}
},
"facets": {
"price": {
"range": {
"field": "price",
"ranges": [
{
"to": 30
},
{
"from": 30
}
]
}
}
}
}
返回结果:
{
...
"hits":{
"total": 2,
"max_score": 1.0,
"hits": [
{
"_index": "books",
"_type": "book",
"_id": "1",
"_score": 1.0,
"_source": {
"id": "1",
"title": "Test book 1",
"category": "book",
"price": 29.99
}
},
{
"_index": "books",
"_type": "book",
"_id": "2",
"_score": 1.0,
"_source": {
"id": "2",
"title": "Test book2",
"category": "book",
"price": 39.99
}
}
]
},
"facets": {
"price": {
"_type": "range",
"ranges": [
{
"to": 30.0,
"count": 1,
"min": 29.99,
"max": 29.99,
"total_count": 1,
"total": 29.99,
"mean": 29.99
},
{
"from": 30.0,
"count": 1,
"min": 39.99,
"max": 39.99,
"total_count": 1,
"total": 39.99,
"mean": 39.99
}
]
}
}
}
从返回结果可以看出,这个时候的filter限制了facet的计算范围。
现在,想象我们想要仅仅对title字段包含”2”的书籍计算刻面。我们可以在query增加第二个filter,但是这样的话,会限制查询结果,这并不是我们想要的。我们要做的是引入facet filter。
在提供facet的同级使用facet_filter,这允许我们限制计算刻面的文本。比如如果想限制刻面计算只针对title字段包含”2“的文本,elasticsearch语句可修改为:
{
"query": {
"filtered": {
"query": {
"match_all": {
}
},
"filter": {
"term": {
"category": "book"
}
}
}
}"facets": {
"price": {
"range": {
"field": "price",
"ranges": [
{
"to": 30
},
{
"from": 30
}
]
},
"facet_filter": {
"term": {
"title": "2"
}
}
}
}
}
返回结果:
{
...
"hits":{
"total":2,
"max_score": 1.0,
"hits": [
{
"_index": "books",
"_type": "book",
"_id": "1",
"_score": 1.0,
"_source": {
"id": "1",
"title": "Test book 1",
"category": "book",
"price": 29.99
}
},
{
"_index": "books",
"_type": "book",
"_id": "2",
"_score": 1.0,
"_source": {
"id": "2",
"title": "Test book 2",
"category": "book",
"price": 39.99
}
}
]
},
"facets": {
"price": {
"_type": "range",
"ranges": [
{
"to": 30.0,
"count": 0,
"total_count": 0,
"total": 0.0,
"mean": 0.0
},
{
"from": 30.0,
"count": 1,
"min": 39.99,
"max": 39.99,
"total_count": 1,
"total": 39.99,
"mean": 39.99
}
]
}
}
}
从上面可以看出,facet限制在了一个文本。而query没变。
现在,假如我们想要对所有category字段为”book“的文档进行query(查询),但是想要对索引中的所有文档都进行facet,改怎么办呢?
直接看语句吧:
{
"query": {
"term": {
"category": "book"
}
},
"facets": {
"price": {
"range": {
"field":"price",
"ranges": [
{
"to": 30
},
{
"from": 30
}
]
},
"global": true
}
}
}
返回结果:
{
...
"hits":{
"total":2,
"max_score": 0.30685282,
"hits": [
{
"_index": "books",
"_type": "book",
"_id": "1",
"_score": 0.30685282,
"_source": {
"id": "1",
"title": "Test book 1",
"category": "book",
"price": 29.99
}
},
{
"_index": "books",
"_type": "book",
"_id": "2",
"_score": 0.30685282,
"_source": {
"id": "2",
"title": "Test book 2",
"category": "book",
"price": 39.99
}
}
]
},
"facets": {
"price": {
"_type": "range",
"ranges": [
{
"to": 30.0,
"count": 3,
"min": 11.99,
"max": 29.99,
"total_count": 3,
"total": 57.97,
"mean": 19.323333333333334
},
{
"from": 30.0,
"count": 1,
"min": 39.99,
"max": 39.99,
"total_count": 1,
"total": 39.99,
"mean": 39.99
}
]
}
}
}
这就是global带给facet的好处。
- Facet切面统计(高版本中为aggregations)
- Spring MVC学习—切面的使用(统计service中方法执行的时间)
- 关于项目中导出,导入由jxl转化为poi3.14(高版本低版本通用)的文档说明
- 分组统计之facet
- Solr Facet 统计查询
- ElasticSearch 的 聚合(Aggregations)
- ElasticSearch 的 聚合(Aggregations)
- aggregations 详解1(概述)
- ElasticSearch 的 聚合(Aggregations)
- ElasticSearch 的 聚合(Aggregations)
- 低版本在在高版本中运行 ------ (高版本兼容低版本)
- 低版本在在高版本中运行 ------ (高版本兼容低版本)
- lucene 3.4 contrib/facet 切面搜索
- vs中由低版本转换为高版本出现如下错误的解决办法
- 修改eclipse中maven工程中默认javaSE1.5版本 以及project facet中java版本
- solr中facet及facet.pivot理解(整合两篇文章保留参考)
- 转:solr中facet及facet.pivot理解(整合两篇文章保留参考)
- AOP_通过切面引入新功能(通过切面为方法添加实现接口)
- ROS 服务 消息传递
- 小程序点击跳转页面
- Useful Command in Ubuntu
- WebStorm 2017激活方式
- Android7.0以上(私有目录被限制访问)
- Facet切面统计(高版本中为aggregations)
- LLE流行嵌入式降维算法
- RDD创建的两种方式
- cad shx 字体读取
- 关于URL接口中数据的获取问题
- 欧拉角EulerAngle
- php-fpm超时时间设置request_terminate_timeout分析
- mysql 5.7.20下载安装
- springMVC前台ajax上传excel文件并且解析其中内容