Es DSL search – Multi_ Match, boost, and Boolean queries

Time:2021-12-8

1 Introduction

It mainly introduces the basic API operation of index request. The request is made using postman. The prefix address of interface request is unified as elasticsearch deployment IP address + port number (for example: http://192.168.51.4:9200 。

Unified request address:

POST /search_demo/_doc/_search

2 multi_match

multi_matchMeet the requirements of using match to query in multiple fields

Passing JSON data

{
    "query": {
        "multi_match": {
            "Query": "combination",
            "fields": [
                "desc","nickname"
            ]
        }
    },
        "_source": [
            "id",
            "nickname",
            "desc"
        ]
}

Request result

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.2874916,
        "hits": [
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1007",
                "_score": 2.2874916,
                "_source": {
                    "Nickname": "old boy",
                    "id": 1007,
                    "Desc": "it's really a good combination, chopsticks"
                }
            }
        ]
    }
}

3 boost

boostWeight: set the weight for a field. The higher the weight, the higher the document relevance score. Generally speaking, the weight of searching product name is higher than that of product profile.

Passing JSON data

{
    "query": {
        "multi_match": {
            "Query": "OK",
            "fields": [
                "desc","nickname*10"
            ]
        }
    },
        "_source": [
            "id",
            "nickname",
            "desc"
        ]
}

Request result

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 9,
            "relation": "eq"
        },
        "max_score": 3.1980762,
        "hits": [
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1004",
                "_score": 3.1980762,
                "_source": {
                    "Nickname": "red hat",
                    "id": 1004,
                    "Desc": "a good system must have a stable system structure"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1005",
                "_score": 3.0979095,
                "_source": {
                    "Nickname": "switch game console",
                    "id": 1005,
                    "Desc": "only good games can be bought, such as Zelda"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1003",
                "_score": 0.37556386,
                "_source": {
                    "Nickname": "turbocharging",
                    "id": 1003,
                    "Desc": "extreme speed requires turbocharging"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1012",
                "_score": 0.36424035,
                "_source": {
                    "nickname": "youzi",
                    "id": 1012,
                    "Desc": "eternal God"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1002",
                "_score": 0.35254776,
                "_source": {
                    "Nickname": "attacking giant",
                    "id": 1002,
                    "Desc": "Allen will become a real giant"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1011",
                "_score": 0.27665582,
                "_source": {
                    "Nickname": "Pete",
                    "id": 1011,
                    "Desc": "Peter's last name seems to be Peter"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1007",
                "_score": 0.2639615,
                "_source": {
                    "Nickname": "old boy",
                    "id": 1007,
                    "Desc": "it's really a good combination, chopsticks"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1009",
                "_score": 0.252381,
                "_source": {
                    "Nickname": "Lucy",
                    "id": 1009,
                    "Desc": "Lucy is a very clever cat"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.18093815,
                "_source": {
                    "Nickname": "flying Holland",
                    "id": 1001,
                    "Desc": "I have learned a lot from solving problems encountered in the p2pi website project"
                }
            }
        ]
    }
}

Nickname ^ 10 means that the search improves the relevance by 10 times, that is, when users search, it is mainly nickname, supplemented by desc. The matching relevance of nickname is high, as long as the weight proportion is increased.

4 Boolean query

Multiple combined queries can be implemented

  • Must: the query must match the search criteria, such as and
  • Should: query matching meets a condition, such as or
  • must_ Not: does not match the search criteria, and none of them is satisfied, such as not in

Single query

{
    "query": {
        "bool": {
            "must": [
                {
                    "multi_match": {
                        "Query": "OK",
                        "fields": ["desc","nickname"]
                    }
                },
                {
                    "term": {
                        "sex": 0
                    }
                },
                {
                    "term": {
                        "birthday": "1992-12-24"
                    }
                }
            
            ]
        }
    },
        "_source": [
            "id",
            "sex",
            "nickname",
            "desc"
        ]
}

Combined query

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "Desc": "OK"
                    }
                },
                {
                    "match": {
                        "Nickname": "OK"
                    }
                }
            ],
            "should": [
                {
                    "match": {
                        "sex": 1
                    }
                }
            ],
            "must_not":[
                {
                    "term": {
                        "birthday": "1993-01-24"
                    }
                }
            ]
            
        }
    },
        "_source": [
            "id",
            "sex",
            "nickname",
            "desc",
            "birthday"
        ]
}

Weight specified words

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "desc": {
                            "Query": "OK",
                            "boost": 2
                        }
                    }
                },
                {
                    "match": {
                        "desc": {
                            "Query": "boy",
                             "boost": 20
                        }
                    }
                }
            ]
            
        }
    },
        "_source": [
            "id",
            "sex",
            "nickname",
            "desc",
            "birthday"
        ]
}

5 relevant information

  • Blog is not easy. Thank you for your attention and praise