I am using searchkick. But when I am trying to order the result, getting error as "reason":"No mapping found for [email] in order to sort on".
class User < ApplicationRecord
# ....
extend FriendlyId
friendly_id :username, use: :slugged
searchkick text_start: [:username], suggest: [:username]
class << self
def list token, page, include_admin = false
query = token.presence || "*"
self.search(query,
suggest: true,
where: { admin: include_admin },
order: { email: :desc },
page: page,
per_page: 10)
end
end
# ..
end
Now in Rails console, the AR query gives data but searchkik throws error.
2.3.1 :010 > User.where(admin: false).count
(0.8ms) SELECT COUNT(*) FROM "users" WHERE "users"."admin" = $1 [["admin", false]]
=> 15
2.3.1 :013 > User.list("", 1)
User Search (20.8ms) curl http://localhost:9200/users_development/_search?pretty -d '{"query":{"bool":{"must":{"match_all":{}},"filter":[{"term":{"admin":false}}]}},"
size":10,"from":0,"sort":{"email":"desc"},"suggest":{"text":"*","username":{"phrase":{"field":"username.suggest"}}},"timeout":"11s","_source":false}'
Searchkick::InvalidQueryError: [400] {"error":{"root_cause":[{"type":"query_shard_exception","reason":"No mapping found for [email] in order to sort on","index_uuid":"-e
g2ilskS4-MRTOrn2PV5A","index":"users_development_20161226000627245"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":tr
ue,"failed_shards":[{"shard":0,"index":"users_development_20161226000627245","node":"KLoaUfVlQTa2LC6sacG4fg","reason":{"type":"query_shard_exception","reason":"No mappin
g found for [email] in order to sort on","index_uuid":"-eg2ilskS4-MRTOrn2PV5A","index":"users_development_20161226000627245"}}],"caused_by":{"type":"query_shard_exceptio
n","reason":"No mapping found for [email] in order to sort on","index_uuid":"-eg2ilskS4-MRTOrn2PV5A","index":"users_development_20161226000627245"}},"status":400}
from /Users/arup/.rvm/gems/ruby-2.3.1@muse/gems/searchkick-1.4.0/lib/searchkick/query.rb:142:in `handle_error'
from /Users/arup/.rvm/gems/ruby-2.3.1@muse/gems/searchkick-1.4.0/lib/searchkick/query.rb:82:in `rescue in execute'
from /Users/arup/.rvm/gems/ruby-2.3.1@muse/gems/searchkick-1.4.0/lib/searchkick/query.rb:75:in `execute'
from /Users/arup/.rvm/gems/ruby-2.3.1@muse/gems/searchkick-1.4.0/lib/searchkick/index.rb:138:in `search_model'
from /Users/arup/.rvm/gems/ruby-2.3.1@muse/gems/searchkick-1.4.0/lib/searchkick/model.rb:24:in `searchkick_search'
from /Users/arup/.rvm/gems/ruby-2.3.1@muse/gems/searchjoy-0.0.10/lib/searchjoy/track.rb:4:in `search_with_track'
from /Users/arup/part-time-projects/muse/app/models/user.rb:46:in `list'
from (irb):13
My version of elasticsearch is 5.1.1 and Rails 5.
I did reindex couple of times but same error. What could be the issue?
It sounds like there's no email field in your mapping. Make sure your search_data method includes an email field.
It seem to also happen if your dataset is empty.
@artshpakov No, email is present. I will check the search_data though if it included the email field or not.
I've got this same problem. email is present in search_data, but I only have one record in this particular Searchkick index (in my development environment). Maybe that is causing the problem.
Found my problem. In my case I was using
Search across multiple indices with:
Searchkick.search "milk", index_name: [Product, Category]
and I was accidentally using indices instead of index_name
Searchkick.search "milk", indices: [Product, Category]
@ankane Thanks. We can close this now. My search_data has no entry for the fields I used in query.
Hello, @artshpakov so how do you deal with empty sets? Thank you.
@ankane Im experimenting this issue too because the tables are empty but i can't create a false record (on sales table).
Any updates on that? We have the same issue with empty indices.
Try the ignore_unmapped option that Elasticsearch supports: https://stackoverflow.com/questions/17051709/no-mapping-found-for-field-in-order-to-sort-on-in-elasticsearch
@ankane looks like using that:
@transfers = Transfer.search( @query, where: @where, order: {time: :desc, ignore_unmapped: true}, page: params[:page], per_page: 20)
Results on:
[400] {"error":{"root_cause":[{"type":"parsing_exception","reason":"[field_sort] Expected START_OBJECT but was: END_OBJECT","line":1,"col":155}],"type":"parsing_exception","reason":"[field_sort] Expected START_OBJECT but was: END_OBJECT","line":1,"col":155},"status":400}
Note: By the way, instead of that I was making a conditional before to ask if there are records.
@Xosmond should be:
order: {time: {order: "desc", ignored_unmapped: true}}
For anyone who came here for this:
@transfers = Transfer.search( @query, where: @where, order: {time: {order: "desc", unmapped_type: "long"}}, page: params[:page], per_page: 20)
That works as well for ES 5+
Most helpful comment
For anyone who came here for this:
Source: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_ignoring_unmapped_fields