1

这是我的模型在弹性搜索中的索引

 {  
       "_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]

4

1 回答 1

0

您首先需要先删除索引并重新创建它。在这样做之前,您需要更改映射并创建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]}}}}
于 2016-11-16T05:16:19.033 回答