ElasticSearch查询数组中的范围OR缺失

时间:2017-08-14 14:11:14

标签: elasticsearch nest

我正在尝试创建一个查询,该查询将查询以下映射,以查找具有"句点"要么匹配特定日期,要么缺少它的值(具有空值)。 请注意我正在使用第三方数据库,因此无法更改映射。如果示例数据和映射很大,请耐心等待。我已经尝试过切掉所有不必要的东西了。

{
   "EXAMPLE":{
      "mappings":{
         "company":{
            "properties":{
               "CompanyData":{
                  "properties":{
                     "participantRelations":{
                        "type":"nested",
                        "include_in_parent":true,
                        "properties":{
                           "participant":{
                              "type":"nested",
                              "include_in_parent":true,
                              "properties":{
                                 "unitNumber":{
                                    "type":"long"
                                 }
                              },
                              "organizations":{
                                 "properties":{
                                    "memberData":{
                                       "type":"nested",
                                       "include_in_parent":true,
                                       "properties":{
                                          "attributes":{
                                             "properties":{
                                                "values":{
                                                   "properties":{
                                                      "period":{
                                                         "properties":{
                                                            "validFrom":{
                                                               "type":"date",
                                                               "format":"dateOptionalTime"
                                                            },
                                                            "validTo":{
                                                               "type":"date",
                                                               "format":"dateOptionalTime"
                                                            }
                                                         }
                                                      }
                                                      "value":{
                                                         "type":"string"
                                                      }
                                                   }
                                                }
                                             }
                                          }
                                       }
                                    }
                                 }
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
}

以下是一些示例数据。我已经将它从它来自的(丹麦语)来源翻译过来,所以任何轻微的错误拼写都只是错误的类型。

 { 
   "company": {
       "companyData":{
          "participantRelations":[
             {
                "participant":{
                   "unitNumber":4003857309
                },
                "organizations":[
                   {
                      "memberData":[
                         {
                            "attributtes":[
                               {
                                  "values":[
                                     {
                                        "value":"chairman",
                                        "period":{
                                           "validFrom":"2014-10-01",
                                           "validTo":"2016-08-11"
                                        }
                                     }
                                  ]
                               },
                               {
                                  "values":[
                                     {
                                        "value":"generalassembly",
                                        "period":{
                                           "validFrom":"2014-10-01",
                                           "validTo":"2016-08-11"
                                        }
                                     }
                                  ]
                               }
                            ]
                         }
                      ]
                   },
                   {
                      "memberData":[
                         {
                            "attributes":[
                               {
                                  "values":[
                                     {
                                        "value":"chairman",
                                        "period":{
                                           "validFrom":"2016-08-16",
                                           "validTo":"2017-06-08"
                                        }
                                     },
                                     {
                                        "value":"boardmember",
                                        "period":{
                                           "validFrom":"2017-06-09",
                                           "validTo":null
                                        }
                                     }
                                  ]
                               },
                               {
                                  "values":[
                                     {
                                        "value":"generalassembly",
                                        "period":{
                                           "validFrom":"2016-08-16",
                                           "validTo":"2017-06-08"
                                        }
                                     },
                                     {
                                        "value":"generalassembly",
                                        "period":{
                                           "validFrom":"2017-06-09",
                                           "validTo":null
                                        }
                                     }
                                  ]
                               }
                            ]
                         }
                      ]
                   }
                ]
             }
          ]
       }
    }
}

我想要做的事情就像下面的查询一样,它不能正常工作,因为它有一些我不知道的原因无法处理的情况。它需要做的是在特定日期查找任何company.participantRelations.organizations.memberData.attributes.values.period.validTo,或者如果日期为null。现在我知道ES中的空值是时髦的,但我知道日期属性将始终存在,但如果还没有日期,则validTo将设置为null。

此外,它还需要嵌套在组织上,因为我需要一个特定的unitNumber才能出现。

{
   "query":{
      "nested":{
         "filter":{
            "bool":{
               "must":[
                  {
                     "nested":{
                        "filter":{
                           "bool":{
                              "must":[
                                 {
                                    "bool":{
                                       "should":[
                                          {
                                             "range":{
                                                "company.companyData.participantRelations.organizations.memberData.attributtes.values.period.validTo":{
                                                   "gte":"2017-08-14T15:23:11.011"
                                                }
                                             }
                                          },
                                          {
                                             "missing":{
                                                "field":"company.companyData.participantRelations.organizations.memberData.attributtes.values.period.validTo"
                                             }
                                          }
                                       ]
                                    }
                                 }
                              ]
                           }
                        },
                        "path":"company.companyData.participantRelations.organizations.memberData"
                     }
                  },
                  {
                     "term":{
                        "company..companyData.participantRelations.participant.unitNumber":4003857309
                     }
                  }
               ]
            }
         },
         "path":"company.companyData.participantRelations"
      }
   }
}

此查询有两种情况:

  • 如果值列表中只有一个条目,并且它的有效日期为空
  • 其中validTo日期大于或等于我的日期限制。

如果有两个条目似乎不起作用,第一个条目的日期早于我的限制,第二个条目的值为空(如示例所示)。

我意识到这有点令人费解,但是对于数据库,我只是查询它的方式。我希望我已经足够简化了你的问题。

提前致谢。

0 个答案:

没有答案
相关问题