【问题标题】:How to term query nested json objects/fields in elastic search?如何在弹性搜索中术语查询嵌套的 json 对象/字段?
【发布时间】:2020-07-23 05:47:38
【问题描述】:

我正在根据字段 [type] 进行术语聚合,如下所示,但弹性仅返回 1 个术语计数而不是 2 它没有进行嵌套对象聚合,即在 cmets.data.cmets[is a list] 下我有2种。

{
    "aggs": {
        "genres": {
            "terms": {
                "field": "comments.data.comments.type"
            }
        }
    }
}

【问题讨论】:

  • 你能提供你的映射吗?
  • 我不确定映射,因为我是弹性搜索的新手。
  • 我使用了您之前评论过的映射,您能否提供正确的搜索查询,您给出的查询有问题
  • { "aggs": { "genres": { "terms": { "field": "events.ecommerceData.cmets.recommendationType" } } } }
  • 问题中已经存在

标签: java elasticsearch kibana elastic-stack elasticsearch-5


【解决方案1】:

必须使用nested 字段类型:

PUT events
{
  "mappings": {
    "properties": {
      "events": {
        "type": "nested",
        "properties": {
          "ecommerceData": {
            "type": "nested",
            "properties": {
              "comments": {
                "type": "nested",
                "properties": {
                  "recommendationType": {
                    "type": "keyword"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

POST events/_doc
{
  "events": [
    {
      "eventId": "1",
      "ecommerceData": [
        {
          "comments": [
            {
              "rank": 1,
              "recommendationType": "abc"
            },
            {
              "rank": 1,
              "recommendationType": "abc"
            }
          ]
        }
      ]
    }
  ]
}

GET events/_search
{
  "size": 0,
  "aggs": {
    "genres": {
      "nested": {
        "path": "events.ecommerceData.comments"
      },
      "aggs": {
        "nested_comments_recomms": {
          "terms": {
            "field": "events.ecommerceData.comments.recommendationType"
          }
        }
      }
    }
  }
}

【讨论】:

  • 您好 jzzfs..感谢您提供正确的解决方案..您显示的映射不适用于现有索引...我已经重新发布了我的原始文档..
  • 必须删除索引并重新同步。
  • 另外,您似乎已将 ecommerceData 更改为 recommendationData,因此请确保您也在映射中进行了更改。
  • 我这样做是抛出错误对象映射 [events] can't be changed from non-nested to nested
  • 不——删除索引意味着删除它。 curl -XDELETE ...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
  • 1970-01-01
  • 1970-01-01
  • 2014-12-30
  • 2020-02-25
  • 1970-01-01
  • 2021-09-01
相关资源
最近更新 更多