Elasticsearch短语映射技术问询:基于球员文档匹配目标短语
Got it, let's walk through a simple, practical way to map your Elasticsearch Player document to that target phrase and get the matching association working. Here's how I'd tackle it:
First, Let's Recap the Key Content
Target phrase to match:
It's never happened to me – Messi Player of Barcelona to Cristiano Ronaldo star of Real Madrid thanks Juve fans for ovation.
Your Player document structure:
{"firstName": {"localizedValues": {"de": "", "en": "Cristiano"}}, "lastName": {"localizedValues": {"de": "", "en": "Ronaldo"}}, "teamAssociations": [{"active": true, "teamName": {"localizedValues": {"de": "", "en": "Real Madrid"}}}]}
1. Identify Core Matchable Entities
First, pull out the fields from your Player doc that directly align with entities in the target phrase:
- Full player name: Combine
firstName.localizedValues.en+lastName.localizedValues.en→ Cristiano Ronaldo - Active team: Grab the active team's English name from
teamAssociations[0].teamName.localizedValues.en→ Real Madrid
2. Update Your Elasticsearch Mapping (for Better Matching)
To make these entities easy to match against the phrase, adjust your Player index mapping to include text fields with keyword sub-fields (great for both full-text and exact matches). Here's a simplified snippet:
{ "mappings": { "properties": { "fullName": { "type": "text", "fields": { "keyword": { "type": "keyword" } } }, "activeTeam": { "type": "text", "fields": { "keyword": { "type": "keyword" } } } } } }
Pro tip: You can either pre-calculate fullName and activeTeam when indexing documents, or use runtime fields to generate them on the fly if you don't want to reindex existing data.
3. Run a Multi-Match Query to Link the Document & Phrase
Use a multi-match query to search the target phrase against your Player doc's key fields. This will automatically find the overlap between the phrase and your document:
{ "query": { "multi_match": { "query": "It's never happened to me – Messi Player of Barcelona to Cristiano Ronaldo star of Real Madrid thanks Juve fans for ovation.", "fields": ["fullName", "activeTeam"] } } }
This query will return your Player doc because it contains both "Cristiano Ronaldo" and "Real Madrid" — which are clearly present in the target phrase.
4. Stricter Matching (Optional)
If you want to ensure you only match when the full name/team appear as exact phrases in the text, use a boolean match_phrase query instead:
{ "query": { "bool": { "must": [ { "match_phrase": { "fullName": "Cristiano Ronaldo" } }, { "match_phrase": { "activeTeam": "Real Madrid" } } ] } } }
You can dynamically plug in the entity values extracted from the target phrase into this query for reuse.
内容的提问来源于stack exchange,提问作者Mark Shawls

