Kareem Hashem
Kareem Hashem

Reputation: 1077

how to get number of documents term appear in, in elasticsearch rails

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

Answers (1)

Brary
Brary

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

Related Questions