Elasticsearch父子关系类型相关性搜索技术求助
First, let's recap your existing index setup to ensure we're aligned. You've created a parent-child index where my_registration is the parent type, and my_specialities is the child type linked to it. Here's your current mapping and sample parent documents, properly formatted:
Your Index Mapping
PUT /myindex { "mappings": { "my_registration": {}, "my_specialities": { "_parent": { "type": "my_registration" } } } }
Sample Parent Documents
PUT myindex/my_registration/100 { "Pid": "100", "Name": "name1", "Age": "28" } PUT myindex/my_registration/200 { "Pid": "200", "Name": "name2", "Age": "28" }
Since your input cut off before adding child documents, I'll add a couple of sample child entries to make the relevance examples concrete:
Sample Child Documents
PUT myindex/my_specialities/1?parent=100 { "Speciality": "Cardiology", "Experience": "5 years" } PUT myindex/my_specialities/2?parent=200 { "Speciality": "Neurology", "Experience": "3 years" }
Now, let's dive into common relevance search scenarios tailored to your parent-child structure:
1. Search Parent Documents Based on Child Field Matches
If you want to find my_registration entries where their linked my_specialities match specific criteria, and factor the child's relevance into the parent's score, use the has_child query.
Example: Find registrations with a speciality matching "cardiology"
GET myindex/my_registration/_search { "query": { "has_child": { "type": "my_specialities", "query": { "match": { "Speciality": "cardiology" } }, "score_mode": "max" // Use "max", "sum", "avg", or "none" to control how child scores affect parent } } }
- score_mode: Determines how child document scores are aggregated into the parent's score.
maxtakes the highest score from matching children,sumadds all child scores,avgaverages them, andnoneignores child scores (just filters parents with matching children).
2. Combine Parent Field Filters with Child Relevance
You often need to filter parent documents first (e.g., by age) then apply child-based relevance scoring. Use a bool query to combine these:
Example: Find 28-year-old registrations with specialities matching "neurology"
GET myindex/my_registration/_search { "query": { "bool": { "filter": [ { "match": { "Age": "28" } } // Filter parents by age first (doesn't affect score) ], "must": [ { "has_child": { "type": "my_specialities", "query": { "match": { "Speciality": { "query": "neurology", "boost": 2 // Boost child match to increase parent relevance } } }, "score_mode": "max" } } ] } } }
- The
filterclause ensures we only consider 28-year-old registrations without impacting their relevance score. - The
boostparameter in the child'smatchquery increases the weight of that match, making relevant parents rank higher.
3. Customize Relevance with function_score
For more control over relevance, use function_score to adjust scores based on parent or child fields. For example, boost registrations where the child has more experience:
Example: Boost parents whose child has 5+ years of experience
GET myindex/my_registration/_search { "query": { "has_child": { "type": "my_specialities", "query": { "function_score": { "query": { "match_all": {} }, "functions": [ { "filter": { "match": { "Experience": "5 years" } }, "weight": 3 // Boost score by 3x for children with 5 years experience } ] } }, "score_mode": "sum" } } }
Key Tips for Better Relevance
- Map child fields appropriately: Ensure text fields (like
Speciality) use an analyzer that fits your search needs (e.g.,standardfor English, or a custom analyzer for medical terms). - Debug scores: Use the
explainparameter (GET myindex/my_registration/_search?explain=true) to see how Elasticsearch calculates relevance scores for each document. - Test with real data: Adjust
boostvalues andscore_modebased on your actual data and user expectations to fine-tune results.
内容的提问来源于stack exchange,提问作者Jamsheer

