Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Term-Level Queries in Elasticsearch

Introduction to Term-Level Queries

In Elasticsearch, Term-Level Queries are used to find documents that contain exact terms in a specified field. Unlike Full-Text Queries, which analyze the query text to determine the search terms, Term-Level Queries operate on exact terms and are typically used for structured data.

Common Term-Level Queries

Some common term-level queries include:

  • Term Query: Finds documents that contain the exact term specified.
  • Terms Query: Finds documents that contain any of the exact terms specified.
  • Range Query: Finds documents where field values are within a specified range.
  • Exists Query: Finds documents that contain a specified field.
  • Prefix Query: Finds documents that contain terms with a specified prefix.
  • Wildcard Query: Finds documents that contain terms matching a pattern.
  • Regexp Query: Finds documents that contain terms matching a regular expression.
  • Fuzzy Query: Finds documents that contain terms similar to the specified term.

Term Query

The Term Query finds documents that contain the exact term specified in the inverted index. It is case-sensitive and does not analyze the input.

{
    "query": {
        "term": {
            "status": {
                "value": "active"
            }
        }
    }
}

This query will find documents where the status field exactly matches the term active.

Terms Query

The Terms Query finds documents that match any of the exact terms specified in the query.

{
    "query": {
        "terms": {
            "status": ["active", "pending"]
        }
    }
}

This query will find documents where the status field matches either active or pending.

Range Query

The Range Query finds documents where the field values are within a specified range.

{
    "query": {
        "range": {
            "age": {
                "gte": 30,
                "lte": 40
            }
        }
    }
}

This query will find documents where the age field is between 30 and 40, inclusive.

Exists Query

The Exists Query finds documents that contain a specified field.

{
    "query": {
        "exists": {
            "field": "email"
        }
    }
}

This query will find documents that have the email field.

Prefix Query

The Prefix Query finds documents that contain terms with a specified prefix.

{
    "query": {
        "prefix": {
            "username": {
                "value": "user"
            }
        }
    }
}

This query will find documents where the username field starts with the prefix user.

Wildcard Query

The Wildcard Query finds documents that contain terms matching a specified pattern. It supports * to match any character sequence and ? to match any single character.

{
    "query": {
        "wildcard": {
            "username": {
                "value": "user*"
            }
        }
    }
}

This query will find documents where the username field matches the pattern user*.

Regexp Query

The Regexp Query finds documents that contain terms matching a specified regular expression.

{
    "query": {
        "regexp": {
            "username": {
                "value": "user[0-9]+"
            }
        }
    }
}

This query will find documents where the username field matches the regular expression user[0-9]+.

Fuzzy Query

The Fuzzy Query finds documents that contain terms similar to the specified term, using Levenshtein Edit Distance to match terms.

{
    "query": {
        "fuzzy": {
            "username": {
                "value": "user",
                "fuzziness": "AUTO"
            }
        }
    }
}

This query will find documents where the username field is similar to the term user.