elasticsearch,Indexing,elasticsearch" /> elasticsearch,Indexing,elasticsearch" />

Indexing 弹性搜索-具有数组类型和嵌套对象属性的复杂查询

Indexing 弹性搜索-具有数组类型和嵌套对象属性的复杂查询,indexing,elasticsearch,Indexing,elasticsearch,给定以下映射,我需要得到符合以下条件的结果 名字、姓氏、出生日期和活动日期的精确匹配=真或 名字、姓氏、Active=true和1封电子邮件(可能的倍数或倍数)完全匹配 名字、姓氏、Active=true和1个traveldocument数字的精确匹配(可能的倍数除外) 其中电子邮件和旅行证件可以指的是一系列物品 { "profile":{ "properties":{ "date_of_birth":{

给定以下映射,我需要得到符合以下条件的结果

  • 名字、姓氏、出生日期和活动日期的精确匹配=真或
  • 名字、姓氏、Active=true和1封电子邮件(可能的倍数或倍数)完全匹配
  • 名字、姓氏、Active=true和1个traveldocument数字的精确匹配(可能的倍数除外)
  • 其中电子邮件和旅行证件可以指的是一系列物品

        {
        "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中执行这种搜索?我可以用这个吗?

    是的,你可以

    首先,要回答有关嵌套查询的问题,请执行以下操作:

    如果需要在一组对象(例如
    travel\u document.national
    travel\u document.expirationDate
    中查询同一对象中的多个字段,则需要将
    travel\u document
    类型从
    OBJECT
    更改为
    nested
    ,并使用嵌套查询

    在您给出的示例查询中,您没有显示您需要此功能。相反,您会询问任何
    travel\u document
    是否有值。因此,在这种情况下,您不需要使用嵌套功能

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

    对于下面的查询,我假设
    travel\u document
    没有嵌套

    第二:在名称字段中使用“精确匹配”

    默认情况下,将分析字符串字段,因此“Mary Jane”将作为术语['Mary','Jane']进行索引。如果在该字段上运行查询以查找“Mary”,则该字段将匹配,因为该字段确实包含“Mary”。但是,这不是完全匹配

    如果要进行精确匹配,则需要使字段
    不被分析
    ,在这种情况下,“Mary Jane”将被索引为单个术语“Mary Jane”,而对“Mary”的查询将不匹配。缺点是,在这种情况下,无法对name字段使用全文查询

    类似地,不分析电子邮件字段可能更有意义(或者使用带有
    关键字
    标记器(不标记字符串)和
    小写
    标记过滤器的自定义分析器)

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

    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
                               ]
                            }
                         }
                      ]
                   }
                ]
             }
          }
       }
    }
    '
    

    谢谢DrTech这正是我所需要的!谢谢你彻底的回答!只有一个挑剔:是的,我们可以。