Skip to content

Instantly share code, notes, and snippets.

@imotov
Created August 16, 2012 18:17
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save imotov/d6e98a1d6faaba2637e7 to your computer and use it in GitHub Desktop.
require 'tire'
require 'sqlite3'
require 'active_record'
# Tire.configure do
# logger STDERR
# end
ActiveRecord::Base.establish_connection( adapter: 'sqlite3', database: ":memory:" )
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define(:version => 1) do
create_table :articles do |t|
t.string :title, :abstract, :tags
end
end
class Article < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
settings :number_of_shard => 1, :number_of_replicas => 0 do
mapping :_source => { :excludes => ['attachment'] } do
indexes :id, :type => 'integer'
indexes :title, :type => 'string'
indexes :tags, :type => 'string', :index => :not_analyzed
end
end
end
Article.index.delete
Article.create_elasticsearch_index
article = Article.new :title => 'First article', :tags => ["retro music", "awesome"]
article.save
Article.index.refresh
response = Article.search do
facet 'tags' do
terms :tags, :size => 100
end
end
print "Found ", response.facets['tags']['terms'].count, " facets:\n"
response.facets['tags']['terms'].each do |term|
print "- ", term['term'], "\n"
end
response = Article.search do
query do
boolean do
must { term :tags, 'retro music'}
end
end
end
print "Found ", response.total, " record\n"
response = Article.search do
query do
boolean do
must { terms :tags, ['retro music', 'something else']}
end
end
end
print "Found ", response.total, " record\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment