ElasticSearch7.x – HTTP operation – Query document operation

  1. Query all documents under the index

    http://192.168.254.101:9200/shopping/_search

  2. Conditional query

    1. Add conditions to the request path: http://192.168.254.101:9200/shopping/_search?q=category:Xiaomi

    2. Add conditions to the request body: http://192.168.254.101:9200/shopping/_search

      Request body content

      {<!-- -->
          "query" :{<!-- -->
              "match":{<!-- -->
                  "category":"Xiaomi"
              }
          }
      }
      

      If the request body content is as follows, it is a full query

      {<!-- -->
          "query" :{<!-- -->
              "match_all":{<!-- -->
                  
              }
          }
      }
      

      Multiple conditions are true at the same time

      {<!-- -->
          "query" : {<!-- -->
              "bool" : {<!-- -->
                  "must" : [ // The multiple conditions of the query are arrays and must be true at the same time. must is equivalent to and & amp; & amp;
                      {<!-- -->
                          "match" : {<!-- -->
                          "category" : "Xiaomi"
                          }
                      },
                      {<!-- -->
                          //Second condition...
                      },
          // ...
                  ],
          "filter" : {<!-- --> // Filter data
                      "range" : {<!-- --> // Range limit
                          "price" : {<!-- -->
                              "gt" : 5000
                          }
                      }
                  }
              }
          }
      }
      

      Change the “must” field to “should” or ||

    3. The traditional “match” matching will automatically disassemble the keywords: for example: Xiaohua -> Xiao and Hua, the query data includes Xiaomi and Huawei

      {<!-- -->
          "query" :{<!-- -->
              "match":{<!-- -->
                  "category":"Xiaohua"
              }
          }
      }
      

      Use “match_phrase”: keywords will not be disassembled

      {<!-- -->
          "query" :{<!-- -->
              "match_phrase":{<!-- -->
                  "category":"Xiaohua"
              }
          }
      }
      
  3. Paging query

    http://192.168.254.101:9200/shopping/_search

    Request body content

    {<!-- -->
        "query" :{<!-- -->
            "match_all":{<!-- -->
                
            }
        },
        "from": 0, # From which item to start
        "size": 2, # How many pieces of data to display
        "_source" : ["title"], # Only displayed fields
    "sort" :{<!-- --> # Sort
            "price" : {<!-- -->
                "order" : "desc" / "asc"
            }
        }
    }
    
  4. Aggregation query

    http://192.168.254.101:9200/shopping/_search

    Request body content

    {<!-- -->
        "aggs" : {<!-- --> // Aggregation operation
            "price_group" : {<!-- --> // Aggregation name (custom name)
                "terms" : {<!-- --> // terms: grouping (the same is a group, count the number of each group) avg: average
                    "field" : "price" // Grouping field
                }
            }
        },
        size: 0 // After adding size: 0, only the aggregate information will be obtained, and the document content will not be obtained.
    }
    
  5. Field matching query

    multi_match is similar to match, except that it can query on multiple fields.

    Send a GET request to the ES server: http://192.168.254.101:9200/shopping/_search

    Request body content

    {<!-- -->
        "query": {<!-- -->
            "multi_match": {<!-- -->
                "query": "zhangsan",
                "fields": ["name","nickname"]
            }
        }
    }
    
  6. Keyword precise query

    Term query, precise keyword matching query, no word segmentation of query conditions.

    Send a GET request to the ES server: http://192.168.254.101:9200/shopping/_search

    Request body content

    {<!-- -->
        "query": {<!-- -->
            "term": {<!-- -->
                "name": {<!-- -->
                    "value": "zhangsan"
                }
            }
        }
    }
    
  7. Multiple keyword precise query

    The terms query is the same as the term query, but it allows you to specify multiple values to match.

    If this field contains any of the specified values, then the document meets the condition, similar to mysql’s in

    Send a GET request to the ES server: http://192.168.254.101:9200/shopping/_search

    Request body content

    {<!-- -->
        "query": {<!-- -->
            "terms": {<!-- -->
                "name": ["zhangsan","lisi"]
            }
        }
    }
    
  8. Specify query fields

    By default, Elasticsearch will return all fields stored in _source in the document in the search results.

    If we only want to get some of the fields, we can add _source filtering

    Send a GET request to the ES server: http://192.168.254.101:9200/shopping/_search

    Request body content

    {<!-- -->
        "_source": ["name","nickname"],
        "query": {<!-- -->
            "terms": {<!-- -->
                "nickname": ["zhangsan"]
            }
        }
    }
    
  9. Filter fields

    includes: to specify the fields you want to display

    excludes: to specify fields that you do not want to display

    Send a GET request to the ES server: http://192.168.254.101:9200/shopping/_search

    {<!-- -->
        "_source": {<!-- -->
            "includes": ["name","nickname"]
        },
        "query": {<!-- -->
            "terms": {<!-- -->
                "nickname": ["zhangsan"]
            }
        }
    }
    
  10. Combined query

    bool passes various other queries through must (must), must_not (must not), should (should) combined in a way

    In Postman, make a GET request to the ES server: http://192.168.254.101:9200/shopping/_search

    For example:

    {<!-- -->
        "query": {<!-- -->
            "bool": {<!-- -->
                "must": [
                    {<!-- -->
                        "match": {<!-- -->
                            "name": "zhangsan"
                        }
                    }
                ],
                "must_not": [
                    {<!-- -->
                        "match": {<!-- -->
                            "age": "40"
                        }
                    }
                ],
                "should": [
                    {<!-- -->
                        "match": {<!-- -->
                            "sex": "male"
                        }
                    }
                ]
            }
        }
    }
    
  11. range query

    The range query finds those numbers or times that fall within a specified range. range query allows the following characters

    Operator Description
    gt Greater than
    gte Greater than or equal
    lt Less than
    lte less than or equal
    {<!-- -->
        "query": {<!-- -->
            "range": {<!-- -->
                "age": {<!-- -->
                    "gte": 30,
                    "lte": 35
                }
            }
        }
    }
    
  12. fuzzy query

    Returns documents that contain words similar to the search terms.

    Edit distance is the number of changes of one character required to transform one term into another. These changes can include:

    • Change character (box → fox)

    • Delete characters (black → lack)

    • Insert character (sic → sick)

    • Transpose two adjacent characters (act → cat)

    To find similar terms, a fuzzy query creates a set of all possible variations or expansions of a search term within a specified edit distance. The query then returns an exact match for each expansion.

    Modify edit distance via fuzziness. Generally, the default value AUTO is used to generate edit distance based on the length of the term.

    {<!-- -->
        "query": {<!-- -->
            "fuzzy": {<!-- -->
                "title": {<!-- -->
                    "value": "zhangsan"
                }
            }
        }
    }
    
  13. Single field sorting

    sort allows us to sort by different fields, and specify the sorting method through order. desc descending order, asc ascending order.

    {<!-- -->
        "query": {<!-- -->
            "match": {<!-- -->
                "name":"zhangsan"
            }
        },
        "sort": [{<!-- -->
            "age": {<!-- -->
                "order":"desc"
            }
        }]
    }
    
  14. Sort by multiple fields

    Suppose we want to query using age and _score together, and the matching results are sorted first by age and then by relevance score

    {<!-- -->
        "query": {<!-- -->
            "match_all": {<!-- -->}
        },
        "sort": [
            {<!-- -->
                "age": {<!-- -->
                    "order": "desc"
                }
            },
            {<!-- -->
                "_score":{<!-- -->
                    "order": "desc"
                }
            }
        ]
    }
    
  15. Highlight query

    When performing a keyword search, the keywords in the searched content will be displayed in different colors, which is called highlighting.

    Elasticsearch can set labels and styles (highlighting) for the keyword part in the query content.

    While using match query, add a highlight attribute:

    • pre_tags: pre-tags

    • post_tags: post tags

    • fields: fields that need to be highlighted

    • title: It is declared here that the title field needs to be highlighted. You can set a unique configuration for this field later, or it can be empty.

    {<!-- -->
        "query": {<!-- -->
            "match": {<!-- -->
                "name": "zhangsan"
            }
        },
        "highlight": {<!-- -->
            "pre_tags": "<font color='red'>",
            "post_tags": "</font>",
            "fields": {<!-- -->
                "name": {<!-- -->}
            }
        }
    }
    
  16. Aggregation query

    Aggregation allows users to perform statistical analysis on es documents, similar to group by in relational databases, and of course there are many

    Many other aggregations, such as taking the maximum value, average value, etc.

    • Get the maximum value of a field max

      {<!-- -->
          "aggs":{<!-- -->
              "max_age":{<!-- -->
                  "max":{<!-- -->"field":"age"}
              }
          },
          "size":0
      }
      
    • Get the minimum value of a field min

      {<!-- -->
          "aggs":{<!-- -->
              "min_age":{<!-- -->
                  "min":{<!-- -->"field":"age"}
              }
          },
          "size":0
      }
      
    • Sum a field sum

      {<!-- -->
          "aggs":{<!-- -->
              "sum_age":{<!-- -->
                  "sum":{<!-- -->"field":"age"}
              }
          },
          "size":0
      }
      
    • average a field avg

    • Deduplicate the value of a field and then get the total number

      {<!-- -->
          "aggs":{<!-- -->
              "distinct_age":{<!-- -->
                  "cardinality":{<!-- -->"field":"age"}
              }
          },
          "size":0
      }
      
    • State aggregation

      stats aggregation, returns count, max, min, avg and sum five indicators for a certain field at once

      {<!-- -->
          "aggs":{<!-- -->
              "stats_age":{<!-- -->
                  "stats":{<!-- -->"field":"age"}
              }
          },
          "size":0
      }
      
    • Bucket aggregation query

      Bucket sum is equivalent to the group by statement in SQL

      terms aggregation, grouping statistics

      {<!-- -->
          "aggs":{<!-- -->
              "age_groupby":{<!-- -->
                  "terms":{<!-- -->"field":"age"}
              }
          },
          "size":0
      }
      

      Aggregate under terms grouping

      {<!-- -->
          "aggs":{<!-- -->
              "age_groupby":{<!-- -->
                  "terms":{<!-- -->"field":"age"}
              }
          },
          "size":0
      }