嵌套字段上的弹性搜索不起作用

时间:2015-02-10 13:22:16

标签: elasticsearch nested elasticsearch-plugin

我们在ES中使用以下设置:

#Settings for elastic search
fos_elastica:
    clients:
        default: 
          host: %elastic_server_host%
          port: %elastic_server_port%
          headers: { Authorization: 'Basic %elastic_server_credential%' }
    indexes:
        organizations:
            settings:
                index:
                    analysis:
                        analyzer:
                            classic_analyser:
                                type: custom
                                tokenizer: lowercase
                                filter   : [my_snow]
                            simple_analyser:
                                type: custom
                                tokenizer: lowercase
                            email_analyser:
                                type: custom
                                tokenizer: uax_url_email    
                        filter:
                            my_snow:
                                type : "snowball"
                                language : "English"
            client: default
            types:
                packages:
                    mappings:
                        id:
                          type: integer
                        trackingCode:
                            type: string
                            index: not_analyzed
                        damaged:
                          type: boolean
                        urgent:
                          type: boolean
                        perishable:
                          type: boolean
                        checkedOut:
                          type: date
                        created:
                          type: date
                        confirmationType:
                          type: integer
                        forwardedTrackingCode   :
                          type: string
                          index: not_analyzed
                        firstNote   : { type: string, analyzer: classic_analyser }
                        secondNote  : { type: string, analyzer: classic_analyser }
                        deleted:
                          type: date
                        recipientId:
                            type: nested
                            properties:
                                id:
                                  type: integer
                                internalId: { type: string, analyzer: simple_analyser }
                                firstName:  { type: string, analyzer: simple_analyser }
                                lastName:  { type: string, analyzer: simple_analyser }
                                email: { type: string, analyzer: email_analyser }
                                mailbox:  { type: string, analyzer: simple_analyser }
                                phoneNumber:
                                  type: string
                                  index: not_analyzed
                                mobileNumber:
                                  type: string
                                  index: not_analyzed
                                jointAccountHolder:  { type: string, analyzer: simple_analyser }
                                deliveryMode: ~
                                deleted:
                                   type: date
                                address:
                                   type: nested
                                   properties:
                                     address:
                                       type: nested
                                       properties:
                                         country:
                                           type: string
                                           index: not_analyzed
                                         organisationName:
                                           type: string
                                           index: not_analyzed
                                         administrativeArea:
                                           type: string
                                           index: not_analyzed
                                         locality:
                                           type: string
                                           index: not_analyzed
                                         postalCode:
                                           type: string
                                           index: not_analyzed
                                         thoroughfare:
                                           type: string
                                           index: not_analyzed
                                         premise:
                                           type: string
                                           index: not_analyzed
                        carrierId:
                            type: nested
                            properties:
                              id:
                                type: integer 
                              slug:
                                type: string
                                index: not_analyzed
                              name:
                                type: string
                                index: not_analyzed
                              phone:
                                type: string
                                index: not_analyzed
                              otherName:
                                type: string
                                index: not_analyzed
                              webUrl:
                                type: string
                                index: not_analyzed
                    persistence:
                        driver: orm
                        model: MyCode\PackageBundle\Entity\Packages
                        finder: ~
                        provider: ~
                        listener: ~
                        repository: MyCode\PackageBundle\Entity\ElasticaPackagesRepository

                recipients:
                    mappings:
                        id:
                          type: integer
                        internalId: { type: string, analyzer: simple_analyser }
                        firstName:  { type: string, analyzer: simple_analyser }
                        lastName:  { type: string, analyzer: simple_analyser }
                        email: { type: string, analyzer: email_analyser }
                        mailbox:  { type: string, analyzer: simple_analyser }
                        phoneNumber:
                          type: string
                          index: not_analyzed
                        mobileNumber:
                          type: string
                          index: not_analyzed
                        jointAccountHolder:  { type: string, analyzer: simple_analyser }
                        deliveryMode: ~
                        deleted:
                          type: date
                        address:
                           type: nested
                           properties:
                             address:
                               type: nested
                               properties:
                                 country:
                                   type: string
                                   index: not_analyzed
                                 organisationName:
                                   type: string
                                   index: not_analyzed
                                 administrativeArea:
                                   type: string
                                   index: not_analyzed
                                 locality:
                                   type: string
                                   index: not_analyzed
                                 postalCode:
                                   type: string
                                   index: not_analyzed
                                 thoroughfare:
                                   type: string
                                   index: not_analyzed
                                 premise:
                                   type: string
                                   index: not_analyzed
                    persistence:
                        driver: orm
                        model: MyCode\RecipientBundle\Entity\Recipient
                        finder: ~
                        provider: ~
                        listener: ~
                        repository: MyCode\RecipientBundle\Entity\ElasticaRecipientRepository

简而言之,我们使用两种不同的类型---包装和包装。收件人。此外,与包关联的收件人也存储在ES中。

现在,如果我们搜索:

{
  "query": {
    "query_string": {
      "query": "james@example.com",
      "fields": [
        "packages.recipientId.email"
      ],
      "default_operator": "OR"
    }
  },
  "size": "50",
  "from": "0",
  "sort": {
    "id": {
      "order": "asc"
    }
  }
} 

虽然该特定收件人确实存在,但ES并未返回任何记录。 但如果我们搜索如下:

{
  "query": {
    "query_string": {
      "query": "james@example.com",
      "fields": [
        "recipients.email"
      ],
      "default_operator": "OR"
    }
  },
  "size": "50",
  "from": "0",
  "sort": {
    "id": {
      "order": "asc"
    }
  }
}

ES正在返回记录。 我不明白为什么搜索在上述两种情况下表现不同。

您能否建议我们需要更改/更正哪些内容,以便在这两种情况下都能进行搜索?

1 个答案:

答案 0 :(得分:2)

您必须执行嵌套查询才能查询嵌套字段:

"由于嵌套文档始终被屏蔽到父文档,因此嵌套文档永远不能在嵌套查询的范围之外访问。" http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html