本系列文章将展示ElasticSearch中23种非常有用的查询使用方法。由于篇幅原因,本系列文章分为六篇,本文是此系列的第五篇文章。欢迎关注大数据技术博客微信公共账号:iteblog_hadoop。
《23种非常有用的ElasticSearch查询例子(1)》
《23种非常有用的ElasticSearch查询例子(2)》
《23种非常有用的ElasticSearch查询例子(3)》
《23种非常有用的ElasticSearch查询例子(4)》
《23种非常有用的ElasticSearch查询例子(5)》
《23种非常有用的ElasticSearch查询例子(6)》
《23种非常有用的ElasticSearch查询例子(2)》
《23种非常有用的ElasticSearch查询例子(3)》
《23种非常有用的ElasticSearch查询例子(4)》
《23种非常有用的ElasticSearch查询例子(5)》
《23种非常有用的ElasticSearch查询例子(6)》
Filtered Query(过滤查询)
过滤查询允许我们对查询结果进行筛选。比如:我们查询标题和摘要中包含Elasticsearch关键字的图书,但是我们想过滤出评论大于20的结果,可以如下使用:
/////////////////////////////////////////////////////////////////////
User: 过往记忆
Date: 2016-10-01
Time: 23:36
bolg: https://www.iteblog.com
本文地址:https://www.iteblog.com/archives/1779.html
过往记忆博客,专注于hadoop、hive、spark、shark、flume的技术博客,大量的干货
过往记忆博客微信公共帐号:iteblog_hadoop
/////////////////////////////////////////////////////////////////////
curl POST https://www.iteblog.com:9200/iteblog_book_index/book/_search
{
"query": {
"filtered": {
"query" : {
"multi_match": {
"query": "elasticsearch",
"fields": ["title","summary"]
}
},
"filter": {
"range" : {
"num_reviews": {
"gte": 20
}
}
}
}
},
"_source" : ["title","summary","publisher", "num_reviews"]
}
[返回结果]
{
"took": 20,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": [
{
"_index": "bookdb_index",
"_type": "book",
"_id": "1",
"_score": 0.5955761,
"_source": {
"summary": "A distibuted real-time search and analytics engine",
"publisher": "oreilly",
"num_reviews": 20,
"title": "Elasticsearch: The Definitive Guide"
}
}
]
}
注意:过滤查询(Filtered queries)并不强制过滤条件中指定查询。如果没有指定查询条件,则会运行match_all查询,其将会返回index中所有文档,然后对其进行过滤。在实际运用中,过滤器应该先被执行,这样可以减少需要查询的范围。而且,第一次使用fliter之后其将会被缓存,这样会对性能代理提升。
Filtered queries在即将发行的Elasticsearch 5.0中移除了,我们可以使用
bool查询来替换他。下面是使用bool查询来实现上面一样的查询效果,返回结果一样:
curl POST https://www.iteblog.com:9200/iteblog_book_index/book/_search
{
"query": {
"bool": {
"must" : {
"multi_match": {
"query": "elasticsearch",
"fields": ["title","summary"]
}
},
"filter": {
"range" : {
"num_reviews": {
"gte": 20
}
}
}
}
},
"_source" : ["title","summary","publisher", "num_reviews"]
}
这个例子这也适用于下面的多个过滤器例子。
Multiple Filters(多过滤器查询)
多过滤器查询可以通过结合使用bool过滤查询实现。下面的示例中,我们将筛选出返回的结果必须至少有20条评论,必须是在2015年之前发布的,而且应该是由O'Reilly出版的,如下所示:
/////////////////////////////////////////////////////////////////////
User: 过往记忆
Date: 2016-10-01
Time: 23:36
bolg: https://www.iteblog.com
本文地址:https://www.iteblog.com/archives/1779.html
过往记忆博客,专注于hadoop、hive、spark、shark、flume的技术博客,大量的干货
过往记忆博客微信公共帐号:iteblog_hadoop
/////////////////////////////////////////////////////////////////////
curl POST https://www.iteblog.com:9200/iteblog_book_index/book/_search
{
"query": {
"filtered": {
"query" : {
"multi_match": {
"query": "elasticsearch",
"fields": ["title","summary"]
}
},
"filter": {
"bool": {
"must": {
"range" : { "num_reviews": { "gte": 20 } }
},
"must_not": {
"range" : { "publish_date": { "lte": "2014-12-31" } }
},
"should": {
"term": { "publisher": "oreilly" }
}
}
}
}
},
"_source" : ["title","summary","publisher", "num_reviews", "publish_date"]
}
[返回结果]
{
"took": 21,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": [
{
"_index": "bookdb_index",
"_type": "book",
"_id": "1",
"_score": 0.5955761,
"_source": {
"summary": "A distibuted real-time search and analytics engine",
"publisher": "oreilly",
"num_reviews": 20,
"title": "Elasticsearch: The Definitive Guide",
"publish_date": "2015-02-07"
}
}
]
}
本博客文章除特别声明,全部都是原创!原创文章版权归过往记忆大数据(过往记忆)所有,未经许可不得转载。
本文链接: 【23种非常有用的ElasticSearch查询例子(5)】(https://www.iteblog.com/archives/1779.html)

