Having a mapping with multi level nested fields like this:
{
otherFields....,
nestedField: {
type: "nested",
include_in_parent: true,
multiple: true,
properties: {
province: {
type: "nested",
include_in_parent: true,
multiple: true
},
properties: {
comuni: {
type: "nested",
include_in_parent: true,
multiple: true,
properties: {
nome: {
type: "string"
},
parziale: {
type: "boolean"
}
}
},
nome: {
type: "string"
}
}
}
},
regione: {
type: "string"
}
}
the documentation mentions that is possible to perform a query on this field https://www.elastic.co/guide/en/elasticsearch/reference/1.7/query-dsl-nested-query.html.
Using this query:
{
"size": 1,
"version": true,
"query": {
"filtered": {
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{
"regex_term": {
"field_2": {
"term": ".*701.*",
"ignorecase": true
}
}
}
]
}
},
"functions": [
{
"script_score": {
"script": "_score * -doc['field_2'].value.length()"
}
}
]
}
},
"filter": {
"nested": {
"path": "nestedField",
"filter": {
"bool": {
"must": [
{
"term": {
"nestedField.regione": "Lazio"
}
},
{
"bool": {
"must": [
{
"or": {
"filters": [
{
"term": {
"nestedField.province.nome": "Pordenone"
}
},
{
"not": {
"filter": {
"exists": {
"field": "nestedField.province.nome"
}
}
}
}
]
}
},
{
"or": {
"filters": [
{
"term": {
"nestedField.province.comuni.nome": "Udine"
}
},
{
"not": {
"filter": {
"exists": {
"field": "nestedField.province.comuni.nome"
}
}
}
}
]
}
}
]
}
}
]
}
}
}
}
}
}
}
the function score part looks good but both have some issues in the nested filter part. The data look like this:
{
"_source": {
"otheFields" ...,
"nestedField": [
{
"regione": "Lazio"
},
{
"province": [
{
"nome": "Venezia"
},
{
"nome": "Treviso"
}
],
"regione": "Veneto"
},
{
"province": [
{
"comuni": [
{
"nome": "Varmo",
"parziale": false
}
],
"nome": "Udine"
}
],
"regione": "Friuli venezia giulia"
}
]
}
}
The query doesn't find the given record even if the province field is missing, instead it work fine if we use "Veneto" for regione and "Treviso" for provincia.nome and also using the comune field in the other nested object.
Why this query doesn't work ?