如何为字符串列表类型的aliases字段配置多字段及对应分析器?
First off, the mapping you shared won’t work for your aliases string list—here’s why and how to fix it:
Your current mapping targets a names object, but your document has an aliases field that’s an array of strings. That mismatch means the mapping won’t apply to your actual data at all, which is why it feels like it doesn’t make sense for your use case.
The Correct Mapping for Your aliases Field
Since aliases is an array of strings, you don’t need an object or nested type (nested is only for arrays of objects, not plain strings). Instead, define aliases as a text field with multi-fields to support your language analyzer needs. Here’s a working example:
{ "mappings": { "properties": { "aliases": { "type": "text", "fields": { "native": { "type": "text", "analyzer": "en" } } } } } }
This setup does two things:
- The main
aliasesfield uses Elasticsearch’s default standard analyzer for general searches. - The
aliases.nativesub-field uses the English analyzer, which handles stemming (e.g., "john" matches "Johnny"), stop-word removal, and other English-specific optimizations.
When you index a document like {"aliases": ["John Doe", "Johnny D"]}, Elasticsearch automatically indexes all string values in both fields—no need to convert each string to an object.
Dynamic Mapping vs. Explicit Mapping
Dynamic mapping would auto-detect aliases as a text field with a keyword sub-field, but it won’t apply the English analyzer unless you set up a dynamic template. For your use case, explicit mapping is better because it gives you direct control over which analyzer is used for your aliases field.
Why Nested Objects Aren’t Needed Here
Nested objects are designed for arrays of structured objects (e.g., [{"firstName": "John", "lastName": "Doe"}, ...]). Since your aliases is just an array of plain strings, Elasticsearch handles this natively without needing the nested type—all values are indexed as part of the same field, and searches will match any value in the array.
Example Search Query
To leverage the English analyzer for precise searches on aliases, use a query targeting the native sub-field:
{ "query": { "match": { "aliases.native": "john" } } }
This query will match both "John Doe" and "Johnny D" because the English analyzer stems "john" to match the root form of those terms.
内容的提问来源于stack exchange,提问作者villasv

