(15)ElasticSearch Filter查询

Posted javasl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(15)ElasticSearch Filter查询相关的知识,希望对你有一定的参考价值。

filter是不计算相关性的,同时可以cache,因此,filter速度要快于query

1、准备数据

POST /lib7/items/_bulk
"index":"_id":1
"price":40,"itemID":"ID100123"
"index":"_id":2
"price":50,"itemID":"ID100124"
"index":"_id":3
"price":25,"itemID":"ID100125"
"index":"_id":4
"price":30,"itemID":"ID100126"
"index":"_id":5
"price":null,"itemID":"ID100127"

2、查询price是40的;查询price是25或者40的

GET /lib7/items/_search

    "query":
        "bool":
            "filter":[
                "term":"price":40
            ]
        
    


GET /lib7/items/_search

    "query":
        "bool":
            "filter":[
                "terms":"price":[25,40]
            ]
        
    

3、查询itemID是ID100123的,用第一种方式查询不出来,因为创建时itemID的mapping类型默认是text,存储时分词,存储的是id100123。默认小写。

#查询不出
GET /lib7/items/_search

    "query":
        "bool":
            "filter":[
                "term":"itemID":"ID100123"
            ]
        
    


#能查询出
GET /lib7/items/_search

    "query":
        "bool":
            "filter":[
                "term":"itemID":"id100123"
            ]
        
    

  或者自定义mapping,改为不分词的:

#查看mapping
GET /lib7/_mapping

#删除mapping
DELETE  lib7

#重新创建mapping
PUT /lib7

    "mappings":
        "items":
            "properties":
                "itemID":
                    "type":"text",
                    "index":false
                
            
        
    

4、bool过滤查询。可以实现组合过滤查询,格式如下 :

  "bool":"must":[],"should":[],"must_not":[]

  must:必须满足的条件---and

  should:可以满足也可以不满足的条件---or

  must_not:不需要满足的条件-----not

(4.1)查询price是25或者itemID是id100123的 并且 price不能是30

GET /lib7/items/_search

    "post_filter":
        "bool":
            "should":[
                "term":"price":25,
                "term":"itemID":"id100123"
            ],
            "must_not":
                "term":"price":30
            
        
    


#或者
GET /lib7/items/_search

    "query":
        "bool":
            "should":[
                "term":"price":25,
                "term":"itemID":"id100123"
            ],
            "must_not":
                "term":"price":30
            
        
    

(4.2)嵌套使用bool:要么id100123,要么(id是100124价格是40)并且价格 不能是30

GET /lib7/items/_search

    "post_filter":
        "bool":
            "should":[
                "term":"itemID":"id100123",
                "bool":"must":[
                    "term":"itemID":"id100124",
                    "term":"price":40
                ]
            ],
            "must_not":
                "term":"price":30
            
        
    

5、范围过滤。gt:>;lt:<;gte:>=;lte:<=

(5.1)price价格大于25且小于50的

GET /lib7/items/_search

    "post_filter":
        "range":
            "price":
                "gt":25,"lt":50
            
        
    

(5.2)查询价格不是null的

GET /lib7/items/_search

    "query":
        "bool":
            "filter":
                "exists":
                    "field":"price"
                
            
        
    

 

  

以上是关于(15)ElasticSearch Filter查询的主要内容,如果未能解决你的问题,请参考以下文章

理解elasticsearch的post_filter

Elasticsearch:过滤搜索结果 - filter 及 post_filter

[ElasticSearch2.x]Filter之Cache

elasticsearch中Query DSL中的must和filter有啥区别?

说说 Elasticsearch filter 和 post_filter 的区别?

说说 Elasticsearch filter 和 post_filter 的区别?