【问题标题】:How to check if a value does not exist in elasticsearch attribute?如何检查elasticsearch属性中是否不存在值?
【发布时间】:2016-12-21 22:53:38
【问题描述】:

我有一个带有系统环境的 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"
                    }
                ]
            }
        }
    }
}

【问题讨论】:

  • 您当前的查询是否正常工作?您的文档是嵌套的。

标签: json elasticsearch


【解决方案1】:

您首先需要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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-01-15
  • 1970-01-01
  • 2013-08-28
  • 2016-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
相关资源
最近更新 更多