首页 > 解决方案 > 无法在 Elastic Search 中检索嵌套对象

问题描述

这里是一个 ELK 菜鸟,最后一分钟有 ELK 任务交给我。

我们正在添加一个名为车辆索引的额外数据prospects,以便用户可以搜索它。我可以将其添加prospects到索引中,现在我无法prospects在车辆索引中获取嵌套的 obj。我正在使用 Elastic Search & Kibana v6.8.11 和 elastic-search-rails gem 并检查了有关嵌套对象的文档。根据文档,我的搜索方法看起来是正确的。希望一些专家指出这里有什么问题,如果您需要更多信息,请告诉我。

这是假设索引 obj -

      {
        "_index" : "vehicles",
        "_type" : "_doc",
        "_id" : "3MZBxxxxxxx",
        "_score" : 0.0,
        "_source" : {
          "vin" : "3MZBxxxxxxx",
          "make" : "mazda",
          "model" : "mazda3",
          "color" : "unknown",
          "year" : 2018,
          "vehicle" : "2018 mazda mazda3",
          "trim" : "grand touring",
          "estimated_mileage" : null,
          "dealership" : [
            209
          ],
          "current_owner_group_id" : null,
          "current_owner_customer_id" : null,
          "last_service_date" : null,
          "last_service_revenue" : null,
          "purchase_type" : [ ],
          "in_service_date" : null,
          "deal_headers" : [ ],
          "services" : [ ],
          "customers" : [ ],
          "salesmen" : null,
          "service_appointments" : [ ],
          "prospects" : [
            {
              "first_name" : "Kammy",
              "last_name" : "Maytag",
              "name" : "Kammy Maytag",
              "company_name" : null,
              "emails" : [ ],
              "phone_numbers" : [ ],
              "address" : "31119 field",
              "city" : "helen",
              "state" : "keller",
              "zip" : "81411",
              "within_dealership_aoi_region" : true,
              "dealership_ids" : [
                209
              ],
              "dealership_dppa_protected_ids" : [
                209
              ],
              "registration_id" : 12344,
              "id" : 1054,
              "prospect_source_id" : "12344",
              "type" : "Prospect"
            }
          ]
        }
      }
    ]
  }
}

这是我试图得到它的方式 -

 GET /vehicles/_search
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": [
        { "term": { "dealership": "209" } },
        {
          "nested": {
            "path": "prospects",
            "query": {
              "bool": {
                "must": [
                  { "term": { "prospects.first_name": "Kammy" } },
                  { "term": { "prospects.dealership": "209" } },
                  { "term": { "prospects.type": "Prospect" } }
                ]
              }
            }
          }
        },
        { "bool": { "must_not": { "term": { "purchase_type": "Wholesale" } } } }
      ]
    }
  },
  "sort": [{ "_doc": { "order": "asc" } }]
}

标签: elasticsearchelastic-stackelkelasticsearch-rails

解决方案


我看到嵌套查询有两个问题:

  1. 您正在查询prospects.dealership,但示例文档仅显示prospects.dealership_ids. 将查询更改为目标prospects.dealership_ids
  2. 更重要的是,您在and上使用term查询。我假设您的索引映射没有将它们定义为这意味着它们很可能是小写的(出于此处解释的原因),但正在寻找精确匹配。 prospects.first_nameprospects.typekeywordsterm
    • 选项 1:使用match而不是term.
    • 选项 2:更改prospects.first_nameprospects.first_name.keyword并对.type.

推荐阅读