ES DSL Search-Filter

1 Introduction

Mainly introduce the basic API operation of index request, use postman to make the request, the prefix address of the interface request is unified as the elasticsearch deployment IP address + port number (for example, http://192.168.51.4:9200.

Unified request address:

POST /search_demo/_doc/_search

Filters are used to filter data from the search results. It will not search in the es library, and will not calculate the relevance score of the document, so the filtering performance will be higher, and the filter can be used in conjunction with full-text search.

2 post_filter

post_filterThe element is a top-level element and only filters the search results. It does not calculate the relevance score of the matching degree of the data, and does not sort it according to the score. On the contrary, the query will calculate the score and sort it according to the score.

scenes to be used:

  • query: Retrieve matching records based on user search criteria
  • post_filter: Filter the result data after user query

Example query: query users whose account amount is greater than 80 but less than 160

  • gte: greater than or equal to
  • lte: less than or equal to
  • gt: greater than
  • lt: less than

传递JSON数据

{
    
    
    "query": {
    
    
        "match": {
    
    
            "desc": "好的"
        }
    },
    "post_filter": {
    
    
        "range": {
    
    
            "money": {
    
    
                "gt": 60,
                "lt": 1000
            }
        } 
    }
  
}

请求结果

{
    
    
    "took": 2,
    "timed_out": false,
    "_shards": {
    
    
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
    
    
        "total": {
    
    
            "value": 7,
            "relation": "eq"
        },
        "max_score": 3.0979095,
        "hits": [
            {
    
    
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1005",
                "_score": 3.0979095,
                "_source": {
    
    
                    "id": 1005,
                    "age": 25,
                    "username": "switch",
                    "nickname": "switch游戏机",
                    "money": 155.8,
                    "desc": "好的游戏,才会有人购买,比如塞尔达",
                    "sex": 1,
                    "birthday": "1989-03-14",
                    "face": "http://www.p2pi.cn/static/img/1005_face.png"
                }
            },
            {
    
    
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1003",
                "_score": 0.37556386,
                "_source": {
    
    
                    "id": 1003,
                    "age": 20,
                    "username": "youTuTu",
                    "nickname": "涡轮增压",
                    "money": 66.8,
                    "desc": "极限的速度是需要涡轮增压的",
                    "sex": 1,
                    "birthday": "1996-01-14",
                    "face": "http://www.p2pi.cn/static/img/1003_face.png"
                }
            },
            {
    
    
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1012",
                "_score": 0.36424035,
                "_source": {
    
    
                    "id": 1012,
                    "age": 19,
                    "username": "youzi",
                    "nickname": "youzi",
                    "money": 188.8,
                    "desc": "永远的神",
                    "sex": 1,
                    "birthday": "1980-08-14",
                    "face": "http://www.p2pi.cn/static/img/1012_face.png"
                }
            },
            {
    
    
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1002",
                "_score": 0.35254776,
                "_source": {
    
    
                    "id": 1002,
                    "age": 19,
                    "username": "Ailun",
                    "nickname": "进击的巨人",
                    "money": 77.8,
                    "desc": "艾伦是会变成真正的巨人的",
                    "sex": 1,
                    "birthday": "1993-01-24",
                    "face": "http://www.p2pi.cn/static/img/1002_face.png"
                }
            },
            {
    
    
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1011",
                "_score": 0.27665582,
                "_source": {
    
    
                    "id": 1011,
                    "age": 31,
                    "username": "petter",
                    "nickname": "皮特",
                    "money": 180.8,
                    "desc": "皮特的姓氏好像是彼得",
                    "sex": 1,
                    "birthday": "1989-08-14",
                    "face": "http://www.p2pi.cn/static/img/1011_face.png"
                }
            },
            {
    
    
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1009",
                "_score": 0.252381,
                "_source": {
    
    
                    "id": 1009,
                    "age": 22,
                    "username": "lucy",
                    "nickname": "露西",
                    "money": 96.8,
                    "desc": "露西是一只很聪明的cat",
                    "sex": 1,
                    "birthday": "1998-07-14",
                    "face": "http://www.p2pi.cn/static/img/1009_face.png"
                }
            },
            {
    
    
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.18093815,
                "_source": {
    
    
                    "id": 1001,
                    "age": 18,
                    "username": "Tic",
                    "nickname": "飞翔的荷兰号",
                    "money": 88.8,
                    "desc": "我在p2pi网站解决项目中遇到的问题,学习到了很多知识",
                    "sex": 0,
                    "birthday": "1992-12-24",
                    "face": "http://www.p2pi.cn/static/img/1001_face.png"
                }
            }
        ]
    }
}

3 Related information

  • The blog post is not easy, everyone who has worked so hard to pay attention and praise, thank you

Guess you like

Origin blog.csdn.net/qq_15769939/article/details/114651289