ElasticSearch ~ query operation ~ (simple query, batch query, matching query, fuzzy query, accurate query, range query, wildcard query, must query, should query, filter query)

1, Simple query

1, Query all results

GET /student_info/_search
{
  "query": {"match_all": {}}
}

2, Query by criteria

GET /student_info/_search
{
  "query":{
    "match":{
      "name":"Zhang San"
    }
  }
}

3, Sort

GET /student_info/_search
{
  "query":{
    "match":{
      "name":"Zhang San"
    }
  },
  "sort":[{
    "age":"desc"
  }]
}

4, Specify the result fields returned by the query

GET /student_info/_search
{
  "query":{"match_all": {}},
  "_source": ["name","age"]
}

2, Batch query

1, Multi ID query

GET /student_info/_search
{
  "query":{
    "ids":{
      "values":[11001,11002,11003]
    }
  }
}

2, Single index batch query

POST /student_info/_mget
{
  "ids":["11001","11002","11003"]
}

3, Cross index batch query

GET /_mget
{
  "docs":[
      {
        "_index":"student_info",
        "_id":"11001"
      },
      {
        "_index":"teacher",
        "_id":"1001"
      }
    ]
}

4, Cross index batch query

GET /_msearch
{"index":"student_info"}
{"query":{"match_all":{}},"from":0,"size":4}
{"index":"teacher"}
{"query":{"match_all":{}}}

3, Matching query

1, Keyword segmentation query

First segment the field values in match, and then query

"Professional": "computer technology": the word segmentation results are "computer" and "technology". Query the records of profession al including "computer" and "technology"

GET /student_info/_search
{
  "query":{
    "match":{
      "profession":"computer technology"
    }
  }
}
GET /student_info/_search
{
  "query":{
    "match":{
      "profession":"Computer Hotel"
    }
  }
}

2, Association query

"Professional": "computer technology": the word segmentation results are "computer" and "technology". The query condition "operator": "and" must meet all word segmentation results.

GET /student_info/_search
{
  "query":{
    "match":{
      "profession": {
        "query":"computer technology",
        "operator": "and"
      }
    }
  }
}

"Professional": "computer technology": the word segmentation result is "computer" and "technology", and the query condition "operator": "or", any one of which is satisfied.

GET /student_info/_search
{
  "query": {
    "match":{
      "profession": {
        "query": "Computer Hotel",
        "operator": "or"
      }
    }
  }
}

3, Multi field query

The search content "my computer technology" will be disassembled into "I", "computer", "technology" matching field, and the result of the disassembled words contained in the profession al or desc field

GET /student_info/_search
{
  "query":{
    "multi_match": {
      "query": "My computer technology",
      "fields": ["profession","desc"]
    }
  }
}

4, Phrase query

match_phrase search requires that all participles must appear in the document at the same time, and the position must be close and consistent.

GET /student_info/_search
{
  "query": {
    "match_phrase": {
      "profession": "Computer Department"
    }
  }
}

5, Highlight search

  • Highlight highlight find

  • pre_tags tag prefix

  • post_tags tag suffix

  • Fields. Multiple fields are supported

  • Note: if prefix and suffix are not declared, < EM > is used by default</em>

GET /student_info/_search
{
  "query":{
    "match":{
      "name":"Zhang San"
    }
  },
  "highlight": {
    "pre_tags":"<p class = \"text_high_light\">",
    "post_tags": "</p>",
    "fields": {
      "name":{}
    }
  }
}

6, Prefix matching

GET /student_info/_search
{
  "query": {
    "match_phrase_prefix": {
      "name":"Small"
    }
  }
}

4, Fuzzy query

GET /student_info/_search
{
  "query":{
    "fuzzy":{
      "name":"Zhang"
    }
  }
}

5, Precise query

term is a keyword query, and the parameter type is generally keyword. For accurate query, the value of the query is not divided into words, and it is directly entered into the inverted index to match.

  • term exact search (single)
  • terms exact search (multiple)
GET /student_info/_search
{
  "query":{
    "term":{
      "name.keyword": "Zhang San"
    }
  }
}

terms means multiple conditions are juxtaposed, and the query content is covered with braces [], which is similar to the in method in MySql

GET /student_info/_search
{
  "query": {
    "terms":{
      "age":[19,20,21,22]
    }
  }
}

6, Range query

1, range

Greater than - gt, less than - lt, greater than or equal to - gte, less than or equal to - lte

  • Digital range
GET /student_info/_search
{
  "query": {
    "range":{
      "age":{
        "gte":19,
        "lte":21
      }
    }
  }
}
  • time frame
GET /student_info/_search
{
  "query": {
    "range":{
      "birthday": {
        "gte": "2001-06-15",
        "lte": "2001-09-20"
      }
    }
  }
}

2, from... to

  • Range query contains boundaries
GET /student_info/_search
{
  "query":{
    "range":{
      "age":{
        "from":19,
        "to":21
      }
    }
  }
}
  • Range query does not contain boundaries
GET /student_info/_search
{
  "query": {
    "range":{
      "age":{
        "from":19,
        "to":21,
        "include_lower":false,
        "include_upper":false
      }
    }
  }
}

7, Wildcard query

Note:? Used to match any character, * used to match zero or more characters, mainly used for - English retrieval

GET /student_info/_search
{
  "query":{
    "wildcard":{
      "english_name": "xiaoxiao*"
    }
  }
}
GET /student_info/_search
{
  "query":{
    "wildcard": {
      "english_name": "li?i"
    }
  }
}

8, must query

  • Many conditions of must must must be met

  • must is equivalent to AND in MySQL condition

GET /student_info/_search
{
  "query":{
    "bool":{
      "must":[{
        "match":{
          "name":"Small"
        }
      },
      {
        "range":{
          "age":{
            "gt":19,
            "lte":22
          }
        }
      }
      ]
    }
  }
}

9, should query

  • It is OK to meet at least one of the conditions of should
  • should is equivalent to OR in MySQL condition
GET /student_info/_search
{
  "query":{
    "bool":{
      "should":[{
        "match":{
          "name":"Small"
        }
      },
      {
        "range":{
          "age":{
            "gt":19,
            "lte":22
          }
        }
      }]
    }
  }
}

10, Filter query

GET /student_info/_search
{
  "query":{
    "bool":{
      "should":[
        {
          "match":{
            "name":"Small"
          }
        }],
        "filter":{
          "range":{
            "age":{
              "gt":21,
              "lte":22
            }
          }
        }
    }
  }
}

Keywords: ElasticSearch lucene elastic

Added by jonki on Fri, 25 Feb 2022 15:23:29 +0200