-
-
Save nono/1600053 to your computer and use it in GitHub Desktop.
autocomplete with tire
This file contains hidden or 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 '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 | |
import corals.map.with_index do |coral,i| | |
{id: i.to_s, name: coral, type: 'coral'} | |
end | |
refresh | |
end | |
s = Tire.search 'corals' do | |
query { string 'name.start:hydr' } | |
sort { by :'name.sort', 'asc' } | |
end | |
puts s.results.map(&:name).join(", ") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment