微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Elasticsearch - 按字段获取结果摘要

如何解决Elasticsearch - 按字段获取结果摘要

{"name": "John","district": 1,"light": 2},{"name": "John","district": 5,"light": 27},"district": 11,"light": 1},"light": 0},"light": 10},"district": 8,"light": 6},{"name": "Mary","district": 2,{"name": "Nick",{"name": "Bob","district": 3,{"name": "Kenny","light": 8}

名称字段搜索时,如何按组数获取剩余字段的摘要

示例:

搜索

  "query": {
    "term": {
      "name": {
        "value": "john"
      }
    }
  }

预期输出

{
  "district": {
    11: 3,// value : the amount of matches
    5: 2,1: 1,},"light": {
    2: 2,// value : the amount of matches
    27: 1,10: 1,// etc..
  }
}

解决方法

尝试一对术语聚合:

{
  "size": 0,"query": {
    "term": {
      "name": {
        "value": "john"
      }
    }
  },"aggs": {
    "by_district": {
      "terms": {
        "field": "district"
      }
    },"by_light": {
      "terms": {
        "field": "light"
      }
    }
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。