Skip to content

Instantly share code, notes, and snippets.

@orbanbotond
Created June 7, 2013 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orbanbotond/5729762 to your computer and use it in GitHub Desktop.
Save orbanbotond/5729762 to your computer and use it in GitHub Desktop.
elastic_search_dsl
aliases = Tire::Configuration.client.get(Tire::Configuration.url + '/_aliases').body
prod = Product.create long_description: 'alma'
price1 = Price.new price:1, agreement_id:1
price2 = Price.new price:2, agreement_id:2
prod.prices<< price1
prod.prices<< price2
prod.tire.update_index
prod.touch
Tire.index('products').delete
Tire.index('products').create
res = Product.search term:'alma'
res.results.each{|x|puts x['prices']['agreement_id']}
res.results.each{|x|puts x['prices']}
# res = Product.search term:'alma', agreement_ids:[1]
res = Product.search agreement_ids:[1]
res.results.first['prices']
Product.all.each do |item|
klass = item.class
klass.tire.index.remove item
end
Product.tire.index.refresh
pr =Product.find_by_id 15
Product.tire.index.remove pr
Tire.index 'articles' do
delete
create
store :title => 'One', :tags => ['ruby'], :published_on => '2011-01-01'
store :title => 'Two', :tags => ['ruby', 'python'], :published_on => '2011-01-02'
store :type => 'article',
:title => 'Three',
:tags => ['java'],
:published_on => '2011-01-02'
class Article
attr_reader :title, :tags, :published_on
def initialize(attributes={})
@attributes = attributes
@attributes.each_pair { |name,value| instance_variable_set :"@#{name}", value }
end
def type
'article'
end
def to_indexed_json
@attributes.to_json
end
end
article = Article.new :title => 'Four',
:tags => ['ruby', 'php'],
:published_on => '2011-01-03'
store article
refresh
end
Tire.index 'articles' do
delete
create :mappings => {
:article => {
:properties => {
:id => { :type => 'string', :index => 'not_analyzed', :include_in_all => false },
:title => { :type => 'string', :analyzer => 'snowball', :boost => 2.0 },
:tags => { :type => 'string', :analyzer => 'keyword' },
:content => { :type => 'string', :analyzer => 'czech' }
}
}
}
end
articles = [
{ :id => '1', :type => 'article', :title => 'one', :tags => ['ruby'], :published_on => '2011-01-01' },
{ :id => '2', :type => 'article', :title => 'two', :tags => ['ruby', 'python'], :published_on => '2011-01-02' },
{ :id => '3', :type => 'article', :title => 'three', :tags => ['java'], :published_on => '2011-01-02' },
{ :id => '4', :type => 'article', :title => 'four', :tags => ['ruby', 'php'], :published_on => '2011-01-03' }
]
Tire.index 'articles' do
import articles
end
Tire.index 'articles' do
import articles do |documents|
documents.map { |document| document.update(:title => document[:title].capitalize) }
end
refresh
end
q = "title:T*"
s = Tire.search('articles') { query { string q } }
s.results.each do |document|
puts "* #{ document.title } [tags: #{document.tags.join(', ')}]"
end
----
class Article
def self.q
"title:T*"
end
def self.search
Tire.search('articles') do |search|
search.query do |query|
query.string self.q
end
end.results
end
end
---
s = Tire.search('articles') do
query do
terms :tags, ['ruby', 'python']
end
end
---
s = Tire.search('articles') do
query do
terms :tags, ['ruby', 'python'], :minimum_match => 2
end
end
s.results.each do |document|
puts "* #{ document.title } [tags: #{document.tags.join(', ')}]"
end
---
s = Tire.search('articles') do
query do
boolean do
should { string 'tags:ruby' }
should { string 'tags:java' }
must_not { string 'tags:python' }
end
end
end
s.results.each do |document|
puts "* #{ document.title } [tags: #{document.tags.join(', ')}]"
end
---
s = Tire.search 'articles' do
query { string 'title:T*' }
facet 'tags' do
terms :tags
end
end
puts "Found #{s.results.count} articles: #{s.results.map(&:title).join(', ')}"
puts "Counts by tag:", "-"*25
s.results.facets['tags']['terms'].each do |f|
puts "#{f['term'].ljust(10)} #{f['count']}"
end
---
s = Tire.search 'articles' do
query { string 'title:T*' }
facet 'global-tags', :global => true do
terms :tags
end
facet 'current-tags' do
terms :tags
end
end
---
s = Tire.search 'articles' do
query { string 'title:Two' }
highlight :title
end
s.results.each do |document|
puts "Title: #{ document.title }; Highlighted: #{document.highlight.title}"
end
s = Tire.search 'articles' do
query { string 'title:Two' }
highlight :title, :body
highlight :title, :body => { :number_of_fragments => 0 }
highlight :title, :body, :options => { :tag => '<strong class="highlight">' }
end
index = Tire.index('weather') do
delete
create
register_percolator_query('warning', :tags => ['warning']) { string 'warning OR severe OR extreme' }
register_percolator_query('tsunami', :tags => ['tsunami']) { string 'tsunami' }
register_percolator_query('floods', :tags => ['floods']) { string 'flood*' }
end
Tire.index('_percolator').refresh
matches = index.percolate(:message => '[Warning] Extreme flooding expected after tsunami wave.')
puts "Matching queries: " + matches.inspect
matches = index.percolate(:message => '[Warning] Extreme flooding expected after tsunami wave.') do
term :tags, 'tsunami'
end
puts "Matching queries: " + matches.inspect
matches = index.percolate(:message => '[Warning] Extreme temperatures expected.') { term :tags, 'tsunami' }
puts "Matching queries: " + matches.inspect
response = index.store( { :message => '[Warning] Severe floods expected after tsunami wave.' },
{ :percolate => 'tags:tsunami' } )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment