gpt4 book ai didi

ruby-on-rails - 如何在 Elasticsearch 中查询数组元素

转载 作者:行者123 更新时间:2023-12-03 00:11:12 27 4
gpt4 key购买 nike

这是我的模型在 flex 搜索中的索引

 {  
"_index":"cars",
"_type":"car",
"_id":"3275",
"_version":4,
"_score":1,
"_source":{
"category_id": 6,
"car_branches":[
{
"id":32,
"name":"Type1"
},
{
"id":33,
"name":"Type2"
},
{
"id":36,
"name":"Type3"
}
],

}
}

我可以用以下方式查询category_id
Car.__elasticsearch__.search query:{match:{category_id: 6}}
如何查询car_branches?我试过了
response = Car.__elasticsearch__.search query:{match:{car_branches:[id: 32]}}
我正在获取 Elasticsearch::Transport::Transport::Errors::BadRequest: [400]

最佳答案

您首先需要先删除索引并重新创建它。在执行此操作之前,您需要更改映射,并将car_branches字段设置为nested,如下所示:

  indexes :car_branches, type: 'nested' do
indexes :id
indexes :name

然后,您就可以像这样进行查询:
response = Car.__elasticsearch__.search query:{nested:{path: 'car_branches', query:{term:{'car_branches.id':[32]}}}}

关于ruby-on-rails - 如何在 Elasticsearch 中查询数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40624208/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com