首页 > 解决方案 > Elasticsearch 聚合 Suggesters 的结果

问题描述

我是 Elasticsearch 的初学者。我想汇总 Completion Suggester 的结果。

有多个索引,并且在“标签”字段中添加了多个标签,如下所示。

文件

GET http://localhost:9200/index1/user-category1/1?pretty

{
  "_index": "index1",
  "_type": "user-category1",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "user_id": 1,
    "tags": [
      "Apple",
      "Orange",
      "Peach"
    ]
  }
}

GET http://localhost:9200/index2/user-category2/2?pretty
{
  "_index": "index2",
  "_type": "user-category2",
  "_id": "2",
  "_version": 1,
  "found": true,
  "_source": {
    "user_id": 2,
    "tags": [
      "Grape",
      "Apple"
    ]
  }
}

然后,我创建了这样的映射。

映射

  "tags": {
  "type": "text",
  "fields": {
    "raw": {
      "type": "keyword"
    },
    "suggest": {
      "type": "completion"
    }
  }
}

询问

POST http://localhost:9200/_all/_search?pretty
{
  "_source": false,
  "suggest": {
    "tags_suggest": {
      "prefix": "A",
      "completion": {
        "field": "tags.suggest"
      }
    }
  }
}

结果

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 2,
    "successful": 2,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": 0,
    "hits": []
  },
  "suggest": {
    "tags_suggest": [
      {
        "text": "A",
        "offset": 0,
        "length": 1,
        "options": [
          {
            "text": "Apple",
            "_index": "index1",
            "_type": "user-category1",
            "_id": "1",
            "_score": 1
          },
          {
            "text": "Apple",
            "_index": "index2",
            "_type": "user-category2",
            "_id": "2",
            "_score": 1
          }
        ]
      }
    ]
  }
}

查询正确返回建议的结果,但是,问题是我无法获得“Apple”的总数如何获得如下结果?

"aggregations": {
  "keywords": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [
      {
        "key": "Apple",
        "doc_count": 2
      }
    ]
  }
}

我尝试了下面的查询,但它不起作用。

  {
  "_source": false,
  "suggest": {
    "tags_suggest": {
      "prefix": "A",
      "completion": {
        "field": "tags.suggest"
      }
    }
  },
  "aggs": {
    "tags": {
      "terms": {
        "field": "tags.keyword"
      }
    }
  }
}

标签: elasticsearchautosuggestsearch-suggestion

解决方案


推荐阅读