0

有没有办法按子文档字段分组并将父字段包含到结果中?想象一下你有

[
  {
    "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"
      }
    ]
  }
]

在一个集合中。

现在查询 /select?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也有办法。我正在使用 solr 8.7.0

4

1 回答 1

0

我找到的一种解决方案是subquery。它满足要求,但性能是您将关系“连接”传输到文档数据库时所期望的。

将数据模型重新设计为扁平结构肯定是一个更好的主意。

在我不得不在子文档中为父 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"
      }
    ]
  }
]

现在我可以查询 /select?group=true&group.field=color&group.limit=10&fl=*%2Cparent%3A%5Bsubquery%5D&parent.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",
                ]}
              }
            ]
          }
        }
      ]
    }
  }
}

如果这提醒您有一个更好的主意,请随时发表评论。

于 2021-10-20T07:46:08.317 回答