概述
ElasticSearch基本查询(Filter查询)
1、数据准备
2、简单的过滤查询
3、bool过滤查询
4、范围过滤
5、过滤非空、判断存在
6、过滤器缓存
7、聚合查询
对人工智能感兴趣的同学,可以点击以下链接:
现在人工智能非常火爆,很多朋友都想学,但是一般的教程都是为博硕生准备的,太难看懂了。最近发现了一个非常适合小白入门的教程,不仅通俗易懂而且还很风趣幽默。所以忍不住分享一下给大家。点这里可以跳转到教程。
https://www.cbedai.net/u014646662
filter是不计算相关性的,同时可以cache。因此,filter速度要快于query。
1、数据准备
POST /lib4/items/_bulk
{"index": {"_id": 1}}
{"price": 40,"itemID": "ID100123","title":"successful"}
{"index": {"_id": 2}}
{"price": 50,"itemID": "ID100124","title":"successful"}
{"index": {"_id": 3}}
{"price": 25,"itemID": "ID100124","title":"failed"}
{"index": {"_id": 4}}
{"price": 30,"itemID": "ID100125","title":"failed"}
{"index": {"_id": 5}}
{"price": 40,"itemID": "ID100125","title":"failed"}
{"index": {"_id": 6}}
{"price": null,"itemID": "ID100127,"title":"failed"}
2、简单的过滤查询
GET /lib4/items/_search
{
"post_filter":{
"term":{"price": 40}
}
}
GET /lib4/items/_search
{
"post_filter":{
"terms":{"price":[25,50]}
}
}
GET /lib4/items/_search
{
"post_filter":{
"terms":{"itemID.keyword":["ID100124","ID100123"]}
}
}
查看分词器分析的结果:
GET /lib4/_mapping
{
"lib4": {
"mappings": {
"items": {
"properties": {
"itemID": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"price": {
"type": "long"
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
3、bool过滤查询
可以实现组合过滤查询
格式:
{
"bool": {
"must": [],
"should": [],
"must_not": []
}
}
must:必须满足的条件---and
should:可以满足也可以不满足的条件--or
must_not:不需要满足的条件--not
下一篇博客会做更详细的介绍,这里主要是说过滤
GET /lib4/items/_search
{
"post_filter":{
"bool":{
"should":[
{"term": {"itemID.keyword": "ID100124"} },
{"term":{"price":25}}
],
"must_not":{"term":{"price":50}}
}
}
}
嵌套使用bool:
GET /lib4/items/_search
{
"post_filter": {
"bool": {
"should": [
{"term": {"itemID.keyword": "id100123"}},
{
"bool": {
"must": [
{"term": {"price": 40}}
]
}
}
]
}
}
}
4、范围过滤
gt: >
lt: <
gte: >=
lte: <=
get lib4/items/_search
{
"post_filter":{
"range":{
"price":{"gt":20,"lt":50}
}
}
}
5、过滤非空
get lib4/items/_search
{
"query":{
"constant_score": {
"filter": {
"exists": {
"field": "price"
}
}
}
}
}
get lib4/items/_search
{
"query":{
"constant_score": {
"filter": {"exists": {
"field": "price"
}}
}
}
}
6、过滤器缓存
ElasticSearch提供了一种特殊的缓存,即过滤器缓存(filter cache),用来存储过滤器的结果,被缓存的过滤器并不需要消耗过多的内存(因为它们只存储了哪些文档能与过滤器相匹配的相关信息),而且可供后续所有与之相关的查询重复使用,从而极大地提高了查询性能。
注意:ElasticSearch并不是默认缓存所有过滤器,
以下过滤器默认不缓存:
numeric_range、script、geo_bbox、geo_distance、geo_distance_range、geo_polygon、geo_shape、and、or、not
默认是开启缓存的过滤器:
exists,missing,range,term,terms
开启方式:在filter查询语句后边加上
"_catch":true
7、聚合查询
sum、min、max、avg、cardinality:求基数、terms:分组
以求和为例:
GET /lib4/items/_search
{
"size":0,
"aggs": {
"price_sum": { //名字自定义
"sum": {
"field": "price"
}
}
}
}
执行结果
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 0,
"hits": []
},
"aggregations": {
"price_sum": {
"value": 185
}
}
}
对那些有游泳兴趣的用户按年龄分组
GET /lib3/user/_search
{
"query": {
"match": {
"interests": "youyong"
}
},
"size": 0,
"aggs":{
"age_group_by":{
"terms": {
"field": "age",
"order": {
"avg_of_age": "desc"
}
},
"aggs": {
"avg_of_age": {
"avg": {
"field": "age"
}
}
}
}
}
}
//数据参见上一篇博客
执行结果如下:
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0,
"hits": []
},
"aggregations": {
"age_group_by": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 22,
"doc_count": 1,
"avg_of_age": {
"value": 22
}
},
{
"key": 20,
"doc_count": 2,
"avg_of_age": {
"value": 20
}
},
{
"key": 18,
"doc_count": 1,
"avg_of_age": {
"value": 18
}
}
]
}
}
}
最后
以上就是俊逸灯泡为你收集整理的ElasticSearch基本查询(Filter查询)ElasticSearch基本查询(Filter查询)的全部内容,希望文章能够帮你解决ElasticSearch基本查询(Filter查询)ElasticSearch基本查询(Filter查询)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复