gpt4 book ai didi

indexing - Elastic Search - 具有数组类型和嵌套对象属性的复杂查询

转载 作者:行者123 更新时间:2023-12-03 06:22:38 24 4
gpt4 key购买 nike

鉴于以下映射,我需要获得符合以下条件的结果

  1. 名字、姓氏、出生日期完全匹配且 Active = true 或
  2. 名字、姓氏完全匹配,Active = true 且 1 封电子邮件(不包括可能的倍数)或
  3. 名字、姓氏完全匹配,Active = true 且 1 个旅行证件编号(不超过可能的倍数)

电子邮件和旅行证件可以引用项目集合。

    {
"profile":{
"properties":{

"date_of_birth":{
"type":"date",
"store":"no"
},
"first_name":{
"type":"string",
"store":"no"
},
"last_name":{
"type":"string",
"store":"no"
},
"email":{
"type":"string",
"store":"no"
},
"active":{
"type":"string",
"store":"no"
},
"travel_document":{
"properties" : {
"countryOfCitizenship" : {"type" : "string"},
"countryOfIssue" : {"type" : "string"},
"expirationDate" : {"type" : "date"},
"nationality" : {"type" : "string"},
"number" : {"type" : "string"},
"addressLines" : {"type": "string"},
"issuedForAreaCode" : {"type": "string"},
"type" : {"type": "string"}
}
}
}
}
}

有没有办法可以在elasticsearch中执行这种搜索?我可以用 Nested Queries 来做到这一点吗? ?

最佳答案

是的,你可以。

首先,回答您有关嵌套查询的问题:

如果您需要查询对象集合中同一对象中的多个字段(例如 travel_document.nationalitytravel_document.expirationDate,那么您需要将 travel_document 从类型 object 更改为类型 nested 并更改为使用嵌套查询。

在您给出的示例查询中,您没有表明您需要此功能。相反,您询问是否有 travel_document有一个值(value)。因此在这种情况下,您不需要使用嵌套功能。

(如果您认为将来可能需要对相关字段进行查询,那么您可能确实希望使用 nested 。您还可以设置 include_in_root 将嵌套对象索引为单独的 nested 对象并在主文档中)。

对于下面的查询,我假设 travel_document没有嵌套。

第二:您在名称字段中使用“完全匹配”。

默认情况下,会分析字符串字段,因此“Mary Jane”将被索引为术语 ['mary','jane']。如果您在该字段上运行查询来查找“Mary”,那么它将匹配,因为该字段确实包含“mary”。但是,这并不完全匹配。

如果要做精确匹配,那么需要将字段设为not_analyzed ,在这种情况下,“Mary Jane”将被索引为单个术语“Mary Jane”,并且“Mary”的查询将不匹配。缺点是在这种情况下您无法在名称字段上使用全文查询。

类似地,将电子邮件字段设置为 not_analyzed 可能更有意义(或者使用带有 keyword 标记生成器的自定义分析器 - 它不会标记字符串 - 和 lowercase 标记过滤器)。

在下面的查询中,我假设已分析您的姓名字段,但未分析您的电子邮件字段:

curl -XGET 'http://127.0.0.1:9200/my_index/properties/_search?pretty=1'  -d '
{
"query" : {
"filtered" : {
"query" : {
"bool" : {
"must" : [
{
"match_phrase" : {
"first_name" : "mary jane"
}
},
{
"match_phrase" : {
"last_name" : "smith"
}
}
]
}
},
"filter" : {
"and" : [
{
"term" : {
"active" : 1
}
},
{
"or" : [
{
"term" : {
"date_of_birth" : "1980-01-01"
}
},
{
"terms" : {
"email" : [
"mary@smith.com",
"maryjane@smith.com"
]
}
},
{
"terms" : {
"travel_document.number" : [
"1234",
1235
]
}
}
]
}
]
}
}
}
}
'

关于indexing - Elastic Search - 具有数组类型和嵌套对象属性的复杂查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14598711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com