'How can I query/filter an exact count by an array of values in Elasticsearch?

I have these documents:

{"title": "aa","cast": ["a","b","c","a"]}
{"title": "bb","cast": ["a","b","c","b"]}
{"title": "cc","cast": ["a","b","c","c"]}

I get the result using the faceting.

facets": {
      "cast_result": {
         "_type": "terms",
         "missing": 0,
         "total": 9,
         "other": 0,
         "terms": [
            {
               "term": "c",
               "count": 3
            },
            {
               "term": "b",
               "count": 3
            },
            {
               "term": "a",
               "count": 3
            }
         ]
      }
  }
}

but I want the result that the count of "a" is four,Because the frequency of "a" is four in the real document."b" and "c" also like the "a".



Solution 1:[1]

create the index

query

the sum of term is the answer

But I find a question ,when the number of the shards is more than 2, the answer is wrong ,so it must be lower than 2.It doesn't matter about the number of replicas.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Luoluo Flly