Search - Leaf Queries Flashcards
what is
Leaf queries are a type of query that operate on individual fields within a document. These queries do not contain other queries within them, meaning they stand alone and match specific values, terms, text, or conditions directly on the field.
Term
Designed for exact matches on values, usually on structured or keyword data.
Example: term, terms, range.
Full-Text Queries
Used for analyzing text fields, performing text analysis, tokenization, and matching tokens.
match, match_phrase, multi_match.
Span Queries
Specialized queries designed for exact matches with positional constraints in text
span_term, span_near, span_or, span_not
Other Queries
Includes queries for handling specific types or conditions.
exists, prefix, wildcard, regexp, fuzzy.
Term Query
The term query is used to find exact matches on keyword fields or fields that are not analyzed (such as IDs or tags). This query is case-sensitive and does not perform any text analysis.
{
“term”: {
“status”: “active”
}
}
Returns documents where the status field has the exact term “active”.
The term query is case-sensitive. For instance, searching for “Active” will not match “active” unless the exact casing matches in the indexed field.
Term Query with Numbers
{
“query”: {
“term”: {
“age”: 30
}
}
}
So there is no analysis of the field during search, like removal of stop words etc. Straight away sent to the search against the index without any change
Terms Query
The terms query works similarly to the term query, but allows for matching any one of multiple specified values.
{
“terms”: {
“status”: [“active”, “pending”]
}
}
Returns documents where the status field is either “active” or “pending”.
Range Query
The range query is used to search for values within a specific range, typically for numbers, dates, or other comparable fields.
{
“range”: {
“created_date”: {
“gte”: “2023-01-01”,
“lte”: “2023-12-31”
}
}
}
Match Query
The match query is a full-text query used to search within text fields. It breaks down the search text into terms and matches them against tokens in the indexed field.
{
“match”: {
“description”: “Elasticsearch tutorial”
}
}
Matches documents where the description field contains the analyzed terms from “Elasticsearch tutorial”.
Match Phrase Query
The match_phrase query looks for an exact sequence of terms in the specified order. This query is useful for phrase searches in text fields.
{
“match_phrase”: {
“description”: “Elasticsearch tutorial”
}
}
Finds documents where “Elasticsearch tutorial” appears as an exact phrase in the description field.
Prefix Query
The prefix query matches documents where a field starts with a specified prefix. This is useful for autocompletion features or finding documents with similar prefixes.
{
“prefix”: {
“title”: “Elast”
}
}
Matches documents where the title field starts with “Elast”.
Wildcard Query
The wildcard query enables partial and pattern-based matches within text fields. It allows the use of * to match any sequence of characters and ? to match a single character.
{
“wildcard”: {
“title”: “elastic*”
}
}
Matches documents where the title field starts with “elastic” followed by any sequence of characters.
Regexp Query
The regexp query allows for complex regular expression matching within fields. It’s more flexible but slower than wildcard queries.
{
“regexp”: {
“title”: “elastic.*search”
}
}
Finds documents where the title field matches the pattern “elastic.*search” (like “elasticsearch”).
Fuzzy Query
The fuzzy query performs approximate matching using a specified level of “fuzziness,” which allows for minor variations like typos or spelling errors.
{
“fuzzy”: {
“title”: {
“value”: “Elastsearch”,
“fuzziness”: “AUTO”
}
}
}
Finds documents with terms similar to “Elastsearch”, accounting for spelling variations (like “Elasticsearch”).
Exists Query
The exists query is used to find documents where a specific field is present (non-null).
{
“exists”: {
“field”: “description”
}
}
Returns documents where the description field exists.