如何通过键值对弹性搜索数组中的键来匹配数组值?

abi*_*964 3 json elasticsearch

我有key value一对对.是否可以精确匹配值,key然后检查它的value范围值?

示例:在下面的doc中oracle_props是一个包含名称,值对的数组.我需要检查它是否有"oracle_cursors"密钥,然后检查它的值是否小于1000.

GET /eg/message/_percolate
{
   "doc": {
      "client": {
         "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"
               }
            ]
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

下面是我的过滤器.

我还需要检查以下内容,以便它返回3作为我的结果

  1. "client.name"必须是"Athena"
  2. "client.db.@ type"必须是"Oracle",然后才继续检查以下几点
  3. 找不到"client.db.oracle_props.@ name"字段
  4. 检查它是否有"oracle_cursors"键,然后检查它的值是否<1000

1和2是和操作,并且3或4中的任何一个满足它应该得到3.我需要第4点的帮助,下面是我的查询.如果有更好的方法,请建议.

PUT /eg/.percolator/3
{
   "query": {
      "filtered": {
         "filter": {
            "or": [
               {
                  "missing": {
                     "field": "client.db.oracle_props.@name"
                  }
               }
            ]
         },
         "query": {
            "bool": {
               "must": [
                  {
                     "match": {
                        "client.name": "Athena"
                     }
                  },
                  {
                     "match": {
                        "client.db.@type": "Oracle"
                     }
                  }
               ]
            }
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

更新

我可以在下面找到类似的东西

{
     "match": {
                    "client.db.oracle_props[name='open_cursors'].value": 4000
                 }
              }
Run Code Online (Sandbox Code Playgroud)

更多尝试

我遵循elasticsearch 嵌套查询并通过重新索引将映射更改为嵌套类型.任何人都可以找到问题为什么我得到nested: NullPointerException;

PUT /eg/.percolator/3
{
   "nested" : {
        "path" : "client.db.oracle_props",
        "score_mode" : "avg",
        "query" : {
            "bool" : {
                "must" : [
                    {
                        "match" : {"client.db.oracle_props.@name" : "open_cursors"}
                    },
                    {
                        "range" : {"client.db.oracle_props.@value" : {"lt" : 4000}}
                    }
                ]
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

映射变化

...
"properties": {
               "@type": {
                  "type": "string"
               },
               "oracle_props": {
                   "type" : "nested",
                  "properties": {
                     "@name": {
                        "type": "string"
                     },
                     "@value": {
                        "type": "long"
                     }
                  }
               }
            }
...
Run Code Online (Sandbox Code Playgroud)

Tho*_*ten 5

让我们进入它:

  1. 您似乎将嵌套路径映射错误,oracle_propsdb示例文档中的子项,但不在映射中,它直接显示为根的子项.
  2. 您正在映射oracle_props.@valuelong,但YCREATE_PERMISSION嵌套文档中为其指定文本
  3. 您查询range lt 4000,其中不包括 4000,lte将适合你

我没有得到你对缺失值的要求,因此我跳过了.

为了让你走上正确的道路,我必须稍微简化一下(因为我无法解决你问题中的所有混乱,对不起)

我也没有进入渗透,并将所有内容重命名为twitter/tweet,因为我更容易从我的示例中复制.

1)创建空索引"twitter"

curl -XDELETE 'http://localhost:9200/twitter/'
curl -XPUT 'http://localhost:9200/twitter/'
Run Code Online (Sandbox Code Playgroud)

2)为实际的"推文"创建geo_point映射

curl -XPUT 'http://localhost:9200/twitter/tweet/_mapping' -d '
{
    "tweet": {
        "properties": {
            "db": {
                "type": "object",
                "properties": {
                    "@type": {
                        "type": "string"
                    },
                    "oracle_props": {
                        "type": "nested",
                        "properties": {
                            "@name": {
                                "type": "string"
                            },
                            "@value": {
                                "type": "string"
                            }
                        }
                    }
                }
            }
        }
    }
}'
Run Code Online (Sandbox Code Playgroud)

3)让我们检查映射是否已设置

curl -XGET 'http://localhost:9200/twitter/tweet/_mapping?pretty=true'
Run Code Online (Sandbox Code Playgroud)

4)发布一些推文,包含嵌套数据

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"
            }
        ]
    }
}'
Run Code Online (Sandbox Code Playgroud)

5)仅嵌套查询

curl -XGET localhost:9200/twitter/tweet/_search -d '{
    "query": {
        "nested" : {
            "path" : "db.oracle_props",
            "score_mode" : "avg",
            "query" : {
                "bool" : {
                    "must" : [
                        {
                            "term": {
                                "db.oracle_props.@name": "open_cursors"
                            }
                        },
                        {
                            "range": {
                                "db.oracle_props.@value": {
                                    "lte": 4000
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}';
Run Code Online (Sandbox Code Playgroud)

6)查询"Athena"和"Oracle"

curl -XGET localhost:9200/twitter/tweet/_search -d '{
    "query" : {
        "bool" : {
            "must" : [
                {
                    "match" : {"tweet.name" : "Athena"}
                },
                {
                    "match" : {"tweet.db.@type" : "Oracle"}
                }
            ]
        }
    }
}'
Run Code Online (Sandbox Code Playgroud)

7)结合前两个查询

curl -XGET localhost:9200/twitter/tweet/_search -d '{
    "query" : {
        "bool" : {
            "must" : [
                {
                    "match" : {"tweet.name" : "Athena"}
                },
                {
                    "match" : {"tweet.db.@type" : "Oracle"}
                },
                {
                    "nested" : {
                        "path" : "db.oracle_props",
                        "score_mode" : "avg",
                        "query" : {
                            "bool" : {
                                "must" : [
                                    {
                                        "term": {
                                            "db.oracle_props.@name": "open_cursors"
                                        }
                                    },
                                    {
                                        "range": {
                                            "db.oracle_props.@value": {
                                                "lte": 4000
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}'
Run Code Online (Sandbox Code Playgroud)

结果为

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 2.462332,
        "hits": [
            {
                "_index": "twitter",
                "_type": "tweet",
                "_id": "1",
                "_score": 2.462332,
                "_source": {
                    "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"
                            }
                        ]
                    }
                }
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @AbhishekSimon - 做一个多类型的领域.使其中一个长,一个字符串.这将使其为string contains指定正确的标记,并将long值进行比较.见,http://www.elasticsearch.org/guide/en/elasticsearch/reference/0.90/mapping-multi-field-type.html (2认同)