1

The manual states this (https://github.com/ankane/searchkick#nested-data):

Nested Data

To query nested data, use dot notation.

User.search "san", fields: ["address.city"], where: {"address.zip_code" => 12345}

This simply doesn't work or the example given needs more qualification.

In my case I have ExpenditureItem which belongs_to: Expenditure so I try this:

ExpenditureItem.search("*", where: { "expenditure.budget_id": '2'})

which results in nothing. I know in my search_data I can set:

budget_id: expenditure.budget_id

but from the example it seems the entire point is to be able to complete quick and easy nested searches.

Am I missing something here?

Dan Tappin
  • 2,692
  • 3
  • 37
  • 77

2 Answers2

0

Nested queries can only be performed on nested fields, specified as such in your mapping. You're gonna have to override your default indexing which is the equivalent of explicitly defining it in your ElasticSearch mapping.

Joe - GMapsBook.com
  • 15,787
  • 4
  • 23
  • 68
0

Associations are not automatically indexed as nested data.

In your case, you would need to specifically include the associations that you want to index in your search_data method.

For example, to include all fields in your ExpenditureItem model as well as the Expenditure association, you could do:

def search_data
  as_json.merge({
    expenditure: expenditure.as_json
  })
end

If you don't need to index all attributes, ou can then of course modify the as_json method params to include or exclude the attributes and instance methods that you require in your index.

Also, to make indexing more performant, be sure to include the association in the search_import scope, e.g.

scope :search_import, -> { includes(:expenditure) }
Mike
  • 987
  • 11
  • 13