Advanced search (DSL search) of elastic search (ES) (Part I)

Keywords: ElasticSearch Distribution search engine

catalogue

1. General

2. Scenario description

3. Advanced search (DSL search) (Part I)

4. Overview

1. General

I talked before   Let's talk about the basic use of elastic search (ES)   The advanced search (DSL search) of elastic search (ES) is divided into two articles because there are many DSL Search contents.

2. Scenario description

2.1 create index and map at the same time

PUT  http://192.168.1.11:9200/index_user

Parameters:

{
    "settings":{
        "index":{
            "number_of_shards":5,
            "number_of_replicas":0
        }
    },
    "mappings" : {
        "properties":{
            "user_id":{
                "type":"long"
            },
            "name":{
                "type":"text",
                "analyzer":"ik_max_word"
            },
            "login_name":{
                "type":"keyword"
            },
            "age":{
                "type":"integer"
            },
            "birthday":{
                "type":"date"
            },
            "desc":{
                "type":"text",
                "analyzer":"ik_max_word"
            },
            "head_url":{
                "type":"text",
                "index":false
            }
        }
    }
}

2.2 creating documents

Here are just a few examples

POST  http://192.168.1.11:9200/index_user/_doc/1

Parameters:

{
    "user_id":"1",
    "name":"Zombie Mayhem ",
    "login_name":"jsls",
    "age":25,
    "birthday":"1990-03-01",
    "desc":"I'm a real estate agent. Now I've changed my career. At present, I'm a transportation worker",
    "head_url":"https://www.zhuifengren.cn/img/jsls.jpg"
}

POST  http://192.168.1.11:9200/index_user/_doc/2

Parameters:

{
    "user_id":"2",
    "name":"Xavier",
    "login_name":"xwe",
    "age":28,
    "birthday":"1992-06-06",
    "desc":"I am a senior development manager. I take the subway to work every day. I live in Beijing and never get stuck in traffic",
    "head_url":"https://www.zhuifengren.cn/img/xwe.jpg"
}

POST  http://192.168.1.11:9200/index_user/_doc/3

Parameters:

{
    "user_id":"3",
    "name":"Disney at large Xianrou",
    "login_name":"dsnzxr",
    "age":10,
    "birthday":"2011-06-22",
    "desc":"I am a primary school student in Grade 5. I take a special bus every day. I join the school at noon. The food in the canteen is good. I won the three good student award in the final exam of last semester",
    "head_url":"https://www.zhuifengren.cn/img/dsnzxr.jpg"
}

........................

3. Advanced search (DSL search) (Part I)

3.1 specify query criteria in url (non DSL search)

GET    http://192.168.1.11:9200/index_user/_doc/_search?q=desc: one & Q = age: 10

desc is a field in the index, and one is the keyword to be retrieved

age is a field in the index, and 10 is the value to look up

This method is less used.

3.2 DSL basic query

POST  http://192.168.1.11:9200/index_user/_doc/_search

Parameters:

{
    "query":{
        "match":{
            "desc":"one person"
        }
    }
}

3.3 does a field exist in the document

If this field exists in the document, it will be found

POST  http://192.168.1.11:9200/index_user/_doc/_search

Parameters:

{
    "query":{
        "exists":{
            "field":"name"
        }
    }
}

3.4 query all  

POST  http://192.168.1.11:9200/index_user/_doc/_search

Parameters:

{
    "query":{
        "match_all":{}
    }
}

3.5 query some fields

POST  http://192.168.1.11:9200/index_user/_doc/_search

Parameters:

{
    "query":{
        "match_all":{}
    },
    "_source": [
        "user_id",
        "name"
    ]
}

3.6 paging

POST  http://192.168.1.11:9200/index_user/_doc/_search

Parameters:

{
    "query":{
        "match_all":{}
    },
    "from":0,   // Which document to start from? The document subscript starts from 0
    "size":10   // How many documents per page
}

  3.7 term query

term query will not segment keywords, but directly query them.

POST  http://192.168.1.11:9200/index_user/_doc/_search 

Parameters:

{
    "query":{
        "term":{
            "desc":"one person"
        }
    }
}

3.8 match query

For match query, the keyword will be segmented first, then each segmented word will be used to query, and finally the results will be combined.

POST  http://192.168.1.11:9200/index_user/_doc/_search 

Parameters:

{
    "query":{
        "match":{
            "desc":"A pupil"
        }
    }
}

3.9 terms query

Similar to term query, you can write multiple keywords, query with each keyword, and finally merge the results.

POST  http://192.168.1.11:9200/index_user/_doc/_search 

Parameters:

{
    "query":{
        "terms":{
            "desc":[
                "one person",
                "pupil"
            ]
        }
    }
}

three point one zero   match_phrase query

Similar to match, the keyword will be segmented first, and then each segmented word will be used to query. However, there will be certain restrictions on the interval between segmented words in the document. Use the slop attribute to limit it. The default is 0. It needs to be less than the set interval to match the document.

For example, if slop is set to 0, the two participles must be next to each other in the document, otherwise they cannot be hit.

POST  http://192.168.1.11:9200/index_user/_doc/_search 

Parameters:

{
    "query":{
        "match_phrase":{
            "desc":{
                "query": "A student",
                "slop":8
            }
        }
    }
}

3.11 match query extension

1) After match query, the result takes the intersection

POST  http://192.168.1.11:9200/index_user/_doc/_search 

Parameters:

{
    "query":{
        "match":{
            "desc": {
                "query": "A pupil",
                "operator": "and"
            }
        }
    }
}

2) Specify matching rate

Specified as an integer, which means the minimum number of document matching participles. For example, set to 3, which means that if 3 participles in the keyword hit the document, it can be found.

Specify as percentage, which means the percentage of matching keyword segmentation. For example, set it to 60%. If the number of keyword segmentation is 4, the number of matching keyword segmentation divided by the total number is greater than 60%.

POST  http://192.168.1.11:9200/index_user/_doc/_search 

Parameters:

{
    "query":{
        "match":{
            "desc": {
                "query": "A pupil",
                "minimum_should_match":"3"  // Or "minimum_should_match":"60%"
            }
        }
    }
}

3.12 query using id set

POST  http://192.168.1.11:9200/index_user/_doc/_search 

Parameters:

{
    "query": {
        "ids":{
            "type":"_doc",
            "values":["1","3"]
        }
    }
}

3.13 multi field query of match

When the field name is written as   "name^10" means to increase the weight of the name field by 10 times, and the score of the documents that hit name will be higher and the sorting will be higher.

POST  http://192.168.1.11:9200/index_user/_doc/_search 

Parameters:

{
    "query":{
        "multi_match":{
            "query":"A pupil zombie",
            "fields":[
                    "desc",
                    "name"   // "name^10"
                ]
        }
    }
}

4. Overview

Today, I briefly talked about part of the advanced search (DSL search) of elastic search (ES), hoping to be helpful to everyone's work.

You are welcome to help us with praise, comments and attention:)

Pay attention to those who follow the wind to talk about Java and update Java dry goods every day.

Posted by codered27 on Mon, 20 Sep 2021 14:39:08 -0700