【问题标题】:Filter nested result inside a nested object with elasticsearch使用 elasticsearch 过滤嵌套对象内的嵌套结果
【发布时间】:2021-10-18 12:08:37
【问题描述】:

我正在尝试过滤嵌套对象并按结果排序,但是,我尝试了一些没有成功的事情,我将放弃我最初的尝试并且它部分工作,它只是根据我在搜索中的内容进行过滤变量,但所有结果都来自这个嵌套对象,因为它位于另一个嵌套对象的“根”内

弹性版本:带有 NodeJS 的 7.13.0 使用来自 npm 的 @elastic/elasticsearch 官方包

let params: RequestParams.Search = {
      index: index,
      body: {
        size: 30,
        query: {
          bool: {
            must: [
              {
                nested: {
                  path: "profile",
                  query: {
                    bool: {
                      must: [
                        {
                          match: {
                            "profile.id": profileId,
                          },
                        },
                      ],
                    },
                  },
                },
              },
            ],
            filter: [
              {
                nested: {
                  path: "profile.following",
                  ignore_unmapped: true,
                  query: {
                    query_string: {
                      fields: [
                        "profile.following.name",
                        "profile.following.username",
                      ],
                      query: searchWord + "*",
                    },
                  },
                },
              },
            ],
          },
        },
      },
    };

我需要它是在函数中通过参数传递的特定“profile.id”,因此结果只有 1 个具有 N 个人的个人资料

文档映射如下,我只留下了与问题相关的字段:

{
  "mappings": {
    "_doc": {
      "properties": {
        "id": {
          "type": "integer"
        },
        "phone": {
          "type": "text"
        },
        "profile": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "integer"
            },
            "username": {
              "type": "text"
            },
            "following": {
              "type": "nested",
              "properties": {
                "id": {
                  "type": "integer"
                },
                "isAwaitingApproval": {
                  "type": "boolean"
                },
                "name": {
                  "type": "text"
                },
                "profilePicURL": {
                  "type": "text"
                },
                "username": {
                  "type": "text"
                }
              }
            }
          }
        }
      }
    }
  }
}

当前结果的示例是: 带有以下参数 (profileId:141, searchWord: "para" )

{
    "res": [
        {
            "profilePicURL": "localimage",
            "name": "donor donor",
            "id": 140,
            "username": "victorTesteElastic2",
            "isAwaitingApproval": false
        },
        {
            "profilePicURL": "localimage",
            "name": "para ser seguido",
            "id": 142,
            "username": "victorprivate",
            "isAwaitingApproval": true
        }
    ]
}

想要的结果是:

{
  "res": [
      {
          "profilePicURL": "localimage",
          "name": "para ser seguido",
          "id": 142,
          "username": "victorprivate",
          "isAwaitingApproval": true
      }
  ]
}

【问题讨论】:

    标签: node.js elasticsearch


    【解决方案1】:

    通过更多研究,我得到了我需要的东西,我会在这里留下答案,以防有人也需要它

    let params: RequestParams.Search = {
          index: index,
          body: {
            size: 30,
            query: {
              bool: {
                must: [
                  {
                    nested: {
                      path: "profile",
                      query: {
                        bool: {
                          must: [
                            {
                              match: {
                                "profile.id": profileId,
                              },
                            },
                          ],
                        },
                      },
                    },
                  },
                  {
                    nested: {
                      path: "profile",
                      inner_hits: {
                        name: "profile",
                      },
                      query: {
                        nested: {
                          path: "profile.following",
                          inner_hits: {
                            name: "following",
                          },
                          ignore_unmapped: true,
                          query: {
                            query_string: {
                              fields: [
                                "profile.following.name",
                                "profile.following.username",
                              ],
                              query: searchWord + "*",
                            },
                          },
                        },
                      },
                    },
                  },
                ],
              },
            },
          },
        }; 
    

    我基本上把 must 放入过滤器中的内容,从上面映射嵌套对象,在本例中为配置文件,然后将标签 inner_hits 用于配置文件,将 inner_hits 用于跟随,这是唯一的方法成功了

    这里返回了我需要的答案: body.hits.hits[0].inner_hits.profile.hits.hits[0].inner_hits.following.hits.hits

    以下是答案示例:

    {
        "res": [
            {
                "_index": "donor",
                "_type": "_doc",
                "_id": "P3VWNnsB4coAEhD-F3fF",
                "_nested": {
                    "field": "profile",
                    "offset": 0,
                    "_nested": {
                        "field": "following",
                        "offset": 0
                    }
                },
                "_score": 1,
                "_source": {
                    "profilePicURL": "localimage",
                    "name": "donor donor",
                    "id": 140,
                    "username": "victorTesteElastic2",
                    "isAwaitingApproval": false
                }
            },
            {
                "_index": "donor",
                "_type": "_doc",
                "_id": "P3VWNnsB4coAEhD-F3fF",
                "_nested": {
                    "field": "profile",
                    "offset": 0,
                    "_nested": {
                        "field": "following",
                        "offset": 1
                    }
                },
                "_score": 1,
                "_source": {
                    "profilePicURL": "localimage",
                    "name": "para ser seguido",
                    "id": 142,
                    "username": "victorprivate",
                    "isAwaitingApproval": true
                }
            }
        ]
    }
    

    我真正需要的过滤数据是must 中的matched 在这个数组中,我需要在其中迭代并查看_source 这是被索引的数据

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-28
      相关资源
      最近更新 更多