term级别查询(term-level queries)
你可以使用term级别查询在结构化的数据中根据精确的值进行查找,例如日期范围、IP地址、价格、产品ID。term级别查询不会对查询语句进行分词,而是精确的去匹配结构化数据中的字段值。
1、 Exists query
Returns documents that contain an indexed value for a field.
An indexed value may not exist for a document’s field due to a variety of reasons:
- The field in the source JSON is null or []
- The field has "index" : false set in the mapping
- The length of the field value exceeded an ignore_above setting in the mapping
- The field value was malformed and ignore_malformed was defined in the mapping
Example:
GET /_search { "query": { "exists": { "field": "user" } } }
While a field is deemed non-existent if the JSON value is null
or []
, these values will indicate the field does exist:
- Empty strings, such as
""
or"-"
- Arrays containing
null
and another value, such as[null, "foo"]
- A custom
null-value
, defined in field mapping
Find documents missing indexed values
To find documents that are missing an indexed value for a field, use the must_not
with the exists
query.
Exable:
GET /_search { "query": { "bool": { "must_not": { "exists": { "field": "user.id" } } } } }
2、Fuzzy query
Returns documents that contain terms similar to the search term, as measured by a Levenshtein edit distance.
An edit distance is the number of one-character changes needed to turn one term into another. These changes can include:
- Changing a character (box → fox)
- Removing a character (black → lack)
- Inserting a character (sic → sick)
- Transposing two adjacent characters (act → cat)
To find similar terms, the fuzzy query creates a set of all possible variations, or expansions, of the search term within a specified edit distance. The query then returns exact matches for each expansion.
Example:
GET /_search { "query": { "fuzzy": { "user.id": { "value": "ki" } } } }
Example:
GET /_search { "query": { "fuzzy": { "user.id": { "value": "ki", "fuzziness": "AUTO", "max_expansions": 50, "prefix_length": 0, "transpositions": true, "rewrite": "constant_score" } } } }
Parameters
<field>
(Required, object) Field you wish to search.
value
(Required, string) Term you wish to find in the provided <field>.
fuzziness
(Optional, string) Maximum edit distance allowed for matching.
AUTO
Generates an edit distance based on the length of the term. Low and high distance arguments may be optionally provided AUTO:[low],[high]. If not specified, the default values are 3 and 6, equivalent to AUTO:3,6 that make for lengths:
0..2 Must match exactly
3..5 One edit allowed
5+ Two edits allowed
AUTO should generally be the preferred value for fuzziness.
max_expansions
(Optional, integer) Maximum number of variations created. Defaults to 50.
prefix_length
(Optional, integer) Number of beginning characters left unchanged when creating expansions. Defaults to 0.
transpositions
(Optional, boolean) Indicates whether edits include transpositions of two adjacent characters (ab → ba). Defaults to true.
rewrite
(Optional, string) Method used to rewrite the query.
3、IDS
Returns documents based on their IDs. This query uses document IDs stored in the _id
field.
Example
GET /_search { "query": { "ids" : { "values" : ["1", "4", "100"] } } }
4、Prefix query
Returns documents that contain a specific prefix in a provided field.
Example:
GET /_search { "query": { "prefix": { "user.id": { "value": "ki" } } } }
网友评论