Skip to content

Instantly share code, notes, and snippets.

@geordee
Created July 8, 2014 05:05
Show Gist options
  • Save geordee/9313f4867d61ce340a08 to your computer and use it in GitHub Desktop.
Save geordee/9313f4867d61ce340a08 to your computer and use it in GitHub Desktop.
Elasticsearch Schema Definition
namespace :elasticsearch do
desc 'Setup Elasticsearch index and mappings'
task :setup do
load('db/elasticsearch.rb')
end
end
require 'elasticsearch'
client = Elasticsearch::Client.new log: true
# edge_ngram settings
settings_with_ngram = { analysis: {
filter: {
index_ngram_filter: {
type: :edgeNGram,
min_gram: 2,
max_gram: 20,
token_chars: %w(letter digit punctuation symbol)
}
},
analyzer: {
index_ngram_analyzer: {
type: :custom,
tokenizer: :standard,
filter: %w(lowercase asciifolding index_ngram_filter)
},
search_ngram_analyzer: {
type: :custom,
tokenizer: :standard,
filter: %w(lowercase asciifolding)
}
}
}
}
# item
client.indices.delete index: 'item_01'
client.indices.create index: 'item_01', body: {
settings: settings_with_ngram,
mappings: {
item: {
properties: {
name: {
type: :multi_field,
fields: {
name: {
type: :string,
analyzer: :snowball
},
ngramized: {
type: :string,
index_analyzer: :index_ngram_analyzer,
search_analyzer: :search_ngram_analyzer
}
}
},
description: {
type: :multi_field,
fields: {
description: {
type: :string,
analyzer: :snowball
},
tokenized: {
type: :string,
analyzer: :simple
}
}
},
price: {
type: :float,
index: :not_analyzed
}
}
}
}
}
puts 'Index item_01 created'
client.indices.put_alias index: 'item_01', name: 'item'
puts 'Alias item on item_01 created'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment