This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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