Created
July 8, 2014 05:05
-
-
Save geordee/9313f4867d61ce340a08 to your computer and use it in GitHub Desktop.
Elasticsearch Schema Definition
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
namespace :elasticsearch do | |
desc 'Setup Elasticsearch index and mappings' | |
task :setup do | |
load('db/elasticsearch.rb') | |
end | |
end |
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
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