I have mapped an associated field as a multi-field. I have set the ‘name’ property to be analyzed using a snow ball analyzer and ‘exact’ as not analyzed. I am able to search on this field and filter as well. I am not able to sort on this field. When trying to sort, elastic returns an error “Can’t sort on string types with more than one value per doc, or more than one token per field”.
I have attempted creating an additional field called ‘raw’ similar to exact, and this did not work either. Below is my mapping and how I am attempting to perform the sort via the tire gem:
mapping do
indexes :sectors, :type => 'object',
:properties => { :name => { :type => 'multi_field',
:fields => {
:name => { :type => 'string', :analyzer => 'snowball' },
:exact => { :type => 'string', :index => 'not_analyzed' , :include_in_all => false }
}
}
}
end
def to_indexed_json
to_json( :include => {
...
:sectors => { :only => ["name"] },
...
})
end
def self.search(params)
tire.search(:load => true, :page=>params[:page], :per_page => 12) do
if params[:query].present?
query { string params[:query], :default_operator => "OR" }
else
query { all }
end
filter :term, "sectors.name.exact" => params[:sector] if params[:sector].present?
sort { by 'sectors.name.exact','asc' } if !params[:sort][:sector].blank?
end
end
There are basically two reasons why you’re getting that error:
name.exactfield since it’snot_analyzedin your mappingnamefield, for example providing an array of values while indexing.I would check what your documents look like. In order to do it quickly you can make a Terms Facet based on a script which returns the number of items included in the
name.exactfield like the following. If you get back at least one of the facet entries with term greather than 1, it means that you’ve added multiple values to the field at least once, thus you cannot sort on it.