嵌套对象上的ElasticSearch查询

时间:2017-08-02 11:49:05

标签: elasticsearch

我有以下文档结构:

           "IDNumberTypes": {
              "ID": [
                 {
                    "@IDType": "Company Identification No.",
                    "IDValue": [
                       {
                          "#text": "CompanyID"
                       }
                    ]
                 },
                 {
                    "@IDType": "Reg. Number",
                    "IDValue": [
                       {
                          "#text": "RegNumber"
                       }
                    ]
                 },
                 {
                    "@IDType": "Tax ID Number",
                    "IDValue": [
                       {
                          "#text": "TaxNumber"
                       }
                    ]
                 }
              ]
           }

我正在尝试编写一个与TaxNumber匹配的查询,但仅限于“国税号”的上下文。 在pseudoSQL中有类似的东西:

IDNumberTypes.ID.IDValue.#text="TaxNumber" WHERE IDNumberTypes.ID.@IDType="Tax ID Number"

这样做显然会导致返回包含“@IDType”对象的文档:“税号”

"query": {
  "bool": {
     "must": {
        "match": {
           "IDNumberTypes.ID.IDValue.#text": {
              "query": "TaxNumber",
              "operator": "and"
           }
        }
     },
     "filter": {
        "match": {
           "IDNumberTypes.ID.@IDType": {
              "query": "Tax ID Number",
              "operator": "and"
           }
        }
     }
  }
}

...但不确定我正在寻找的对象必须具有这种特定的结构:

{
 "@IDType": "Tax ID Number",
 "IDValue": [{
              "#text": "TaxNumber"
             }]
}

如何正确过滤/构建查询?

我已经通过添加适当的映射对其进行了整理:

{
   "mappings": {
     "doxx": {
       "properties": {
         "IDNumberTypes": {
           "properties": {
             "ID": {
               "type": "nested",
               "properties": {
                 "@IDType": {
                   "type": "string"
                 },
                 "IDValue": {
                   "type": "nested",
                   "properties": {
                     "#text": {
                       "type": "string"
                     },
                     "@IDnotes": {
                       "type": "string"
                     }
                   }
                 }
               }
             }
           }
         }
       }
     }
   }
 }

1 个答案:

答案 0 :(得分:0)

正确的映射:

{
   "mappings": {
     "doxx": {
       "properties": {
         "IDNumberTypes": {
           "properties": {
             "ID": {
               "type": "nested",
               "properties": {
                 "@IDType": {
                   "type": "string"
                 },
                 "IDValue": {
                   "type": "nested",
                   "properties": {
                     "#text": {
                       "type": "string"
                     },
                     "@IDnotes": {
                       "type": "string"
                     }
                   }
                 }
               }
             }
           }
         }
       }
     }
   }
 }