You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Elasticsearch父子关系类型相关性搜索技术求助

Building Relevance Search Queries for Your Elasticsearch Parent-Child Index

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. max takes the highest score from matching children, sum adds all child scores, avg averages them, and none ignores 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 filter clause ensures we only consider 28-year-old registrations without impacting their relevance score.
  • The boost parameter in the child's match query 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., standard for English, or a custom analyzer for medical terms).
  • Debug scores: Use the explain parameter (GET myindex/my_registration/_search?explain=true) to see how Elasticsearch calculates relevance scores for each document.
  • Test with real data: Adjust boost values and score_mode based on your actual data and user expectations to fine-tune results.

内容的提问来源于stack exchange,提问作者Jamsheer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:31:48