如何在Elasticsearch中对嵌套的JSON对象/字段进行术语查询?

huangapple go评论74阅读模式
英文:

How to term query nested json objects/fields in elastic search?

问题

以下是您要求的翻译内容:

我正在基于字段[type]执行术语聚合,如下所示,但是弹性只返回1个术语计数,而不是2个,它没有执行嵌套对象聚合,即在comments.data.comments[是一个列表]下面,我有2种类型。

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

I am doing term aggregation based on field [type] like below but elastic is returning only 1 term count instead of 2 it is not doing nested object aggregation i.e under comments.data.comments[is a list] under this i have 2 type.

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

答案1

得分: 2

请查阅下面翻译的内容:

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"
          }
        }
      }
    }
  }
}
英文:

Gotta utilize the nested field type:

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"
          }
        }
      }
    }
  }
}

huangapple
  • 本文由 发表于 2020年4月10日 21:08:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61140951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定