This is the 15th day of my participation in the August Text Challenge.More challenges in August

Data preparation

Operate on the interface provided by Kibana.

POST /school/student/_bulk
{ "index": { "_id": 1 }}
{ "name" : "liubei", "age" : 20 , "sex": "boy", "birth": "1996-01-02" , "about": "i like diaocan he girl" }
{ "index": { "_id": 2 }}
{ "name" : "guanyu", "age" : 21 , "sex": "boy", "birth": "1995-01-02" , "about": "i like diaocan" }
{ "index": { "_id": 3 }}
{ "name" : "zhangfei", "age" : 18 , "sex": "boy", "birth": "1998-01-02" , "about": "i like travel" }
{ "index": { "_id": 4 }}
{ "name" : "diaocan", "age" : 20 , "sex": "girl", "birth": "1996-01-02" , "about": "i like travel and sport" }
{ "index": { "_id": 5 }}
{ "name" : "panjinlian", "age" : 25 , "sex": "girl", "birth": "1991-01-02" , "about": "i like travel and wusong" }
{ "index": { "_id": 6 }}
{ "name" : "caocao", "age" : 30 , "sex": "boy", "birth": "1988-01-02" , "about": "i like xiaoqiao" }
{ "index": { "_id": 7 }}
{ "name" : "zhaoyun", "age" : 31 , "sex": "boy", "birth": "1997-01-02" , "about": "i like travel and music" }
{ "index": { "_id": 8 }}
{ "name" : "xiaoqiao", "age" : 18 , "sex": "girl", "birth": "1998-01-02" , "about": "i like caocao" }
{ "index": { "_id": 9 }}
{ "name" : "daqiao", "age" : 20 , "sex": "girl", "birth": "1996-01-02" , "about": "i like travel and history" }
Copy the code

1, use match_all to query

GET /school/student/_search? pretty { "query": { "match_all": {} } }Copy the code

Problem: After matching with match_all, all the data will be retrieved, but often the real business need is not to find all the data, but to retrieve what they want; And for the ES cluster, directly retrieve all the data, it is easy to cause GC phenomenon. So, we have to learn how to retrieve data efficiently, right

2. Query information based on key fields

GET /school/student/_search? pretty { "query": { "match": {"about": "travel"} } }Copy the code

If you want to query the user who likes traveling, and can not be a boy, what to do?

This approach is incorrect because multiple field values cannot appear under a match

[match] query doesn’t support multiple fields

3, bool compound query

Bool can be used to contain multiple query statements.

Bool combination contains: must, must_not or should, should means or

Example: Query non-male people who like to travel

GET /school/student/_search? pretty { "query": { "bool": { "must": { "match": {"about": "travel"}}, "must_not": {"match": {"sex": "boy"}} } } }Copy the code

4, bool should

“Should” means dispensable (show if “should” matches, not show otherwise)

Example: query for those who like to travel, display if there are men, otherwise not

GET /school/student/_search? pretty { "query": { "bool": { "must": { "match": {"about": "travel"}}, "should": {"match": {"sex": "boy"}} } } }Copy the code

5. Term matching

Use term for exact matching

Such as numbers, dates, Boolean values or not_analyzed strings (unanalyzed text data types)

grammar

{ "term": { "age": 20 }}
{ "term": { "date": "2018-04-01" }}
{ "term": { "sex"Boy: ""}} {"term": { "about": "trivel" }}
Copy the code

6. Use terms to match multiple values

GET /school/student/_search? pretty { "query": { "bool": { "must": { "terms": {"about": ["travel","history"]}} } } }Copy the code

Term is primarily used for precise filtering

Like, “I love you.”

Matches under match can be parsers that contain: I, love, you, I love, and so on

“I love you.”

7, Range filter

Range filtering allows us to find some data in a specified Range:

Operating range meaning
gt Is greater than
gae Greater than or equal to
lt Less than
lte Less than or equal to

Example: Find students who are older than 20 and younger than or equal to 25

GET /school/student/_search? pretty {"query": {
   "range": {
    "age": {"gt": 20."lte": 25}}}}Copy the code

8, exists and missing filter

The exists and missing filters find out whether a field is present or absent in a document

Example: Find a document that contains age in the field

GET /school/student/_search? pretty {"query": {
   "exists": {
    "field": "age"}}}Copy the code

9. Bool multi-conditional filtering

Bool can also be used to filter multi-line conditions like match:

The keyword meaning The equivalent of
must An exact match of multiple query criteria and
must_not Reverse matching of multiple query criteria not
should At least one query condition is matched or

Example: Filter out students older than 20 and younger than 30 whose About field contains travel

GET /school/student/_search? pretty { "query": { "bool": { "must": [ {"term": { "about": { "value": "travel" } }},{"range": { "age": { "gte": 20, "lte": 30 } }} ] } } }Copy the code

10. Merge query and filter criteria

Usually complex query statements, we also need to cooperate with the filter statement to achieve caching, with the filter statement can be achieved

Example: Find documents that like to travel and are 20 years old

GET /school/student/_search? pretty {"query": {
   "bool": {
     "must": {"match": {"about": "travel"}},     
     "filter": [{"term": {"age": 20}}]}}}Copy the code