Search code examples
ruby-on-railsrubyelasticsearchsearchkick

Searchkick, search by parents names


I'm using searchkick gem, which provides to my very powerful searching.

When I type this for my Items:

Item.search 'computer'

It will return me Item models which has this string in its attributes.

If I have Item objects with names SuperCalc1, ExtraTurboSmartand WoodenPC and Item model is belongs to Category model and category of these items are Computers, how can I search these items by name of its parent model?


Solution

  • You're either going to have to index the Category model and search by that, or add the model to Item's index:

    def search_data
      {
        name: name,
        category: category.name
      }
    end
    

    and then reindex.