Elasticsearch 5.X Percolate:如何自动生成copy_to字段?
Great question! I ran into a similar issue when migrating from ES 2.x to 5.x, so I know exactly where you're coming from. The core problem here is: when using the percolator in 5.x, the temporary in-memory index that processes your incoming document doesn't automatically apply copy_to rules unless you explicitly tell it to. Here's how to fix it:
Option 1: Add copy_to Mapping to Your Percolate Index
The simplest approach is to mirror the copy_to configuration from your business index into your percolate index. This ensures that when the percolator processes an incoming document, it uses the same field rules to generate your combined copy_to field.
For example, if your business index has this mapping:
PUT /business_index { "mappings": { "doc": { "properties": { "title": { "type": "text", "copy_to": "combined_content" }, "description": { "type": "text", "copy_to": "combined_content" }, "combined_content": { "type": "text" } } } } }
Update your percolate index (usually named .percolator) to include the same field definitions and copy_to rules:
PUT .percolator { "mappings": { "query": { "properties": { "query": { "type": "percolator" }, "title": { "type": "text", "copy_to": "combined_content" }, "description": { "type": "text", "copy_to": "combined_content" }, "combined_content": { "type": "text" } } } } }
Now, when you run a percolate request, the incoming document will generate the combined_content field just like it does in your regular searches, and your percolate queries targeting that field will work as expected.
Option 2: Specify a Temporary Mapping in the Percolate Request
If you don't want to modify your percolate index's global mapping (e.g., for one-off requests or varying copy_to rules), you can define the mapping directly in your percolate request. This tells the percolator to use those rules when processing the incoming document.
Here's an example request:
POST /business_index/_percolate { "mappings": { "doc": { "properties": { "title": { "type": "text", "copy_to": "combined_content" }, "description": { "type": "text", "copy_to": "combined_content" }, "combined_content": { "type": "text" } } } }, "doc": { "title": "New Product Launch", "description": "Check out our latest software update" } }
Critical Notes to Avoid Issues
- Sync Analyzers: If your
copy_tofield uses a custom analyzer, make sure to include that analyzer definition in the percolate index mapping (or temporary request mapping). Mismatched analyzers will lead to inconsistent tokenization and failed matches. - Query Consistency: Ensure your percolate queries are targeting the
copy_tofield exactly like your regular searches—no changes needed here if you're reusing the same query logic. - ES 5.x Percolator Syntax: Double-check that your percolate index uses the
percolatorfield type for storing queries (this is a change from 2.x, where queries were stored in a special_percolatorfield).
内容的提问来源于stack exchange,提问作者Jon Dewitt

