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

如何在Solr中构造查询,匹配子文档条件并求和exp值获取父文档

Solr Query to Fetch Parent Document by Child Field Conditions & Sum of Child Values

Got it, let's walk through how to build the right Solr query to get the parent document ID data1 based on your requirements. First, let's make sure we cover the prerequisites, then build the query step by step.

Prerequisites

Before we dive into the query, confirm your Solr setup meets these:

  • Your schema defines the necessary fields correctly:
    • isParent: A string or integer field to distinguish parent docs (value 1) from child docs (value 2)
    • description: A text field (set up for full-text matching)
    • exp: A numeric field (like int or float) to support sum calculations
  • You're using a Solr version that supports the child() function and Block Join queries (Solr 6.6+; 8.x and above is recommended for better nested document support)

Build the Correct Query

Your goal is to find parent docs where:

  1. At least one child doc has Payroll in its description
  2. The sum of exp values for those matching child docs equals 7

We'll combine Block Join Parent Queries (to link parent and child docs) with a Function Query (to calculate the sum of child field values). Here's the full query:

select?q=*:*
&fq={!parent which="isParent:1"}(description:Payroll)
&fq=sum(child(exp,description:Payroll)):7
&fl=id

Breakdown of Each Parameter

  • {!parent which="isParent:1"}(description:Payroll): This Block Join filter targets parent docs that have at least one child doc matching description:Payroll. The which clause tells Solr that parent docs are identified by isParent:1.
  • sum(child(exp,description:Payroll)):7: This filter calculates the sum of exp values only for child docs that match description:Payroll, and keeps only parent docs where this sum equals 7. If you wanted the sum of all child docs (regardless of description) plus the description match, you'd use sum(child(exp)):7 instead.
  • fl=id: This parameter tells Solr to only return the parent doc's id field, keeping the response clean and focused.

Expected Response

When you run the query, Solr will return the matching parent document. Here's what the response will look like (simplified):

{
  "responseHeader": {
    "status": 0,
    "QTime": 2,
    "params": {
      "q": "*:*",
      "fq": [
        "{!parent which=\"isParent:1\"}(description:Payroll)",
        "sum(child(exp,description:Payroll)):7"
      ],
      "fl": "id"
    }
  },
  "response": {
    "numFound": 1,
    "start": 0,
    "docs": [
      {
        "id": "data1"
      }
    ]
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:01:54