Search - Leaf Queries Flashcards

1
Q

what is

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Term

A

Designed for exact matches on values, usually on structured or keyword data.
Example: term, terms, range.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Full-Text Queries

A

Used for analyzing text fields, performing text analysis, tokenization, and matching tokens.
match, match_phrase, multi_match.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Span Queries

A

Specialized queries designed for exact matches with positional constraints in text
span_term, span_near, span_or, span_not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Other Queries

A

Includes queries for handling specific types or conditions.
exists, prefix, wildcard, regexp, fuzzy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Term Query

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Terms Query

A

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”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Range Query

A

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”
}
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Match Query

A

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”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Match Phrase Query

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Prefix Query

A

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”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Wildcard Query

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Regexp Query

A

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”).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Fuzzy Query

A

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”).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Exists Query

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Span Queries

A

Span queries allow for matching terms with specific positional constraints. They’re especially useful for precise text searches, such as finding words within a certain distance from each other.

{
“span_near”: {
“clauses”: [
{ “span_term”: { “description”: “Elasticsearch” } },
{ “span_term”: { “description”: “tutorial” } }
],
“slop”: 1,
“in_order”: true
}
}

Matches documents where “Elasticsearch” and “tutorial” appear in the description field within one position of each other, in the specified order.