Skip to content

Instantly share code, notes, and snippets.

@otaviomedeiros
Created March 21, 2016 13:02
Show Gist options
  • Save otaviomedeiros/e015b25d10c367fd63d1 to your computer and use it in GitHub Desktop.
Save otaviomedeiros/e015b25d10c367fd63d1 to your computer and use it in GitHub Desktop.
Elastic search indexing - Products
# ------- PRODUCTS
# mappings
PUT connectere
{
"settings": {
"analysis": {
"analyzer": {
"ngram_analyzer": {
"tokenizer": "ngram_tokenizer",
"filter": [
"lowercase"
]
}
},
"tokenizer": {
"ngram_tokenizer":{
"type": "edgeNGram",
"min_gram": 3,
"max_gram": 10
}
}
}
},
"mappings": {
"product": {
"properties": {
"produtor_id": {"type": "integer"},
"name": {"type": "string", "analyzer": "ngram_analyzer"},
"category": {"type": "string", "analyzer": "ngram_analyzer"},
"manufacturer": {"type": "string", "analyzer": "ngram_analyzer"}
}
}
}
}
# Check out mappings
GET connectere/_mapping
# Testing my ngram analizer
GET connectere/_analyze?analyzer=ngram_analyzer
{ "text": "colh"}
# Retrieve all products
GET connectere/product/_search
{
"query": {
"filtered": {
"query": {
"multi_match": {
"query": "herbi",
"fields": ["name", "category", "manufacturer"]
}
},
"filter": {
"terms": {"produtor_id": ["1","154"]}
}
}
},
"highlight":{
"fields": {"name": {}}
}
}
# destroy connectere index
DELETE connectere
# bulk insert
POST _bulk
{"create": {"_index": "connectere", "_type": "product", "_id": "1"}}
{"produtor_id": 1, "name": "Produto", "category": "adubo", "manufacturer": "BASF"}
{"create": {"_index": "connectere", "_type": "product", "_id": "2"}}
{"produtor_id": 1, "name": "Azeite", "category": "adubo", "manufacturer": "BASF"}
# code to create documents
def create_documents(products = Produto.includes(:pessoa, :familia).all)
bulk_index_rows = []
products.each do |product|
bulk_index_rows << {"create": {"_index": "connectere", "_type": "product", "_id": product.id}}
bulk_index_rows << {"produtor_id": product.pessoa.try(:produtor_id), "name": product.nome, "category": product.familia.descricao, "manufacturer": product.pessoa.try(:nome)}
end
client = Elasticsearch::Client.new log: true, host: '192.168.33.12'
client.bulk body: bulk_index_rows
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment