Question

I'm using the Tire gem to integrate Elasticsearch into my app, but it throws an error when I try to apply my named scope to it:

scope :unflagged, -> { where('flags_count < 4') }


def self.search(params)
  tire.search(page: params[:page], per_page: 12) do
        query { string params[:query], default_operator: "AND" } if params[:query].present?   
    sort { by :unflagged } if params[:query].blank?
     end
end

Error:

400 : {"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[JDdrV2g5RWiePD6SezSHvQ][posts][1]: SearchParseException[[posts][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"sort\":[\"unflagged\"],\"size\":12}]]]; nested: SearchParseException[[posts][1]: from[-1],size[-1]: Parse Failure [No mapping found for [unflagged] in order to sort on]]; }{[JDdrV2g5RWiePD6SezSHvQ][posts][2]: SearchParseException[[posts][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"sort\":[\"unflagged\"],\"size\":12}]]]; nested: SearchParseException[[posts][2]: from[-1],size[-1]: Parse Failure [No mapping found for [unflagged] in order to sort on]]; }{[JDdrV2g5RWiePD6SezSHvQ][posts][0]: SearchParseException[[posts][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"sort\":[\"unflagged\"],\"size\":12}]]]; nested: SearchParseException[[posts][0]: from[-1],size[-1]: Parse Failure [No mapping found for [unflagged] in order to sort on]]; }{[JDdrV2g5RWiePD6SezSHvQ][posts][4]: SearchParseException[[posts][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"sort\":[\"unflagged\"],\"size\":12}]]]; nested: SearchParseException[[posts][4]: from[-1],size[-1]: Parse Failure [No mapping found for [unflagged] in order to sort on]]; }{[JDdrV2g5RWiePD6SezSHvQ][posts][3]: SearchParseException[[posts][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"sort\":[\"unflagged\"],\"size\":12}]]]; nested: SearchParseException[[posts][3]: from[-1],size[-1]: Parse Failure [No mapping found for [unflagged] in order to sort on]]; }]","status":400}

I've seen a couple of similar questions posted on SO, but never with an answer.

Could anyone explain if it's possible to apply a named scope to Tire/Elasticsearch? And if so, how?

Was it helpful?

Solution

I'm not sure this qualifies as an 'answer', but being completely stumped and seeing there were several similar unanswered questions about scopes and Tire on SO, I would recommend anyone else having the same issues to simply use another search client.

My criteria were that it had a free tier production support on Heroku, was relatively easy to implement with good documentation, and supported my existing scopes.

I have gone with Swiftype, which meets all those criteria, providing excellent documentation, a sample app, and a free tier on heroku.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top