← Back to list

Elasticsearch Term-Level Queries

Term level queries are used for searching in structured data with exact values. They don’t analyze text (no tokenization, lowercasing…

Kasun Jayashan · 2026-03-29 05:16 · 0 claps · 1.8 min read
#termquery #rangequery #exact-match #wildcardquery #regexpquery
Open on Medium ↗

Elasticsearch Term-Level Queries

Term level queries are used for searching in structured data with exact values. They don’t analyze text (no tokenization, lowercasing, etc.). They are perfect for fields like keyword, numeric, date, boolean, etc. There are few types of term-level queries as below

exist Query

This query will find the documents where the given field exists and has a non-null value.

GET /users/_search
{
  "query": {
    "exists": {
      "field": "mobile_number"
    }
  }
}

Above query will find users who have a mobile number

fuzzy Query

This query match the terms that are similar to the given term. This search is based on the edit distance and we can define the edit distance using fuzziness parameter.

GET products/_search
{
  "query": {
    "fuzzy": {
      "name.keyword": {
        "value": "iphon",
        "fuzziness": 1
      }
    }
  }
}

Above query will find the values similar to “iphon” like “iphone” or “iphonr”, with one character difference allowed.

ids Query

This query will find the documents based on their document ids.

GET employees/_search
{
  "query": {
    "ids": {
      "values": ["1", "3", "7"]
    }
  }
}

Above query will return the documents whose internal elasticsearch IDs are 1,3, or 7.

prefix Query

This query will return the documents where the field value start with a given prefix.

GET customers/_search
{
  "query": {
    "prefix": {
      "customer_name": "KAS"
    }
  }
}

Above query will return the customer name start with “KAS” ex : KASUN

range Query

This query will return the documents with field values within a given range.

GET employees/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 25,
        "lte": 40
      }
    }
  }
}

Above query will return the documents where the “age” field contains a term between 25 and 40.

parameters for <field> gt- grater than gte- grater than or equal to lt- less than lte- less than or equal to format- data format used to convert data values in the query relation- indicate how the rabge query matches values for “range” field time_zone- used to convert “data” values in the query to UTC boost- floating point number used to decrease of increase the relevent score of a query

Range queries on text or keyword fields will not be executed if “search.allow_expensive_queries” is set to false.

wildcard Query

Wildcard query allows simple pattern matching using special characters

GET employees/_search
{
  "query": {
    "wildcard": {
      "username.keyword": "ka*un"
    }
  }
}

Above query will return below matches

  • kasun
  • karun
  • kaaun

Regexp Query

This query is more flexible than the wild card query. this allows full regular matching.

GET employees/_search
{
  "query": {
    "regexp": {
      "username.keyword": "k.*n"
    }
  }
}

Above query will return below matches

  • kasun
  • kiran
  • kevin

Common regex patterns as below

(.) Any single character (.*) Any sequence of characters [abc] Matches a, b, or c [a-z] Matches any lowercase letter


메타데이터
post_id
6f1a70ec77fc
slug
elasticsearch-term-level-queries-6f1a70ec77fc
url
https://medium.com/@jayashankk/elasticsearch-term-level-queries-6f1a70ec77fc
canonical_url
https://medium.com/@jayashankk/elasticsearch-term-level-queries-6f1a70ec77fc
author_url
https://medium.com/@jayashankk
status
ok
fetched_at
2026-07-31 05:46:17