Hi everyone,
Me and my team have similar properties in our documents(of course we have multiple of these array of objects that we would like to query by):
{
...
"example_array": [
{
"type": "some_test_type1",
"label": "some_test_label1"
},
{
"type": "some_test_type2",
"label": "some_test_label2"
}
]
...
}
We need to get all documents that have specificaly an object with type = some_test_type1 AND label = some_test_label1
(here the example is only with one object but we could want to filter by multiple
like: (type = some_test_type1 label = some_test_label1) AND (type = some_test_type2 AND label = some_test_label2) AND (type = some_test_type3 AND label = some_test_label3), etc...
we could also have more nesting and the objects that we filter by could have different nesting levels)
We tried with a query like this:
{
"query": {
"bool": {
"must": [
{
"term": {"example_array.type": "some_test_type1"}
},
{
"term": {"example_array.label": "some_test_label1"}
}
]
}
}
}
but it did not work, we got our desired documents but we also got results like this:
{
...
"example_array": [
{
"type": "some_test_type1",
"label": "some_test_label2"
},
{
"type": "some_test_type2",
"label": "some_test_label1"
}
]
...
}
(notice the some_test_type1 + some_test_label2 in first object and the opposite in the second object)which is not desired.
We then changed our array(example_array) to be type nested and tried with the following query:
{
"query": {
"nested": {
"path": "example_array",
"query": {
"bool": {
"must": [
{ "term": { "example_array.type": "some_test_type1" }},
{ "term": { "example_array.lable": "some_test_label1" }}
]
}
}
}
}
}
It worked but now we need to change a lot of our old logic in our API for building queries since we need to change most of our fields to nested and old queries with terms/match return nothing now. Also we read that you should avoid having a lot nested fields since it could impact performance.
We are wondering if there is a feature that we are missing that could help our case or if you have other recommendations that could help, we would be very grateful if you share it. Thank you!