Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yuanzhaoYZ/912ef4e41f43fbdc2afcf02735af9fbc to your computer and use it in GitHub Desktop.
Save yuanzhaoYZ/912ef4e41f43fbdc2afcf02735af9fbc to your computer and use it in GitHub Desktop.
Elasticsearch missing filter with nested objects
PUT http://localhost:9200/test2/IR/1
{
"id": 1,
"priosenio": {
"claim": 1,
"ir": 1,
"senioclaim": 1,
"seniostatus": 1
}
}
PUT http://localhost:9200/test2/IR/2
{
"priosenio": {
"claim": 2,
"ir": 2,
"senioclaim": 2,
"seniostatus": 2
}
}
PUT http://localhost:9200/test2/IR/3
{
"id": 3,
"priosenio": {
"ir": 3,
"senioclaim": 3,
"seniostatus": 3
}
}
PUT http://localhost:9200/test2/IR/4
{
"id": 4,
"priosenio": []
}
PUT http://localhost:9200/test2/IR/5
{
"id": 5
}
Elasticsearch missing filter with nested objects
PUT http://localhost:9200/test2
{
"mappings": {
"IR": {
"_all": {
"enabled": false
},
"_id": {
"path": "id"
},
"properties": {
"id": {
"type": "string",
"null_value": "",
"index": "not_analyzed"
},
"priosenio": {
"type": "nested",
"properties": {
"claim": {
"type": "integer",
"null_value": -2147483648
},
"ir": {
"type": "integer",
"null_value": -2147483648
},
"senioclaim": {
"type": "integer",
"null_value": -2147483648
},
"seniostatus": {
"type": "integer",
"null_value": -2147483648
}
}
}
}
}
}
}
// this one works as expected, returning only doc with id 3, since it has "claim" attribute missing
POST http://localhost:9200/test2/IR/_search
{
"query": {
"filtered" : {
"filter" : {
"nested" : {
"path": "priosenio",
"filter": {
"missing" : {
"field" : "claim"
}
}
}
}
}
}
}
// however this query returns all the docs, and I am expecting only docs 4 and 5 (since it works with no-nested objects)
POST http://localhost:9200/test2/IR/_search
{
"query": {
"filtered" : {
"filter": {
"missing" : {
"field" : "priosenio"
}
}
}
}
}
// if I run the query as I would if priosenio was not a nested object, it returns all the docs (instead of only 4 and 5)
POST http://localhost:9200/test2/IR/_search
{
"filter": {
"missing" : {
"field" : "priosenio"
}
}
}
// this query solves the problem and returns all the docs (4 and 5) with "priosenio" missing
POST http://localhost:9200/test2/IR/_search
{
"filter": {
"not": {
"nested": {
"path": "priosenio",
"filter": {
"match_all": {}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment