【问题标题】:Get all tags with count from all documents elasticsearch从所有文档中获取所有带有计数的标签elasticsearch
【发布时间】:2017-01-28 16:10:29
【问题描述】:

我有带有源字段的索引 mp_v1:id 和标签。 “标签”字段包含字符串中文档中的所有标签。

例子:

{
        "_index": "mp_v1",
        "_type": "mp",
        "_id": "5",
        "_score": 1,
        "_source": {
          "id": 5,
          "tags": "tag1 black blue"
        }
}

如何从所有文档中出现的弹性搜索标签中获取信息?例如,如果我有两个文档,第一个带有标签“tag1 black blue”,第二个带有标签“blue square”,它应该返回:blue: 2, tag1: 1, black: 1, square: 1

【问题讨论】:

  • tags 是单个字符串?如果它是array 类型,那么它就是小菜一碟。如果不可能是数组,那么您可以使用regex query

标签: elasticsearch


【解决方案1】:

我正在运行 ES 5.12

PUT testindex_51
{
    "settings": {
        "analysis": {
            "analyzer": {
            },
             "filter":{
        }
        }
    },
    "mappings": {
        "table1": {
            "properties": {
                "title": {
                    "type": "text",
                    "analyzer": "whitespace",
                    "fielddata": true
                }
            }
        }
    }
}

POST testindex_50/table1
{
  "title" : "tag1 aggs1 blue"
}

POST testindex_50/table1
{
  "title" : "tag2 aggs2 blue"
}

POST testindex_50/table1/_search
{
  "aggs": {
    "tags_count": {
      "terms": {
        "field": "title",
        "size": 10
      }
    }
  }
}

回应

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "tags_count": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "blue",
          "doc_count": 2
        },
        {
          "key": "aggs1",
          "doc_count": 1
        },
        {
          "key": "aggs2",
          "doc_count": 1
        },
        {
          "key": "tag1",
          "doc_count": 1
        },
        {
          "key": "tag2",
          "doc_count": 1
        }
      ]
    }
  }
}

【讨论】:

    【解决方案2】:

    您可以简单地使用简单的术语聚合来获得相同的启用字段数据(肮脏的方式)。

    但建议使用分解字段然后执行聚合。

    【讨论】:

      猜你喜欢
      • 2021-05-10
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      相关资源
      最近更新 更多