Json 如何检查嵌套类型elasticsearch对象中缺少的键?
我在一个案例中收集了一些常规信息和数据库信息(db2、oracle、sybase、informix),这些信息以键值对json格式的文档形式存在 我也有一些规则来检查上面的文档是否满足特定的规则,如果满足,则返回特定的文档进行分析 这是医生Json 如何检查嵌套类型elasticsearch对象中缺少的键?,json,
elasticsearch,Json,
elasticsearch,我在一个案例中收集了一些常规信息和数据库信息(db2、oracle、sybase、informix),这些信息以键值对json格式的文档形式存在 我也有一些规则来检查上面的文档是否满足特定的规则,如果满足,则返回特定的文档进行分析 这是医生 PUT /twitter/tweet/1 { "name": "Athena", "version": 1, "db": { "@type": "Oracle Database 10g Enterprise Editi
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
}
]
}
}
这是它的地图
PUT /twitter/tweet/_mapping
{
"properties": {
"db": {
"type": "object",
"properties": {
"@type": {
"type": "string"
},
"oracle_props": {
"type": "nested",
"properties": {
"@name": {
"type": "string"
},
"@value": {
"type": "long"
}
}
}
}
}
}
}
规则标准
列出使用名称雅典娜
和Oracle数据库
的tweet的opencursors小于建议值4000
或当opencursors不存在时的文档
因此,只有在以下匹配的情况下,上面的doc/twitter/tweet/1
才会作为结果返回
If(name==“Athena”)&&(db.@类型包含“Oracle”关键字)
和(如果((“open_cursors”@value<4000)或(“open_cursors”未在“db.oracle_props@name”下找到)
下面是与上述文档匹配但缺少最后一个条件的搜索查询(display doc)/twitter/tweet/1”,即使在“db.oracle\u props@name”下缺少“open\u cursors”键
我会再试一次,参考你的和
如果我正确理解您的要求,我将建立一些示例文档,这些文档应该与我的评论正确匹配,或者不正确匹配:
// 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个应该返回的文档(ID1,2,3
)
我发现的解决方案似乎很复杂,也许其他人可以提供一个更简单的方法来解决这个问题
为了能够使用
谢谢。这正是我需要的。@Thorsten,为什么我们需要在嵌套筛选器之前使用bool.must?我们可以在OR筛选器之后立即使用嵌套筛选器吗?谢谢,没有它应该可以工作,你是对的。我已经更新了答案。
// 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"
}
]
}
}'
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"}
}
]
}
}
}
}
}
'
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"}
}
]
}
}
}
}
}
'