2014-07-09 55 views
0

我有一个系统环境的json对象。如果environment variable does not contain a variable name "JAVA_HOME"也如果JAVA_HOME's value is not present in PATH variable,我需要列出该过滤器。这在Elasticsearch中可能吗?下面如何检查elasticsearch属性中是否存在一个值?

是我的过滤器

PUT /eg/.percolator/2 
{ 
    "query": { 
     "filtered": { 
      "query": { 
       "bool": { 
        "must": [ 
         { 
          "match": { 
          "client.name": "Athena" 
          } 
         }, 
         { 
          "match": { 
          "[email protected]": "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" 
        } 
       ] 
      } 
     } 
    } 
} 
+0

你当前的查询是否工作?你的文档是嵌套的。 – vaidik

回答

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": { 
        "[email protected]": "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": { 
          "[email protected]": "JAVA_HOME" 
          } 
         } 
         } 
        } 
        } 
       } 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 

我刚刚尝试在我的本地Elasticsearch服务器上,它的作品就像一个魅力。

第二个似乎很难用Elasticsearch实现,因为你要查询的东西就像value of 1 variable should not be present in another variable's value

+0

可以请你帮助我[相关的问题](http://stackoverflow.com/questions/24690293/how-to-match-an-array-value-by-its-key-in-a-key-value -pair-elasticsearch阵列)?谢谢 – abi1964