Ask A Question

Notifications

You’re not receiving notifications from this thread.

EleasticSearch and Indexing Empty Models

Dan Tappin asked in Gems / Libraries

I have added Elastic Search to my Rails app via the Searchkick gem. I have hit a snag - when I have a model with no records I get these errors:

Searchkick::InvalidQueryError: [400] {"error":{"root_cause":[{"type":"query_shard_exception","reason":"No mapping found for [name] in order to sort on"...

My controller by default is trying to sort the results but there are no results in the index so this fails. A plain old .search("*") returns no results - as expected. It seems that the reindex runs but the index doesn't build the mappings because there is no data.

I know I could add some logic before my search (like a Model.count) but this seems non-ideal. Am I missing a basic ElasticSearch feature here or any other ways to avoid this?

Reply
Reply

That was the trick - I should have spent more time searching the issues and I would have found that. If any one else hits this issue also note the requirement to add the second level of { } to the order query. That is not obvious. Just spent 10 minutes baning my head against the wall. 🤦‍♂️

Reply
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 74,071+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.