问题描述
这里是一个 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" } }]
}
解决方法
我发现嵌套查询有两个问题:
- 您正在查询
prospects.dealership
,但示例文档仅显示prospects.dealership_ids
。将查询更改为目标prospects.dealership_ids
。 - 更重要的是,您正在对
term
和prospects.first_name
使用prospects.type
查询。我假设您的索引映射没有将它们定义为keywords
,这意味着它们很可能是小写的(出于 explained here 的原因),但term
正在寻找完全匹配。- 选项 1:使用
match
而不是term
。 - 选项 2:更改
prospects.first_name
→prospects.first_name.keyword
并对.type
执行相同操作。
- 选项 1:使用