I'm relatively new to ElasticSearch and I need some advice. After several tries, i did not found the solution and that's why I need you.
I want to make a conditional query based on the document content.
Let me explain, I have those documents in ES:
{
"name": "Product n°1",
"type": "Mail",
"sub": "Letter"
},
{
"name": "Product n°2",
"type": "Video",
"sub": null
},
{
"name": "Product n°3",
"type": "Mail",
"sub": "Postcard"
}
The user can filter by types and sub with checkboxes (so, the user can search more than one type and sub at the same time)
Edited
Customers can selects with checkbox the types of the products they want to get from ES (For ex: Video, Image, Mail), they can select all of them.
The "Document" type has 4 sub-types: Letter, Postal Card, Paper, Printed and they can select which sub type they want to retrieve too.
So, we admit a customer selected Video and Mail, and Letter as sub-type of Mail.
I want to ES returns all of the documents within the selected types AND just the Letters when the document is a "Mail" type.
Sorry for my mistakes, I'm very new to ES.
Thanks to all of you trying to help !
UPDATE 2
Here's my query with Andrei Stefan's solution
{
"query": {
"filtered": {
"query": {
"match_all": []
},
"filter": {
"bool": {
"should": [
{
"terms": {
"type": [
"Video",
"Mail"
]
}
},
{
"bool": {
"must": [
{
"term": {
"type": Mail
}
},
{
"terms": {
"sub": [
"Letters"
]
}
}
]
}
}
]
}
}
}
}
}
ES returns all of documents of type Video and Mail, it does not apply the fillter Letters when the type is Mail.
{ "query": { "bool": { "should": [ { "terms": { // ['type' => $types] } }, { "bool": { "must": [ { "term": { // 'type' => 2 } }, { "terms": { // 'sub' => [1] } } ] } } ] } } }