Link Search Menu Expand Document Documentation Menu

Index prefixes

The index_prefixes mapping parameter instructs the engine to generate additional index entries for the beginning segments of terms in a text field. When enabled, it builds a prefix index based on configurable minimum and maximum character lengths. This can significantly improve the performance of prefix queries, such as autocomplete or search as you type, by allowing these queries to quickly match the pre-indexed term prefixes.

By default, prefix indexing is not performed, maintaining minimal index size and fast indexing operations. However, if your application benefits from rapid prefix matching, enabling this parameter can provide a marked improvement in query efficiency.

Index prefixes configuration

You can pass the following configuration parameters to the index_prefixes mapping parameter:

  • min_chars: The minimum length of the prefix that needs to be indexed. Minimum is 0. Default is 2.
  • max_chars: The maximum length of the prefix that needs to be indexed. Maximum is 20. Default is 5.

Enabling index prefixes on a field

The following request creates an index named products with the name field configured to build a prefix index with a length of between 2 and 10 characters:

PUT /products
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "index_prefixes": {
          "min_chars": 2,
          "max_chars": 10
        }
      }
    }
  }
}

Index a document using the following request:

PUT /products/_doc/1
{
  "name": "Ultra HD Television"
}

The following search request shows a prefix query that searches for documents in which the name field starts with ul:

POST /products/_search
{
  "query": {
    "prefix": {
      "name": "ul"
    }
  }
}

The response contains the matching document:

{
  ...
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "products",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "Ultra HD Television"
        }
      }
    ]
  }
}

Using default parameters with index prefixes

The following request creates an index named products_default using index_prefixes with the default parameters:

PUT /products_default
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "index_prefixes": {}
      }
    }
  }
}

350 characters left

Have a question? .

Want to contribute? or .