在ElasticSearch中的嵌套对象数组上应用``存在''查询

时间:2018-08-09 04:47:12

标签: elasticsearch

我在elasticsearch complete / user中有一个具有以下结构的json文档:

"_source":{
"experiences":[
{experience 1}, {experience 2}, ... , {experience n}

]

}

experience数组中Experience对象的结构如下:

{
    "company":

    {
        "industry": industry of the comany,
        "name": company name,
​        ... other fields
    },
    "start_date": date when person joined company,
    ... other fields
}

我想找到所有这样的文档,其中经验数组中的任何经验都没有company.industry字段,即所有经验都必须缺少该字段。我可以使用任何查询吗?

预先感谢

1 个答案:

答案 0 :(得分:0)

尝试一下:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"/>
	<link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet'>
	<link rel="stylesheet" type="text/css" href="home.css">
	
	<title>Home Page</title>
	<style type="text/css">

	</style>
</head>
<body>
	<div class="header">
		<div class="logo">
			<a href="home.html"><img id="logo"src="logo.png" alt="Logo"></a>
		</div>
		<div class="links">			
			<a href="home.html">Home</a>
			<a href="home.html">Offers</a>
			<a href="home.html">Login</a>
			<a href="register.html">Register</a>
			<a href="home.html">Contact</a>
			<a href="home.html">About</a>
		</div>
	</div>
	<div clear="both"></div>
	<div class="content">
	
	</div>
<script type="text/javascript" src="home.js"></script>
</body>
</html>

此查询将返回所有文档,其中经验数组所有经验 company.industry 缺失

示例:

{
  "query": {
    "bool": {
      "must_not": [
        {
         "exists": {"field" : "experiences.company.industry"}
        }
      ]
    }
  }
}

此查询将不会返回的内容:

"_source": {
  "experiences": [
    {
      "company": {
        "name": "company name"
      }
    },
    {
      "company": {
        "industry": null,
        "name": "company name"
      }
    }
  ]
}

让我知道是否有帮助。