Hello,
I have the following index mappings:
{
"mappings": {
"properties": {
"library": {
"type": "keyword"
},
"books": {
"properties": {
"name": {
"type": "keyword"
},
"id": {
"type": "keyword"
}
}
}
}
}
}
The following indexed documents:
{"index":{"_index":"library","_id":"1"}}
{ "library": "amsterdam", "books": [{"id":"1", "name": "Book1" }, {"id":"2", "name": "Book2"}, { "id":"3", "name": "Book3" }] }
{"index":{"_index":"library","_id":"2"}}
{ "library": "paris", "books": [{"id":"4", "name": "Book4" }, {"id":"2", "name": "Book2"}, { "id":"5", "name": "Book5" }] }
{"index":{"_index":"library","_id":"3"}}
{ "library": "berlin", "books": [{"id":"5", "name": "Book5" }, {"id":"2", "name": "Book2"}, { "id":"6", "name": "Book6" }] }
My goal is to create a multi field search like they have in https://youtrack.jetbrains.com/issues for example, but in a much more simplified version.
My example focuses on a 'library' that can have multiple 'books' and each book can exist in multiple libraries at the same time. For the purpose of providing 'as-you-type' book name suggestions I have attempted to use a term aggregation combined with a filter aggregation as follows:
{
"size": 0,
"aggs": {
"filtered_books": {
"filter": {
"term": {
"books.name": "Book6"
}
},
"aggs": {
"books": {
"terms": {
"field": "books.name"
}
}
}
}
}
}
With the following results:
"filtered_books": {
"doc_count": 1,
"books": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Book2",
"doc_count": 1
},
{
"key": "Book5",
"doc_count": 1
},
{
"key": "Book6",
"doc_count": 1
}
]
}
}
From my understanding of the above filtering based on "Book6" will give document with name"Berlin", this document has other 2 tags so in the end performing the aggregation on it will result in 3 books . I want to have just "Book6" in the final aggregation.
As I've mentioned, my goal is to have a filtered list of 'books' based on their name to be provided in multi-field search suggestions.
I'm not sure this is the correct approach to achieving that.
ES 7.6.2
Thank you