How to check for missing key in nested type elasticearch object?

I have a case where I collect some general information and db information (db2, oracle, sybase, informix) in value pairs of json format doc value.

I also have some rules to check if a particular rule satisfies the above document, if it does, as a result returns that particular document for analysis.

here is the document

PUT /twitter/tweet/1
{
    "name": "Athena",
    "version": 1,
    "db": {
        "@type": "Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 64bit",
        "oracle_props": [
            {
                "@name": "open_cursors",
                "@value": 4000
            },
            {
                "@name": "USER_ROLE_PRIVS_COUNT",
                "@value": 1
            }
        ]
    }
}

and this mapping

PUT /twitter/tweet/_mapping
{
   "properties": {
      "db": {
         "type": "object",
         "properties": {
            "@type": {
               "type": "string"
            },
            "oracle_props": {
               "type": "nested",
               "properties": {
                  "@name": {
                     "type": "string"
                  },
                  "@value": {
                     "type": "long"
                  }
               }
            }
         }
      }
   }
}

Rules rules

A list of documents where tweet with name Athenaand a Oracle databasehas opencursors less than recommendaed value 4000or when opencursors is not present.

Therefore, the above doc /twitter/tweet/1should be returned as a result only if the following matches.

  • If (name == "Athena") && & (type db. @ Contains the Oracle keyword)
  • (if (( "open_cursors" @value < 4000) ( "open_cursors" "db.oracle_props. @name" )

- , , (display doc "/twitter/tweet/1", "db.oracle_props. @name" "open_cursors" ).

GET /twitter/tweet/_search
{
   "query": {
      "bool": {
         "must": [
            {
               "match": {
                  "tweet.name": "Athena"
               }
            },
            {
               "match": {
                  "tweet.db.@type": "Oracle"
               }
            }
         ],
         "should": [
            {
               "nested": {
                  "path": "db.oracle_props",
                  "query": {
                     "bool": {
                        "must": [
                           {
                              "term": {
                                 "db.oracle_props.@name": "open_cursors"
                              }
                           },
                           {
                              "range": {
                                 "db.oracle_props.@value": {
                                    "lt": 4001
                                 }
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ],
         "minimum_should_match": 1
      }
   }
}
+1
1

, .

, , :

// All good, should match
curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{
    "name": "Athena",
    "version": 1,
    "db": {
        "@type": "Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 64bit",
        "oracle_props": [
            {
                "@name": "open_cursors",
                "@value": 4000
            },
            {
                "@name": "USER_ROLE_PRIVS_COUNT",
                "@value": 1
            },
            {
                "@name": "CREATE_PERMISSION",
                "@value": "Y"
            }
        ]
    }
}'

// open cursors missing, should match
curl -XPUT 'http://localhost:9200/twitter/tweet/2' -d '{
    "name": "Athena",
    "version": 1,
    "db": {
        "@type": "Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 64bit",
        "oracle_props": [
            {
                "@name": "USER_ROLE_PRIVS_COUNT",
                "@value": 2
            },
            {
                "@name": "CREATE_PERMISSION",
                "@value": "N"
            }
        ]
    }
}'

// open_cursors less than 4000, should match
curl -XPUT 'http://localhost:9200/twitter/tweet/3' -d '{
    "name": "Athena",
    "version": 1,
    "db": {
        "@type": "Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 64bit",
        "oracle_props": [
            {
                "@name": "open_cursors",
                "@value": 2134
            },
            {
                "@name": "USER_ROLE_PRIVS_COUNT",
                "@value": 6
            },
            {
                "@name": "CREATE_PERMISSION",
                "@value": "N"
            }
        ]
    }
}'

// Different name, shouldn't match
curl -XPUT 'http://localhost:9200/twitter/tweet/4' -d '{
    "name": "Alexandroupolis",
    "version": 1,
    "db": {
        "@type": "Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 64bit",
        "oracle_props": [
            {
                "@name": "open_cursors",
                "@value": 4000
            },
            {
                "@name": "USER_ROLE_PRIVS_COUNT",
                "@value": 1
            },
            {
                "@name": "CREATE_PERMISSION",
                "@value": "Y"
            }
        ]
    }
}'

// open_cursors more than 4000, shouldn't match
curl -XPUT 'http://localhost:9200/twitter/tweet/5' -d '{
    "name": "Athena",
    "version": 1,
    "db": {
        "@type": "Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 64bit",
        "oracle_props": [
            {
                "@name": "open_cursors",
                "@value": 6500
            },
            {
                "@name": "USER_ROLE_PRIVS_COUNT",
                "@value": 1
            },
            {
                "@name": "CREATE_PERMISSION",
                "@value": "Y"
            }
        ]
    }
}'

, 3 (ID 1,2,3), .

, , , , - ?

, OR

curl -XGET 'http://localhost:9200/twitter/tweet/_search?pretty=true' -d '
{
    "query" : {
        "filtered" : {
            "filter" : {
                /* Set up two conditions */
                "or" : [
                    /* First */
                    {
                        /* Check for open_cursors AND value < 4000 */
                        "bool" : {
                            "must" : [
                                /* Same nested query as in other questions answer */
                                {
                                    "nested" : {
                                        "path" : "db.oracle_props",
                                        "filter" : {
                                            "bool" : {
                                                "must" : [
                                                    {
                                                    "term": {
                                                        "db.oracle_props.@name": "open_cursors"
                                                    }
                                                },
                                                {
                                                    "range": {
                                                        "db.oracle_props.@value": {
                                                            "lte": 4000
                                                        }
                                                    }
                                                }
                                                ]
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    },
                    /* OR */
                    {
                        "bool" : {
                            /* watch out: negation, this MUST NOT be found*/
                            "must_not" : [
                                {
                                    "nested" : {
                                        "path" : "db.oracle_props",
                                        "filter" : {
                                            "bool" : {
                                                /* We do not want open_cursors to be in the nested document */
                                                "must" : [
                                                    {
                                                    "term": {
                                                        "db.oracle_props.@name": "open_cursors"
                                                    }
                                                }
                                                ]
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                ]
            },
            /* the query for the non-nested things */
            "query" : {
                "bool" : {
                    "must" : [
                        {
                            "match" : {"tweet.name" : "Athena"}
                        },
                        {
                            "match" : {"tweet.db.@type" : "Oracle"}
                        }
                    ]
                }
            }
        }
    }
}
'

1,2 3.

: . @TuanHuynh

curl -XGET 'http://localhost:9200/twitter/tweet/_search?pretty=true' -d '
{
    "query" : {
        "filtered" : {
            "filter" : {
                /* Set up two conditions */
                "or" : [
                    /* First */
                    {
                        "nested" : {
                            "path" : "db.oracle_props",
                            "filter" : {
                                "bool" : {
                                    "must" : [
                                        {
                                        "term": {
                                            "db.oracle_props.@name": "open_cursors"
                                        }
                                    },
                                    {
                                        "range": {
                                            "db.oracle_props.@value": {
                                                "lte": 4000
                                            }
                                        }
                                    }
                                    ]
                                }
                            }
                        }
                    },
                    /* OR */
                    {
                        "nested" : {
                            "path" : "db.oracle_props",
                            "filter" : {
                                "bool" : {
                                    /* We do not want open_cursors to be in the nested document */
                                    "must" : [
                                        {
                                        "term": {
                                            "db.oracle_props.@name": "open_cursors"
                                        }
                                    }
                                    ]
                                }
                            }
                        }
                    }
                ]
            },
            /* the query for the non-nested things */
            "query" : {
                "bool" : {
                    "must" : [
                        {
                            "match" : {"tweet.name" : "Athena"}
                        },
                        {
                            "match" : {"tweet.db.@type" : "Oracle"}
                        }
                    ]
                }
            }
        }
    }
}
'
+3

Source: https://habr.com/ru/post/1548014/


All Articles