1. Data preparation

  • 1.1 Create an index named “index” (shop)
  • 1.2 Add the following fields
{
    "properties": {
        "id": {
            "type": "long"
        },
        "age": {
            "type": "integer"
        },
        "username": {
            "type": "keyword"
        },
        "nickname": {
            "type": "text",
            "analyzer": "ik_max_word"
        },
        "money": {
            "type": "float"
        },
        "desc": {
            "type": "text",
            "analyzer": "ik_max_word"
        },
        "sex": {
            "type": "byte"
        },
        "birthday": {
            "type": "date"
        },
        "face": {
            "type": "text",
            "index": false
        }
    }
}
Copy the code

The HTTP POST request creates the above fields

POST /shop/_mapping HTTP/1.1
Host: 192.168.10.235:9200
Content-Type: application/json
Content-Length: 654

{
    "properties": {
        "id": {
            "type": "long"
        },
        "age": {
            "type": "integer"
        },
        "username": {
            "type": "keyword"
        },
        "nickname": {
            "type": "text",
            "analyzer": "ik_max_word"
        },
        "money": {
            "type": "float"
        },
        "desc": {
            "type": "text",
            "analyzer": "ik_max_word"
        },
        "sex": {
            "type": "byte"
        },
        "birthday": {
            "type": "date"
        },
        "face": {
            "type": "text",
            "index": false
        }
    }
}
Copy the code
  • 1.3 Adding Test Data

The HTTP POST request

POST /shop/_doc/1001 HTTP/1.1 Host: 192.168.10.2323:9200 Content-Type: Application /json Content-Length: 335 {"id": 1001, "age": 18, "username": "zhangsan", "money": 199.8, "desc": "I'm learning Elasticsearch", "sex": 0, "birthday": "2003-09-01", "face": "http://www.imooc.com/static/img/index/logo.png" }Copy the code

Complete a post request link is: http://192.168.10.235:9200/shop/_doc/1001

The 1001 at the end of the link corresponds best to the JSON id:1001.

In this format, add more data for testing.

2. Simple data query

  • 2.1 GET Request Query Data (Unconditional, query all data)
http://192.168.10.235:9200/shop/_search
Copy the code
  • 2.2 GET Request Query Data (Adding Query conditions)
A query conditions at http://192.168.10.235:9200/shop/_search? Q = nickname: zhang SAN multiple http://192.168.10.235:9200/shop/_search? query conditions Q =nickname: zhang Sam &q=desc: learningCopy the code
  • 2.3 POST Requests to query data
POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 79 {"query": {"match": {"nickname": "booty"}}Copy the code

Post request link is fixed: http://192.168.10.235:9200/shop/_search

The search criteria are written in JSON, match is the es keyword, and if you make an error, the request will get an error.

3. DSL search – all queries, paging queries

  • 3.1 GET request
http://192.168.10.235:9200/shop/_search
Copy the code
  • 3.2 For POST requests, use the es keywordmatch_all
POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 48 {"query": { "match_all": {} } }Copy the code

Post is recommended for all queries. If you need to add paging and filtering functions later, get request links will become more complicated.

  • 3.3 Returns only specified fields_source
POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 120 {"query": { "match_all": {} }, "_source": [ "id", "nickname", "age" ] }Copy the code

In this case, only the specified fields ID, nickname, and age are returned in the query result.

  • 3.4 Paging query, usedThe from, the size,
POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 151 {"query": { "match_all": {} }, "_source": [ "id", "nickname", "age" ], "from": 0, "size": 10 }Copy the code

From: Start with the number of data items

Size: How many pieces of data are returned on a page

4. DSL Search – No word segmentation searchterm

Match performs word segmentation during query

Term stands for perfect match, that is, no word segmentation analysis is performed, and the document must contain the entire search term

POST /shop/_search HTTP/1.1
Host: 192.168.10.235:9200
Content-Type: application/json
Content-Length: 77

{
    "query": {
        "term": {
            "desc": "学习"
        }
    }
}
Copy the code

Search for data that contains “learn” in DESC

If you need to match multiple words, you can use terms

POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 77 {"query": {"terms": {"desc": [" in "," in "]}}}Copy the code

5. DSL Search – Adjacent search termsmatch_phrase

Desc = desc;

# 1 “Have you finished college yet?”

# 2: “After I graduated from university, I went to graduate school”

Now use match_PHRASE for the query

POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 129 {"query": {" match_phrase ": {" desc" : {" query ":" college "}}}}Copy the code

At this point, only the second data can be queried, and the first data cannot be matched.

It is match_phrase that matches the phrase “graduated from college”.

So how do I get the first and second data at the same time

You can use the keyword slop, that is, to skip certain characters

For example, if there is a two-character gap between college and graduation, use “slop”: 2

POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 156 {"query": {" match_phrase ": {" desc" : {" query ":" college ", "slop" : 2}}}}Copy the code

As long as the SLOP is greater than or equal to 2, both data can be queried.

For detailed explanation and usage of match_PHRASE, see the official document for phrase matching

5. DSL search – operatoroperator

Look at the request

POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 154 {"query": {" match ": {" desc" : {" query ":" learning ", "operator" : "or"}}}}Copy the code

We know that match will perform word segmentation. When querying, it will divide “learning” into two words “in” and “learning” for retrieval and matching

Use OR to query for data containing ‘in’ or ‘learn’ in DESC

If you use and (“operator”: “and”), only data with “learning” in DESC will be queried

If the query condition is a single sentence, such as “make developers a respectable profession”, the word will be broken into many words and the matched data will be too many, which makes the query precision not high enough.

You can use minimum_should_match to improve the matching accuracy.

Let’s take a look at how many words ES would break down into “making developers a respectable profession”.

POST /_analyze HTTP/1.1 Host: 192.168.10.2323:9200 Content-Type: application/json Content-Length: 64 {" Analyzer ": "Ik_max_word ", "text":" Make developer a respectable profession "}Copy the code

The query result is divided into 13 words, so if it is directly matched, it will query a lot of data

Minimum_should_match represents the minimum that is matched

POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 167 {"query": {" match ": {" desc" : {" query ":" let developers become a respected profession ", "minimum_should_match" : "60%"}}}}Copy the code

Here 60% means that the search results should meet more than 60% (inclusive) of 13 words, i.e. 13*0.6=7.8, rounded down to 7

You need to include seven or more words, not just one of them.

It’s not just a percentage, it’s a number, for example: “minimum_should_match”: “3”

Contains three or more words, including three

6. DSL search – Query data by IDids

This is relatively simple, use the keyword IDS, you can query multiple

POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 164 {"query": { "ids": { "type": "_doc", "values": [ "1001", "1006" ] } } }Copy the code

Just fill in the VALUES array with the ID you want to query.

7. DSL search – Multiple queriesmulti_matchAnd increase the weight of the specified field

Multiple query means using multiple fields to perform a query

Here are two pieces of data:

Nickname = desc; nickname = desc

Use the keyword multi_match

POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 175 {"query": {" multi_match ": {" query" : "sand monk," "fields" : [" nickname ", "desc"]}}}Copy the code

To increase the weight of the specified field, use ^

For example, if I want to increase the weight of nickname in the query, I just add ^ after nickname

POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 175 {"query": {" multi_match ": {" query" : "sand monk," "fields" : [" nickname ^ 10 ", "desc"]}}}Copy the code

The result order of the query is not the same as the default.

The max_score and _score values in the returned data are also changed

7. DSL search – Combined querybool

Combine multiple conditions

  • 7.1 Must: indicates that the following two conditions must be met at the same time
POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 481 {"query": {" bool ": {" must" : [{" multi_match ": {" query" : "sand monk", and "fields" : [" nickname ", "desc"]}}, {" term ": {" sex" : 0}}]}}}Copy the code
  • 7.2 should: One of the following two conditions should be met
{" query ": {" bool" : {" should ": [{" multi_match" : {" query ":" sand monk ", and "fields" : [" nickname ", "desc"]}}, {" term ": { "sex": 0 } } ] } } }Copy the code
  • 7.3 Must_NOT: The opposite of must must be true
{" query ": {" bool" : {" must_not ": [{" multi_match" : {" query ":" sand monk ", and "fields" : [" nickname ", "desc"]}}, {" term ": { "sex": 0 } } ] } } }Copy the code
  • 7.4 Must, must_not, and should can be used together
POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 771 {"query": {" bool ": {" must" : [{" match ": {" desc" : "university"}}], "must_not" : [{" term ": {" sex" : "1"}}], "should" : [{" range ": {" age" : {" gt ":" 18 ", "lt" : "22"}}}, {" match ": {" desc" : "graduation"}}]}}}Copy the code

8. DSL search – Filterspost_filter

Filter out data with money less than 100 and more than 1000

POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 226 {"query": {" match ": {" desc" : "university"}}, "post_filter" : {" range ": {" money" : {" gt ": 100," lt ": 1000}}}}Copy the code

Post_filter and query are equivalent

9. DSL search – Sortsort

Es sort by _score by default

Positive sequence asc

Reverse desc

POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 144 {"query": {" match ": {" desc" : "university"}}, "sort" : [] {" age ":" asc "}}Copy the code

Multiple sorting conditions, combined sorting

POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 193 {"query": {" match ": {" desc" : "university"}}, "sort" : [{" age ":" asc "}, {" money ":" desc "}]}Copy the code

Note: The text type cannot be sorted, the keyword type can

Remember the field types you created in Step 1.2

"username": {
	"type": "keyword"
},
"nickname": {
	"type": "text",
	"analyzer": "ik_max_word"
}
Copy the code

Username is allowed to sort, but nickname is not allowed. Nickname sort error!

So how do you use text and sort?

Let’s add another test field by clicking json below

POST /shop/_mapping HTTP/1.1
Host: 192.168.10.235:9200
Content-Type: application/json
Content-Length: 256

{
    "properties": {
        "nickname_test": {
            "type": "text",
            "analyzer": "ik_max_word",
            "fields": {
                "keyword": {
                    "type": "keyword"
                }
            }
        }
    }
}
Copy the code

This field is the attached property of the Nickname_test field

At this point, if you want to use nickname_test* to sort, you can’t write this way

{
	"nickname_test": "asc"
}
Copy the code

Instead, the nickname_test accessory attribute keyword should be used for sorting

{
	"nickname_test.keyword": "asc"
}
Copy the code

10. DSL Search – keyword highlightinghighlight

POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 157 {"query": {" match ": {" desc" : "sand monk"}}, "highlight" : {" fields ": {" desc" : {}}}}Copy the code

An EM tag is added to the search keyword in the returned data

"Highlight ": {"desc": [" Tang Sanzang is the master of <em> Sand Monk </em>"]}Copy the code

We can also customize this tag using pre_tags and post_tags

POST /shop/_search HTTP/1.1 Host: 192.168.10.235:9200 Content-Type: application/json Content-Length: 267 {"query": {" match ": {" desc" : "sand monk"}}, "highlight" : {" pre_tags: "[]" < span > ", "post_tags:" [] "< / span >", "fields" : {" desc ": {}}}}Copy the code

In the returned data, the EM label is changed to span

"Highlight" : {" desc ": [" longevity monk is < span > no < / span > master"]}Copy the code