Elasticsearch汇总建议者的结果

时间:2018-11-22 04:00:13

标签: elasticsearch autosuggest search-suggestion

我是Elasticsearch的初学者。 我想汇总完成建议器的结果。

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

文档

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

0 个答案:

没有答案