Skip to content

Instantly share code, notes, and snippets.

@conikeec
Forked from nono/gist:1600053
Created March 20, 2012 23:47
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 conikeec/2142635 to your computer and use it in GitHub Desktop.
Save conikeec/2142635 to your computer and use it in GitHub Desktop.
autocomplete with tire
#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