搜索嵌套数组对象时Elasticsearch问题不起作用

时间:2016-12-13 13:20:39

标签: elasticsearch spring-data-elasticsearch

我目前正在使用ES版本2.3.5。我有一个由springboot REST项目自动创建的ES映射:

{
  "bool" : {
    "should" : {
      "query_string" : {
        "query" : "(addresses.address1:Queensland)",
        "fields" : [ "type", "name", "companyNumber", "status", "parent.id", "parent.name", "parent.type", "addresses.id", "addresses.unit", "addresses.address1", "addresses.address2", "addresses.address3", "addresses.town", "addresses.state", "addresses.postcode", "addresses.country", "contactMethods.type", "contactMethods.value", "contactMethods.description" ],
        "default_operator" : "or",
        "analyze_wildcard" : true,
        "lenient" : true
      }
    }
  }
}

搜索服务类构建查询。例如,通过发出" addresses.address1:Queensland"来查询地址字段。在端点上,查询将是:

{
    "page": 1,
    "pageSize": 10,
    "totalPages": 1,
    "totalElements": 1,
    "data": [
        {
            "id": "1",
            "timeCreated": "2016-09-01T14:52:44Z",
            "timeUpdated": "2016-09-01T15:25:46Z",
            "type": "BUSINESS",
            "name": "John Doe",
            "companyNumber": "1000000002",
            "status": "PENDING",
            "addresses": [
                {
                    "id": "1",
                    "address1": "Queensland Street",
                    "address2": "Casa Fuego",
                    "town": "New Kingslanding",
                    "state": "QA",
                    "postcode": "2222",
                    "country": "AU",
                    "longitude": 151.080739,
                    "latitude": -33.770029
                }
            ],
            "contactMethods": [
                {
                    "type": "MOBILE",
                    "value": "0123456789"
                },
                {
                    "type": "EMAIL",
                    "value": "john@doe.com"
                }
            ]
        }
    ]
}

返回正确的文档。回复示例:

{{1}}

但是,如果我通过" contactMethods.type查询contactMethods字段:EMAIL"或" contactMethods.type:MOBILE"甚至" contactMethods.type:*"甚至小写"电子邮件"和"移动"返回空或0文档。

是什么导致这种情况?

1 个答案:

答案 0 :(得分:0)

嵌套属性被索引为不同的文档。需要使用嵌套运算符。请参阅https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html