【问题标题】:Elastic search exclude a nested element from search results, get element by id弹性搜索从搜索结果中排除嵌套元素,通过 id 获取元素
【发布时间】:2021-08-07 11:37:59
【问题描述】:

我有嵌套对象的项目:

          {
                "name": "The Amazon rainforest",
                "id": "610d33da26c25b00191ebcbe",
                "tags": [
                    {
                        "name": "Brazil",
                        "verified": 1
                    },
                    {

                        "name": "new_tag",
                        "verified": 0,
                    }
                ],

            }

在搜索结果中未验证的标签应该省略:

按 id 搜索的输出:610d33da26c25b00191ebcbe

          {
                "name": "The Amazon rainforest",
                "id": "610d33da26c25b00191ebcbe",
                "tags": [
                    {
                        "name": "Brazil",
                        "verified": 1
                    }
                ],

            }

【问题讨论】:

    标签: elasticsearch nested elastic-stack


    【解决方案1】:

    Node.js 版本的答案:

          const { body } = await elasticWrapper.client.search({
            index: ElasticIndexs.Products,
            filter_path:
              'hits.hits._source*, hits.hits.inner_hits.tags.hits.hits._source*',
            body: {
              _source: {
                excludes: ['tags'],
              },
              query: {
                bool: {
                  must: [
                    {
                      match: {
                        id: req.params.id,
                      },
                    },
                  ],
                  should: [
                    {
                      nested: {
                        path: 'tags',
                        query: {
                          term: {
                            'tags.verified': 1,
                          },
                        },
                        inner_hits: {},
                      },
                    },
                  ],
                },
              },
            },
          });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-01
      • 2011-08-16
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多