Skip to content

adding index prefix mapping parameter documentation #9541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions _field-types/mapping-parameters/index-prefixes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
layout: default
title: Index prefixes
parent: Mapping parameters
grand_parent: Mapping and field types
nav_order: 90
has_children: false
has_toc: false
---

# 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, keeping the index size minimal and indexing operations fast. However, if your application benefits from rapid prefix matching, enabling this parameter can provide a marked improvement in query efficiency.

## Index prefixes configuration

Following configuration can be passed to `index_prefixes` mapping parameter:

- `min_chars`: Minimum length of the prefix that needs to be indexed. Minimum is `0`. Default is `2`.
- `max_chars`: 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 length between `2` and `10` characters:

```json
PUT /products
{
"mappings": {
"properties": {
"name": {
"type": "text",
"index_prefixes": {
"min_chars": 2,
"max_chars": 10
}
}
}
}
}
```
{% include copy-curl.html %}

Index a document using the following command:

```json
PUT /products/_doc/1
{
"name": "Ultra HD Television"
}
```
{% include copy-curl.html %}

The following search request demonstrates a prefix query that looks for documents where the `name` field starts with `ul`:

```json
POST /products/_search
{
"query": {
"prefix": {
"name": "ul"
}
}
}
```
{% include copy-curl.html %}

Expected result:

```json
{
...
"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 command creates an index named `products_default` using `index_prefixes` with default parameters:

```json
PUT /products_default
{
"mappings": {
"properties": {
"name": {
"type": "text",
"index_prefixes": {}
}
}
}
}
```
{% include copy-curl.html %}