1

This is the index of my model in the elastic search

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

       }
    }

I can query category_id with

Car.__elasticsearch__.search query:{match:{category_id: 6}}

How do I query for car_branches? I tried this

response = Car.__elasticsearch__.search query:{match:{car_branches:[id: 32]}}

I am getting Elasticsearch::Transport::Transport::Errors::BadRequest: [400]

gates
  • 4,465
  • 7
  • 32
  • 60
  • What is the mapping of the `car_branches` field? Run this `curl -XGET localhost:9200/cars/_mapping/car` and update your question with the output you get. – Val Nov 16 '16 at 04:46
  • https://gist.github.com/anonymous/e90d5c881635f682f3a25b5235dcf91a @Val – gates Nov 16 '16 at 04:52
  • Can you show your mapping definition of your cars model (in ruby)? – Val Nov 16 '16 at 04:58
  • I can't understand you .. can you explain please – gates Nov 16 '16 at 05:01
  • How does your `Car` ruby model look like? – Val Nov 16 '16 at 05:02
  • It is just like a normal model, and it is working fine with category_id search, Just wanted to know how to query for key values inside arrays.. I am sorry there is no car, it's different, I am just making up own Car for a similar model I have – gates Nov 16 '16 at 05:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128221/discussion-between-gates-and-val). – gates Nov 16 '16 at 05:47

1 Answers1

0

You first need to delete your index first and recreate it. Before doing so, you need to change your mapping and make the car_branches field nested, like this:

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

Then you'll be able to make the query your want like this:

response = Car.__elasticsearch__.search query:{nested:{path: 'car_branches', query:{term:{'car_branches.id':[32]}}}}
Val
  • 207,596
  • 13
  • 358
  • 360
  • Mapping like this ```def as_indexed_json(options={}) as_json( only: [:category_id], include: { car_branches: {only: [:id, :name]} } ) end``` – gates Nov 16 '16 at 05:50