2

I'm a beginner in Elasticsearch. I want to aggregate the result of Completion Suggester.

There are multi-indices and multiple tags are added on "tags" field like below.

Documents

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

And then, I created mappings like this.

Mappings

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

Query

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

Result

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

The query returns the results of suggestions correctly, however, the problem is that I can't get the total count of "Apple" How can I get the results like below?

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

I tried the query below, but it doesn't work.

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

0 Answers0