autocomplete with tire
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
#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