如何检查elasticsearch属性中是否存在值?

时间:2014-07-09 11:50:14

标签: json elasticsearch

我有一个带有系统环境的json对象。如果environment variable does not contain a variable name "JAVA_HOME" ,我需要列出该过滤器,如果JAVA_HOME's value is not present in PATH variable,我需要列出。这在Elasticsearch中是否可行?

下面是我的过滤器

PUT /eg/.percolator/2
{   
    "query": {
        "filtered": {
           "query": {
               "bool": {
                   "must": [
                      {
                          "match": {
                             "client.name": "Athena"
                          }
                      },
                      {
                          "match": {
                             "client.environment.variable.@name": "JAVA_HOME"
                          }
                      }
                    ]                   
               }               
           }
        }
    }  
}

我的文档

GET /eg/message/_percolate
{
    "doc": {
        "client": {
            "name": "Athena",
            "environment": {
                "variable": [
                    {
                        "@name": "JAVA_HOME",
                        "@value": "/home/vikrant/Linux/Sandra/java"
                    },
                    {
                        "@name": "PATH",
                        "@value": "/bin:/sbin:/usr/bin:/usr/sbin:/home/vikrant/Linux/FAS611:/home/vikrant/Linux/FAS611/odbc:/home/vikrant/Linux/Sandra/java/bin:/home/vikrant/Linux/Sandra/server/bin:.:/bin:/usr/bin:/sbin:/usr/sbin:/usr/bin/X11:/usr/local/bin:/usr/local/etc:/etc:/usr/etc:.:/databases/ORACLE/bin:/databases/SYBASE/OCS-15_0/bin:/databases/DB2/bin:/usr/odbc/bin"
                    }
                ]
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

由于您的文档已嵌套,因此首先需要nested mapping。这是我首次为您的文档创建的映射,使其能够正常工作:

{
  "nested_exp": {
    "properties": {
      "client": {
        "type": "nested",
        "properties": {
          "environment": {
            "type": "nested",
            "properties": {
              "variable": {
                "type": "nested",
                "properties": {
                  "@name": {
                    "type": "string"
                  },
                  "@value": {
                    "type": "string"
                  }
                }
              }
            }
          },
          "name": {
            "type": "string"
          }
        }
      }
    }
  }
}

然后我更改了您提供的查询,使其按预期工作:

{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "client",
                "query": {
                  "match": {
                    "client.name": "Athena"
                  }
                }
              }
            },
            {
              "nested": {
                "path": "client.environment.variable",
                "query": {
                  "match": {
                    "client.environment.variable.@name": "JAVA_HOME"
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

提出你的问题,它有两个部分。

对于第一部分,您可以将NOT过滤器与nested匹配过滤器一起使用。像这样:

{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "client",
                "query": {
                  "match": {
                    "client.name": "Athena"
                  }
                }
              }
            },
            {
              "filtered": {
                "query": {
                  "match_all": {}
                },
                "filter": {
                  "not": {
                    "filter": {
                      "nested": {
                        "path": "client.environment.variable",
                        "query": {
                          "match": {
                            "client.environment.variable.@name": "JAVA_HOME"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

我刚刚尝试使用我的本地Elasticsearch服务器,它就像一个魅力。

第二个似乎难以使用Elasticsearch实现,因为您要查询的内容类似于value of 1 variable should not be present in another variable's value