Elasticsearch 7.15 query term query, terms query and terms set query of DSL

Posted by gnathan87 on Sat, 18 Dec 2021 21:02:48 +0100

term query

introduce

Returns the document that contains the exact value in the field provided.

You can use term queries to find documents based on precise values such as price, product ID, or user name.

Note: avoid using term queries on text fields. No results will be found

For example:

GET /_search
{
  "query": {
    "term": {
      "user.id": {
        "value": "kimchy",
        "boost": 1.0
      }
    }
  }
}

Top level parameter of term

< field > (required, object) the field name you want to query

Secondary parameter of field

Parameter namedescribe
value(Required, string) the value you want to find in the provided < field >. To return a document, the parameter must exactly match the field value, including spaces and case.
boost(Optional, float) a floating-point number used to reduce or improve the query correlation score. The default is 1.0.
case_insensitive [7.10.0](Optional, Boolean) when set to true, the value is allowed to match the ASCII case insensitive of the index field value. The default value is false, which means that the matching case depends on the mapping of the underlying field.

terms query

introduce

Returns a document that contains one or more exact values in the provided fields.

The terms query is the same as the term query, except that you can search for multiple values.

For example, the following query returns a document with an id of kimchy or elkbee

GET /_search
{
  "query": {
    "terms": {
      "user.id": [ "kimchy", "elkbee" ],
      "boost": 1.0
    }
  }
}

Top level parameters for terms

Parameter namedescribe
<field>(Optional, object) an array of fields you want to search By default, Elasticsearch limits the size of this parameter array of terms query to 65536 at most. You can use index max_ terms_ Count this setting to change.
boost(Optional, float) a floating-point number used to reduce or improve the query correlation score. The default is 1.0.

terms set query

introduce

Returns the document that contains the least number of exact values in the field provided.

terms_ The set query is the same as the terms query, except that you can define the number of matching terms required to return the document. For example:

  • One is called programming_ The languages field contains a list of known programming languages, such as c + +, java, or php for job candidates. You can use terms_ The set query returns documents that match at least two of these languages.
  • A permission field named permissions contains a list of possible user permissions for the application. You can use terms_ The set query returns documents that match these permission subsets.

Top level parameters of terms set

< field > (required, object) the field array you want to query

Secondary parameter of field

Parameter namedescribe
terms(required, string array) the array of terms you want to find in the provided < field >. To return a document, the number of entries (the size of the array passed in) must exactly match the number of field values, including spaces and uppercase.
minimum_should_match_field(Optional, string) field name. The filled field should be a number, including the number of matching conditions required to return the document.
minimum_should_match_script(Optional, string) a custom script containing the number of matching criteria required to return the document.

For example:

#1. There are the following indexes:
# required_matches field, a long field. This field contains the number of matching entries required to return the document.
PUT /job-candidates
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      },
      "programming_languages": {
        "type": "keyword"
      },
      "required_matches": {
        "type": "long"
      }
    }
  }
}

#2. Insert a document. The document must match the two terms "C + +" and "Java". Only when they match can it be returned.
# contain? refresh parameter so that the document can be searched immediately.
PUT /job-candidates/_doc/1?refresh
{
  "name": "Jane Smith",
  "programming_languages": [ "c++", "java" ],
  "required_matches": 2
}
#This document must match the three terms "C + +," Java "and" PHP ", which are all matched before it is returned.
PUT /job-candidates/_doc/2?refresh
{
  "name": "Jane Smith",
  "programming_languages": [ "c++", "java", "php" ],
  "required_matches": 3
}

#This document will be returned as long as it matches any of the three terms "C + +," Java "and" JavaScript ".
PUT /job-candidates/_doc/3?refresh
{
  "name": "Jane Smith",
  "programming_languages": [ "c++", "java", "javascript" ],
  "required_matches": 1
}

#3. For example, the following query will_ Documents with IDS 1, 2 and 3 are returned.
GET /job-candidates/_search
{
  "query": {
    "terms_set": {
      "programming_languages": {
        "terms": [ "c++", "java", "php" ],
        "minimum_should_match_field": "required_matches"
      }
    }
  }
}

#4. For example, the following query only returns_ Documents with IDS 1 and 3.
GET /job-candidates/_search
{
  "query": {
    "terms_set": {
      "programming_languages": {
        "terms": [ "c++", "java","javascript"],
        "minimum_should_match_field": "required_matches"
      }
    }
  }
}

Topics: ElasticSearch search engine lucene