Solr:按子文档字段分组并包括父字段

okxuctiv  于 2022-11-05  发布在  Solr
关注(0)|答案(1)|浏览(221)

是否有一种方法可以按子文档字段进行分组并将父字段包含到结果中?

[
  {
    "id": "p1",
    "name": "parent 1",
    "_childDocuments_": [
      {
        "id": "p1c1",
        "name": "child1_of_parent1",
        "color": "red"
      },
      {
        "id": "p1c2",
        "name": "child2_of_parent1",
        "color": "yellow"
      }
    ]
  },
  {
    "id": "p2",
    "name": "parent 2",
    "_childDocuments_": [
      {
        "id": "p2c1",
        "name": "child1_of_parent2",
        "color": "yellow"
      }
    ]
  }
]

在集合中。
现在,查询/选择?group=true&group.field=color&group.limit=10将返回

{
  "responseHeader":{
    "params":{
      "group.limit":"10",
      "group.field":"color",
      "group":"true"
    }
  },
  "grouped":{
    "color":{
      "matches":3,
      "groups":[
        {
          "groupValue":"red",
          "doclist":{"numFound":1,"docs":[
              {
                "id":"p1c1",
                "name":"child1_of_parent1"
              }
            ]
          }
        },
        {
          "groupValue":"yellow",
          "doclist":{"numFound":2,"docs":[
              {
                "id":"p1c2",
                "name":"child2_of_parent1"
              },
              {
                "id":"p2c1",
                "name":"child1_of_parent2"
              }
            ]
          }
        }
      ]
    }
  }
}

但我需要一个包含其父字段的结果,类似于

{
  "responseHeader":{
    "params":{
      "group.limit":"10",
      "group.field":"color",
      "group":"true"
    }
  },
  "grouped":{
    "color":{
      "matches":3,
      "groups":[
        {
          "groupValue":"red",
          "doclist":{"numFound":1,"docs":[
              {
                "id":"p1c1",
                "name":"child1_of_parent1",
                "parent":{
                  "id": "p1",
                  "name": "parent 1",
                }
              }
            ]
          }
        },
        {
          "groupValue":"yellow",
          "doclist":{"numFound":2,"docs":[
              {
                "id":"p1c2",
                "name":"child2_of_parent1",
                "parent":{
                  "id": "p1",
                  "name": "parent 1",
                }
              },
              {
                "id":"p2c1",
                "name":"child1_of_parent2",
                "parent":{
                  "id": "p2",
                  "name": "parent 2",
                }
              }
            ]
          }
        }
      ]
    }
  }
}

我来自关系数据库,在关系数据库中可以很容易地实现这一点。希望在solr中也有一种方法。我使用的是solr8.7.0

p8ekf7hl

p8ekf7hl1#

我发现的一个解决方案是子查询,它满足了需求,但是性能是当您将关系“连接”传输到文档数据库时所期望的。
将数据模型修改为平面结构肯定会是一个更好的主意。
之前我不得不在子文档中添加一个字段作为父id(默认的“root”不起作用):

[
  {
    "id": "p1",
    "name": "parent 1",
    "_childDocuments_": [
      {
        "id": "p1c1",
        "name": "child1_of_parent1",
        "color": "red",
        "parent_id": "p1"
      },
      {
        "id": "p1c2",
        "name": "child2_of_parent1",
        "color": "yellow",
        "parent_id": "p1"
      }
    ]
  },
  {
    "id": "p2",
    "name": "parent 2",
    "_childDocuments_": [
      {
        "id": "p2c1",
        "name": "child1_of_parent2",
        "color": "yellow",
        "parent_id": "p2"
      }
    ]
  }
]

现在我可以查询/选择?组=真&组.字段=颜色&组.限制=10&fl=*%2C父项%3A%5B子查询%5D &父项. q =%7B%21terms+f%3Did+v%3D%24row.parent_id%7D
然后它返回

{
  "responseHeader":{
    "params":{
      "group.limit":"10",
      "group.field":"color",
      "group":"true",
      "fl":"*,parent:[subquery]",
      "parent.q":"{!terms f=id v=$row.parent_id}",
    }
  },
  "grouped":{
    "color":{
      "matches":3,
      "groups":[
        {
          "groupValue":"red",
          "doclist":{"numFound":1,"docs":[
              {
                "id":"p1c1",
                "name":"child1_of_parent1",
                "parent":{"numFound":1,"start":0,"numFoundExact":true,"docs":[
                  "id": "p1",
                  "name": "parent 1",
                ]}
              }
            ]
          }
        },
        {
          "groupValue":"yellow",
          "doclist":{"numFound":2,"docs":[
              {
                "id":"p1c2",
                "name":"child2_of_parent1",
                "parent":{"numFound":1,"start":0,"numFoundExact":true,"docs":[
                  "id": "p1",
                  "name": "parent 1",
                ]}
              },
              {
                "id":"p2c1",
                "name":"child1_of_parent2",
                "parent":{"numFound":1,"start":0,"numFoundExact":true,"docs":[
                  "id": "p2",
                  "name": "parent 2",
                ]}
              }
            ]
          }
        }
      ]
    }
  }
}

如果这提醒了你一个更好的主意,请随意评论。

相关问题