Reputation:
Here's the full error message:
Searchkick::InvalidQueryError: Searchkick::InvalidQueryError: [400] {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [foo_name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"foos_test","node":"k0yYkVnIQzaXbvpAG_rKgw","reason":{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [foo_name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."}}],"caused_by":{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [foo_name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."}},"status":400}
I get this error (this error only shows in minitest) when I add the code below in index action:
order: {foo_name: :asc}
Here's my full index code:
def index
query = params[:q].presence || "*"
@foos = Foo.search(
query,
page: params[:page], per_page: 25,
order: {foo_name: :asc}
)
end
end
I'm using Searchkick.
Upvotes: 3
Views: 4714
Reputation: 138
This was a nasty one; we were fortunate to stumble on the solution. I assume your model looks something like this:
class Foo
attr_accessor :foo_name
searchkick # some settings
def search_data
{ foo_name: foo_name }
end
end
What you need to do is define a foo_name_sort field with the same value as foo_name, but set to filterable:
class Foo
attr_accessor :foo_name
searchkick # some settings,
filterable [:foo_name_sort]
def search_data
{ foo_name: foo_name,
foo_name_sort: foo_name }
end
end
And then to search:
@foos = Foo.search(
query,
page: params[:page], per_page: 25,
order: {foo_name_sort: :asc}
)
Upvotes: 2
Reputation: 882
Try this
def index
query = params[:q].presence || "*"
@foos = Foo.search(
query,
page: params[:page], per_page: 25,
aggs: {foo_name: {order: {"_term" => "asc"}}}
)
end
Upvotes: 0