I have rails app where I try to use both elasticsearch-rails and
elasticsearch-model gems.
I'm getting results but facets aren't working. I wondering if anyone can
pinpoint on what I am doing wrong.
Here is my code that is written but example from elasticsearch team
View
</div>
</div>
module SearchCompanies
extend ActiveSupport::Concern
included do
include Elasticsearch::Model
mapping do
indexes :name, :type => 'string', :boost => 10
indexes :made_in_usa
indexes :categories do
indexes :id, :type => 'string'
indexes :name, :type => 'string', analyzer: 'keyword'
end
end
def as_indexed_json(options={})
self.as_json(
include: { categories:{only:[:name, :id]}})
end
def self.search(query, options={})
__set_filters = lambda do |key, f|
@search_definition[:filter][:and] ||= []
@search_definition[:filter][:and] |= [f]
@search_definition[:facets][key.to_sym][:facet_filter][:and] ||= []
@search_definition[:facets][key.to_sym][:facet_filter][:and] |= [f]
end
@search_definition = {
query: {},
filter: {},
facets: {
categories: {
terms: {
field: 'categories.name'
},
facet_filter: {}
},
made_in_usa: {
terms: {
field: 'made_in_usa'
},
facet_filter: {}
}
}
}
unless query.blank?
@search_definition[:query] = {
bool: {
should: [
{ multi_match: {
query: query,
fields: ['name'],
operator: 'and'
}
}
]
}
}
else
@search_definition[:query] = { match_all: {} }
@search_definition[:sort] = { identification: 'desc' }
end
if options[:categories]
f = { term: { 'categories.name' => options[:categories] } }
__set_filters.(:made_in_usa, f)
end
if options[:made_in_usa]
f = { term: { made_in_usa: options[:made_in_usa] } }
__set_filters.(:categories, f)
end
if options[:sort]
@search_definition[:sort] = { options[:sort] => 'desc' }
@search_definition[:track_scores] = true
end
__elasticsearch__.search(@search_definition)
end
end
end
Controller
def search
params[:query].present?
@search = Company.search params[:query]
@companies = @search.results.to_a
respond_to do |format|
format.html { @companies }
format.json { render :json => @companies}
end
end
My maping
{
"companies" : {
"mappings" : {
"company" : {
"properties" : {
"categories" : {
"properties" : {
"id" : {
"type" : "long"
},
"name" : {
"type" : "string"
}
}
},
"id" : {
"type" : "long"
},
"made_in_usa" : {
"type" : "boolean"
},
"name" : {
"type" : "string"
}
}
}
}
If anyone can see obvious error would you mind pinpoint it.
Thanks
--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/075e6fd9-fafa-4b09-b049-a36cb2827830%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.