Ideally, you would need to change the mapping of your index, to be able to search all the comments from one blog post. You can't really search for documents and say that one particular blog id (which is a field in documents) matched over multiple documents at the same time. Elasticsearch knows how to match across multiple fields from the same document, not multiple.
There is one workaround, though. But it depends on what else you need to do with this query, apart from getting back JUST the blog ID.
GET /blog/entries/_search?search_type=count
{
"query": {
"match": {
"comments": "Apple Earth"
}
},
"aggs": {
"unique": {
"terms": {
"field": "blog_id",
"min_doc_count": 2
}
}
}
}
The query above will return something like this:
"aggregations": {
"unique": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 2,
"doc_count": 2
}
]
}
}
The idea of the query is to return just the blog_id
("key":2
under buckets
), thus you see there an aggregation of type terms
. And depending on how many terms you search (Apple Earth
counts for two terms), you set min_doc_count
to the number of terms. Meaning, you say that you want to search for apple earth
in minimum two documents. The difference between your example and what this actually does is that it will return documents that have, for example apple earth
for comments
, not just apple
in one document and earth
in another.
But, as I said, ideally you'd want to change the mapping of your index.