使用子查询时如何将子文档与父项匹配?

时间:2018-06-18 13:08:23

标签: solr

我想在Solar中使用子查询,但我的子文档是由不相关的父母检索的。

模型示例:

{
    "id": 1,
    "parent_title": "stack overflow question 1",
    "_childDocuments_": [
        {
            "id": 1,
            "child_body": "body of title 1"
        }
    ]
},
{
    "id": 2,
    "parent_title": "stack overflow question 2",
    "_childDocuments_": [
        {
            "id": 2,
            "child_body": "body of title 2"
        }
    ]
}

我正在使用以下查询:

q= parent_title:stack
fl= *, subquery:[subquery]&subquery.q=child_body:title

结果:

{
    "id":"1",
    "parent_title":["stack overflow question 1"],
    "subquery":{"numFound":2,"start":0,"docs":[
        {
          "id":"1",
          "child_body":["body of title 1"]
        },
        {
          "id":"2",
          "child_body":["body of title 2"]            
        }]
    }
},
{
    "id":"2",
    "parent_title":["stack overflow question 2"],
    "subquery":{"numFound":2,"start":0,"docs":[
        {
          "id":"1",
          "child_body":["body of title 1"]        
        },
        {
          "id":"2",
          "child_body":["body of title 2"],
        }]
    }
}

问题:我不想在父“问题1”下检索子项“标题2”。那么,我如何将我的子查询子项与相应的父项匹配?

1 个答案:

答案 0 :(得分:0)

您可以更改模型以包含parent_id并按$row.id进行过滤。

型号:

{
    "id": 1,
    "parent_title": "stack overflow question 1",
    "_childDocuments_": [
        {
            "id": 1,
            "parent_id": 1,
            "child_body": "body of title 1"
        }
    ]
},
{
    "id": 2,
    "parent_title": "stack overflow question 2",
    "_childDocuments_": [
        {
            "id": 2,
            "parent_id": 2,
            "child_body": "body of title 2"
        }
    ]
}

查询:

q= parent_title:stack
fl= *, subquery:[subquery]&subquery.q=child_body:title&subquery.fq={!terms f=parent_id v=$row.id}

结果:

[
    {
        "id":"1",
        "parent_title":["stack overflow question 1"],
        "subquery":{"numFound":1,"start":0,"docs":[
            {
              "id":"1",
              "parent_id":[1],
              "child_body":["body of title 1"],
              "$row.id":1000
            }
        ]}
    },
    {
        "id":"2",
        "parent_title":["stack overflow question 2"],
        "subquery":{"numFound":1,"start":0,"docs":[
            {
              "id":"2",
              "parent_id":[2],
              "child_body":["body of title 2"],
              "$row.id":2000
            }
        ]}
    }
]