Specify an analyzer
editSpecify an analyzer
editElasticsearch offers a variety of ways to specify built-in or custom analyzers:
-
By
text
field, index, or query - For index or search time
Keep it simple
The flexibility to specify analyzers at different levels and for different times is great… but only when it’s needed.
In most cases, a simple approach works best: Specify an analyzer for each
text
field, as outlined in Specify the analyzer for a field.
This approach works well with Elasticsearch’s default behavior, letting you use the same analyzer for indexing and search. It also lets you quickly see which analyzer applies to which field using the get mapping API.
If you don’t typically create mappings for your indices, you can use index templates to achieve a similar effect.
How Elasticsearch determines the index analyzer
editElasticsearch determines which index analyzer to use by checking the following parameters in order:
-
The
analyzer
mapping parameter for the field. See Specify the analyzer for a field. -
The
analysis.analyzer.default
index setting. See Specify the default analyzer for an index.
If none of these parameters are specified, the
standard
analyzer is used.
Specify the analyzer for a field
editWhen mapping an index, you can use the analyzer
mapping parameter
to specify an analyzer for each text
field.
The following create index API request sets the
whitespace
analyzer as the analyzer for the title
field.
resp = client.indices.create( index="my-index-000001", mappings={ "properties": { "title": { "type": "text", "analyzer": "whitespace" } } }, ) print(resp)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { properties: { title: { type: 'text', analyzer: 'whitespace' } } } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { properties: { title: { type: "text", analyzer: "whitespace", }, }, }, }); console.log(response);
PUT my-index-000001 { "mappings": { "properties": { "title": { "type": "text", "analyzer": "whitespace" } } } }
Specify the default analyzer for an index
editIn addition to a field-level analyzer, you can set a fallback analyzer for
using the analysis.analyzer.default
setting.
The following create index API request sets the
simple
analyzer as the fallback analyzer for my-index-000001
.
resp = client.indices.create( index="my-index-000001", settings={ "analysis": { "analyzer": { "default": { "type": "simple" } } } }, ) print(resp)
response = client.indices.create( index: 'my-index-000001', body: { settings: { analysis: { analyzer: { default: { type: 'simple' } } } } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", settings: { analysis: { analyzer: { default: { type: "simple", }, }, }, }, }); console.log(response);
PUT my-index-000001 { "settings": { "analysis": { "analyzer": { "default": { "type": "simple" } } } } }
How Elasticsearch determines the search analyzer
editIn most cases, specifying a different search analyzer is unnecessary. Doing so could negatively impact relevancy and result in unexpected search results.
If you choose to specify a separate search analyzer, we recommend you thoroughly test your analysis configuration before deploying in production.
At search time, Elasticsearch determines which analyzer to use by checking the following parameters in order:
-
The
analyzer
parameter in the search query. See Specify the search analyzer for a query. -
The
search_analyzer
mapping parameter for the field. See Specify the search analyzer for a field. -
The
analysis.analyzer.default_search
index setting. See Specify the default search analyzer for an index. -
The
analyzer
mapping parameter for the field. See Specify the analyzer for a field.
If none of these parameters are specified, the
standard
analyzer is used.
Specify the search analyzer for a query
editWhen writing a full-text query, you can use the analyzer
parameter to specify a search analyzer. If provided, this overrides any other
search analyzers.
The following search API request sets the stop
analyzer as
the search analyzer for a match
query.
resp = client.search( index="my-index-000001", query={ "match": { "message": { "query": "Quick foxes", "analyzer": "stop" } } }, ) print(resp)
response = client.search( index: 'my-index-000001', body: { query: { match: { message: { query: 'Quick foxes', analyzer: 'stop' } } } } ) puts response
const response = await client.search({ index: "my-index-000001", query: { match: { message: { query: "Quick foxes", analyzer: "stop", }, }, }, }); console.log(response);
GET my-index-000001/_search { "query": { "match": { "message": { "query": "Quick foxes", "analyzer": "stop" } } } }
Specify the search analyzer for a field
editWhen mapping an index, you can use the search_analyzer
mapping
parameter to specify a search analyzer for each text
field.
If a search analyzer is provided, the index analyzer must also be specified
using the analyzer
parameter.
The following create index API request sets the
simple
analyzer as the search analyzer for the title
field.
resp = client.indices.create( index="my-index-000001", mappings={ "properties": { "title": { "type": "text", "analyzer": "whitespace", "search_analyzer": "simple" } } }, ) print(resp)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { properties: { title: { type: 'text', analyzer: 'whitespace', search_analyzer: 'simple' } } } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { properties: { title: { type: "text", analyzer: "whitespace", search_analyzer: "simple", }, }, }, }); console.log(response);
PUT my-index-000001 { "mappings": { "properties": { "title": { "type": "text", "analyzer": "whitespace", "search_analyzer": "simple" } } } }
Specify the default search analyzer for an index
editWhen creating an index, you can set a default search
analyzer using the analysis.analyzer.default_search
setting.
If a search analyzer is provided, a default index analyzer must also be
specified using the analysis.analyzer.default
setting.
The following create index API request sets the
whitespace
analyzer as the default search analyzer for the my-index-000001
index.
resp = client.indices.create( index="my-index-000001", settings={ "analysis": { "analyzer": { "default": { "type": "simple" }, "default_search": { "type": "whitespace" } } } }, ) print(resp)
response = client.indices.create( index: 'my-index-000001', body: { settings: { analysis: { analyzer: { default: { type: 'simple' }, default_search: { type: 'whitespace' } } } } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", settings: { analysis: { analyzer: { default: { type: "simple", }, default_search: { type: "whitespace", }, }, }, }, }); console.log(response);
PUT my-index-000001 { "settings": { "analysis": { "analyzer": { "default": { "type": "simple" }, "default_search": { "type": "whitespace" } } } } }