I am new to Elasticsearch and I'm trying to write some queries, which happen not to work. I have information about people and I want to query the people based on some attributes.
Here's a part of the mapping in the Elasticsearch:
"properties": {
"doc": {
...
"languages": [{
"language": {
"type": "text",
"null_value": "NULL"
},
"level": {
"type": "integer",
"null_value": "NULL"
}
}],
...
}
}
For example, I want to filter out all of the people who speak English with level greater than 3 and Japanese with level greater than 2.
What I've already tried is this approach that doesn't give the expected result.
"size": 1000,
"from": 0,
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{"match": {"languages.language": "english"}},
{"range": {"languages.search_value": {"gt": 3}}}
]
}
},
{
"bool": {
"must": [
{"match": {"languages.language": "japanese"}},
{"range": {"languages.search_value": {"gt": 2}}}
]
}
}
]
}
}
The above query returns all of the people who speak English and Japanese, but the levels of the languages don't match. There are people who speak English with level 4 (which is ok), but Japanese with level 1 (which is not ok) and German with level 5. I guess the problem is that instead of looking for the wanted level just for the specified languages, my query looks for levels in all of the spoken languages by the people.
So, the result that I'm expecting from the query is to get all of the people who speak English with level 4 or 5 and Japanese with level 3, 4 or 5 (both in the same time). I don't care about the other languages they speak and their levels.
Any help or idea how can I solve this is welcomed.