在elasticsearch上搜索嵌套对象的字段

时间:2016-08-29 11:03:18

标签: spring elasticsearch spring-data-elasticsearch

我在ES 1.7.3上有这个映射:

{
   "customer": {
      "aliases": {},
      "mappings": {
         "customer": {
            "properties": {
               "addresses": {
                  "type": "nested",
                  "include_in_parent": true,
                  "properties": {
                     "address1": {
                        "type": "string"
                     },
                     "address2": {
                        "type": "string"
                     },
                     "address3": {
                        "type": "string"
                     },
                     "country": {
                        "type": "string"
                     },
                     "latitude": {
                        "type": "double",
                        "index": "not_analyzed"
                     },
                     "longitude": {
                        "type": "double",
                        "index": "not_analyzed"
                     },
                     "postcode": {
                        "type": "string"
                     },
                     "state": {
                        "type": "string"
                     },
                     "town": {
                        "type": "string"
                     },
                     "unit": {
                        "type": "string"
                     }
                  }
               },
               "companyNumber": {
                  "type": "string"
               },
               "id": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "name": {
                  "type": "string"
               },
               "status": {
                  "type": "string"
               },
               "timeCreated": {
                  "type": "date",
                  "format": "dateOptionalTime"
               },
               "timeUpdated": {
                  "type": "date",
                  "format": "dateOptionalTime"
               }
            }
         }
      },
      "settings": {
         "index": {
            "refresh_interval": "1s",
            "number_of_shards": "5",
            "creation_date": "1472372294516",
            "store": {
               "type": "fs"
            },
            "uuid": "RxJdXvPWSXGpKz8pdcF91Q",
            "version": {
               "created": "1050299"
            },
            "number_of_replicas": "1"
         }
      },
      "warmers": {}
   }
}

spring应用程序生成此查询:

{
   "query": {
      "bool": {
         "should": {
            "query_string": {
               "query": "(addresses.\\*:sample* AND NOT status:ARCHIVED)",
               "fields": [ 
                   "type",
                    "name",
                    "companyNumber",
                    "status",
                    "addresses.unit",
                    "addresses.address1",
                    "addresses.address2",
                    "addresses.address3",
                    "addresses.town",
                    "addresses.state",
                    "addresses.postcode",
                    "addresses.country"
                ],
               "default_operator": "or",
               "analyze_wildcard": true
            }
         }
      }
   }
}

“地址。*:sample *”是唯一的输入。

"query": "(sample* AND NOT status:ARCHIVED)"

上面的代码可以工作,但会搜索客户对象的所有字段。 由于我只想搜索地址字段,因此我使用了“地址。*”

查询仅在地址对象的字段属于字符串类型且在地址对象上添加双重类型的经度纬度字段之前有效。现在由于这两个新字段而发生错误。

错误:

Parse Failure [Failed to parse source [{
   "query": {
     "bool": {
        "should": {
            "query_string": {
               "query": "(addresses.\\*:sample* AND NOT status:ARCHIVED)",
                  "fields": [ 
                      "type",
                      "name",
                      "companyNumber","country",
                      "state",
                      "status",
                      "addresses.unit",
                      "addresses.address1",
                      "addresses.address2",
                      "addresses.address3",
                      "addresses.town",
                      "addresses.state",
                      "addresses.postcode",
                      "addresses.country",
                  ],
                  "default_operator": "or",
                  "analyze_wildcard": true
              }
          }
      }
    }
  }
]]

NumberFormatException[For input string: "sample"

有没有办法只使用地址。* 搜索嵌套对象中的“字符串”字段?

1 个答案:

答案 0 :(得分:0)

解决方案是添加" lenient":true。根据文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

lenient - 如果设置为true将导致忽略基于格式的失败(如向数字字段提供文本)。