elasticsearch 对祖父母的内心打击仍然不起作用

bwitn5fc  于 2023-01-08  发布在  ElasticSearch
关注(0)|答案(1)|浏览(101)

我在检索我的“祖父母”项的inner_hits时遇到问题。来自子查询的父项工作正常,但不能让它也返回更高一级的项。
有什么想法吗?
known issue现在应该是固定的(2.3),workaround是根据嵌套对象编写的,而不是父/子层次结构数据,所以不能让它为我工作。
有意义格式的代码:

POST /test/child/_search
{
  "query": {
    "has_parent": {
      "type": "parent",
      "query": {
        "has_parent": {
          "type": "grandparent",
          "query": {            
            "match_all": {}
          },
    "inner_hits": {}
        }
      },
    "inner_hits": {}
    }     
  }
}

PUT /test/child/3?parent=2&routing=1
{
  "id": 3,
  "name": "child",
  "parentid": 2
}
PUT /test/parent/2?parent=1&routing=1
{
  "id": 2,
  "name": "parent",
  "parentid": 1
}
PUT /test/grandparent/1
{
  "id": 1,
  "name": "grandparent"
}

PUT /test
{
  "mappings": {
    "grandparent": {},
    "parent": {
      "_parent": {
        "type": "grandparent" 
      }
    },
    "child": {
      "_parent": {
        "type": "parent" 
      }
    }
  }
}
tct7dpnv

tct7dpnv1#

这是用于查找祖父级的示例代码

const filterPath = `hits.hits.inner_hits.activity.hits.hits.inner_hits.user.hits.hits._source*,
    hits.hits.inner_hits.activity.hits.hits.inner_hits.user.hits.hits.inner_hits.fofo.hits.hits._source*`;

const source = ['id', 'name', 'thumbnail'];

  const { body } = await elasticWrapper.client.search({
        index: ElasticIndex.UserDataFactory,
        filter_path: filterPath,
        _source: source,
        body: {
          from,
          size,
          query: {
            bool: {
              must: [
                {
                  match: {
                    relation_type: ElasticRelationType.Like,
                  },
                },
                {
                  has_parent: {
                    parent_type: ElasticRelationType.Post,
                    query: {
                      bool: {
                        must: [
                          {
                            term: {
                              id: {
                                value: req.params.id,
                              },
                            },
                          },
                          {
                            has_parent: {
                              parent_type: ElasticRelationType.User,
                              query: {
                                bool: {
                                  must: [
                                    {
                                      exists: {
                                        field: 'id',
                                      },
                                    },
                                  ],
                                  should: [
                                    {
                                      has_child: {
                                        type: ElasticRelationType.Follower,
                                        query: {
                                          bool: {
                                            minimum_should_match: 1,
                                            should: [
                                              {
                                                match: {
                                                  follower:
                                                    req.currentUser?.id,
                                                },
                                              },
                                              {
                                                match: {
                                                  following:
                                                    req.currentUser?.id,
                                                },
                                              },
                                            ],
                                          },
                                        },
                                        inner_hits: {
                                          _source: [
                                            'follower',
                                            'following',
                                            'status',
                                          ],
                                        },
                                      },
                                    },
                                  ],
                                },
                              },
                              inner_hits: {
                                _source: ['id', 'name', 'thumbnail'],
                              },
                            },
                          },
                        ],
                      },
                    },
                    inner_hits: {},
                  },
                },
              ],
            },
          },
          sort: [
            {
              createdAt: {
                order: 'desc',
              },
            },
          ],
        },
      });

相关问题