Skip to content

Instantly share code, notes, and snippets.

@athoune
Created January 12, 2012 11:23
Show Gist options
  • Save athoune/1599957 to your computer and use it in GitHub Desktop.
Save athoune/1599957 to your computer and use it in GitHub Desktop.
autocomplete with tire
#encoding: utf-8
require 'rubygems'
require 'tire'
require 'json'
require 'active_support/core_ext/object/to_query'
require 'active_support/core_ext/object/to_param'
conf = {
settings: {
number_of_shards: 1,
number_of_replicas: 0,
analysis: {
analyzer: {
my_start: {
tokenizer: 'whitespace',
filter: %w{asciifolding lowercase my_edge}
},
my_sort: {
tokenizer: 'keyword',
filter: %w{asciifolding lowercase}
}
},
filter: {
my_edge: {
type: 'edgeNGram',
min_gram: 1,
max_gram: 10,
side: 'front'
}
}
}
},
mappings: {
coral: {
properties: {
id: {type: 'string', index: 'not_analyzed', include_in_all: false},
name: { type: 'multi_field', fields: {
start: {
type: 'string', analyzer: 'my_start', include_in_all: false
},
sort: {
type: 'string', analyzer: 'my_sort', include_in_all: false
}
}
}
}
}
}
}
corals = [
'Hydractinia echinata',
'Heterastridium',
'Hydractinia symbiolongicarpus',
'Hydrichthys'
]
Tire.configure { logger 'elasticsearch.log', :level => 'debug' }
Tire.index 'corals' do
delete
create conf
cpt = 0
import corals.map{ |coral|
cpt += 1
{id: cpt.to_s, name: coral, type: 'coral'}
}
refresh
end
s = Tire.search 'corals' do
query do
string 'name.start:hydr'
end
sort do
by :'name.sort', 'asc'
end
end
s.results.each do |document|
p document.name
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment