Skip to content

Instantly share code, notes, and snippets.

@FestivalBobcats
Created May 21, 2013 21:24
Show Gist options
  • Save FestivalBobcats/5623373 to your computer and use it in GitHub Desktop.
Save FestivalBobcats/5623373 to your computer and use it in GitHub Desktop.
setting up auto-complete in ElasticSearch with test documents
require 'tire'
Tire.configure do
url "http://api.qbox.io/xxxxxxxx"
end
CONFIGURATION = {
mappings: {
product: {
properties: {
title: {
type: "multi_field",
fields: {
title: { type: "string" },
sortable: { type: "string", index: "not_analyzed" },
autocomplete: { type: "string", index_analyzer: "shingle_analyzer" }
}
}
}
}
},
settings: {
analysis: {
filter: {
shingle_filter: {
type: "shingle",
min_shingle_size: 2,
max_shingle_size: 5
}
},
analyzer: {
shingle_analyzer: {
type: "custom",
tokenizer: "standard",
filter: ["lowercase", "shingle_filter"]
}
}
}
}
}
PRODUCTS = [
{ type: "product", title: "Tremors" },
{ type: "product", title: "Tremors 2: Aftershocks" },
{ type: "product", title: "Tremors 3: Back to Perfection" },
{ type: "product", title: "Tremors 4: The Legend Begins" },
{ type: "product", title: "True Blood" },
{ type: "product", title: "Tron" },
{ type: "product", title: "True Grit" },
{ type: "product", title: "Land Before Time" },
{ type: "product", title: "The Shining" }
]
Tire.index "products" do
create CONFIGURATION
import PRODUCTS
end
search = Tire.search "products" do
query { prefix 'title.autocomplete', 'tr' }
sort { by 'title.sortable', 'asc' }
end
puts search.results.map(&:title)
# Tremors
# Tremors 2: Aftershocks
# Tremors 3: Back to Perfection
# Tremors 4: The Legend Begins
# Tron
# True Blood
# True Grit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment