Reputation: 1077
if I have many documents and want to know " how many documents the term 'rails' appear in " is there a way to get this information from elasticsearch " tire gem " from the index ?
Upvotes: 1
Views: 116
Reputation: 1014
You can use Facets to make something like SQL 'group-by' aggregate function, as described here in elasticsearch documentation. A sample code would look like:
index_name = "documents" # name of your index
search_word = 'rails' # word searching for
field_name = 'document_text' # field to search in
search = Tire.search index_name do
query do
string "#{field_name}:#{search_word}"
end
facet 'ids-facets', :global => false do
terms :id
end
end
document_ids = search.results.facets['ids-facets']['terms']
Hope this helps.
Upvotes: 1