具有多个值的Elasticsearch术语过滤器

时间:2016-04-14 03:02:20

标签: elasticsearch

我有以下映射:

PUT /files
{
   "mappings": {
      "file": {
         "properties": {
            "FileID": {
               "type": "integer"
            },
            "FolderID": {
               "type": "integer"
            }
         }
      }
   }
}

我的数据是:

PUT /clients/client/1
{
    "id":"1",
    "name":"Joe Doe", 
    "FolderIDs":["577173","245340","777035"],
    "Emails" : ["some@email.com", "other@email.com"]
}



PUT /files/file/1
{
    "FileID": "10550",
    "FolderID" : "577173"
}

我的查询是:

GET /_search
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "terms": {
               "FolderID": {
                  "index": "clients",
                  "type": "client",
                  "id": "1",
                  "path": "FolderIDs"
               }
            }
         }
      }
   }
}

这将返回ID为10550的文件,非常棒。我的问题是如何在电子邮件字段中执行此操作,其中包含电子邮件列表,例如。

映射:

PUT /emails
{
   "mappings": {
      "email": {
         "properties": {
            "EmailID": {
               "type": "integer"
            },
            "ADDRESS_FROM": {
               "type": "string",
               "index" : "not_analyzed"
            }
      } 
   } 
}

数据:

PUT /emails/email/1
{
    "EmailID": "8335",
    "ADDRESS_FROM" : "random@email.com user@email.com"
}

如何构建一个查询,从客户端电子邮件字段返回ADDRESS_FROM中没有任何电子邮件的电子邮件?

客户1有[" some@email.com" ;," other@email.com"] 所以返回电子邮件1,因为ADDRESS_FROM不包含任何客户端1电子邮件(" random@email.com user@email.com")。

我尝试了类似的东西(不起作用):

GET /_search
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
   "filter": {
      "bool": {
         "must_not": [
            {
               "terms": {
                  "ADDRESS_FROM": {
                     "index": "clients",
                     "type": "client",
                     "id": "1",
                     "path": "Emails"
                  }
               }
            }
         ]
      }
   }
}

1 个答案:

答案 0 :(得分:1)

确保客户端索引中的Emails字段设置为not_analyzed

理想情况下,AddressForm字段应该是一组电子邮件。 即 "ADDRESS_FROM" : ["random@email.com", "user@email.com"]

而不是

"ADDRESS_FROM" : "random@email.com user@email.com"

如果无法更改文档结构,则需要AddressForm使用PUT /clients { "mappings": { "client": { "properties": { "FolderIDs": { "type": "integer" }, "Emails" : { "type": "string", "index" : "not_analyzed" } } } } } PUT /clients/client/1 { "id":"1", "name":"Joe Doe", "FolderIDs":["577173","245340","777035"], "Emails" : ["some@email.com", "other@email.com"] } PUT /clients/client/2 { "id":"1", "name":"Joe Doe", "FolderIDs":["577173","245340","777035"], "Emails" : ["random@email.com", "other@email.com"] } PUT /emails { "mappings": { "email": { "properties": { "EmailID": { "type": "integer" }, "ADDRESS_FROM": { "type": "string", "analyzer": "whitespace" } } } } } PUT /emails/email/1 { "EmailID": "8335", "ADDRESS_FROM" : "random@email.com user@email.com" }

以下示例说明了这一点:

POST emails/_search
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "bool": {
               "must_not": [
                  {
                     "terms": {
                        "ADDRESS_FROM": {
                           "index": "clients",
                           "type": "client",
                           "id": "1",
                           "path": "Emails"
                        }
                     }
                  }
               ]
            }
         }
      }
   }
}

 "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "emails",
            "_type": "email",
            "_id": "1",
            "_score": 1,
            "_source": {
               "EmailID": "8335",
               "ADDRESS_FROM": "random@email.com user@email.com"
            }
         }
      ]
   }

示例查询1:

POST emails/_search
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "bool": {
               "must_not": [
                  {
                     "terms": {
                        "ADDRESS_FROM": {
                           "index": "clients",
                           "type": "client",
                           "id": "2",
                           "path": "Emails"
                        }
                     }
                  }
               ]
            }
         }
      }
   }
}

  "hits": {
      "total": 0,
      "max_score": null,
      "hits": []
   }

示例Query2(0次点击):

ul {
	background: rgba(37,39,44,.80);
	list-style-type:none;
	margin : 0;
	padding: 0;
	overflow:visible;
	font-family:Kreon;
	min-width:1349px;
	width:100%;
	top:0;
	position:fixed;
	border-bottom: 5px solid #22A85B;
	
}
li {
	float : right;
}

li a{
	display:block;
	color : white;
	text-align:center;
	padding: 15px 20px 15px 20px;
	text-decoration :none;
	border-right:0.5px solid #22a85b;
	transition: 0.3s all;
}
li a:hover {
	background:#22a85b;
}
.dropdown-arrow {
	position:relative;
	top:1px;
	display:inline-block;
	width:0;
	height:0;
	margin-left:5px;
	border: 4px solid transparent;
	border-top-color:#fff;
}
.dropdown {
	float:right;
	position:relative;
	display:inline-block;
	color: white;
	text-align:center;
	padding: 15px 20px 15px 20px;
	cursor:pointer;
	border-right:1px solid #22A85B;
}
.dropdown:hover{
	background:#22a85b;
}
.dropdown:focus {
	pointer-events:none;
	background:#eee;
	color:#000;
}
.dropdown-content {
	position:absolute;
	background-color:#eee;
	min-width:150px;
	z-index:1;
	opacity:0;
	visibility:hidden;
	transition: 0.2s linear;
	margin-top:36px;
	margin-left:-50px;
	border : 1px solid #bbb;
	border-top:none;
}
.dropdown-content a{
	text-align:right;
	color: black;
	font-family:kreon;
	text-decoration:none;
	padding: 10px 15px 10px 15px;
	display:block;
	transition: 0.1s linear;
	border-bottom: 1px solid #20D23F;
}
.dropdown-content a:hover{
	background: #22a85b;
	color:#fff;
}
.dropdown:focus .dropdown-content{
	opacity:1;
	visibility:visible;
	pointer-events:auto;
}
.dropdown:focus .dropdown-arrow{
	border: 4px solid transparent;
	border-bottom-color:#000;
	margin-bottom : 4px;
}