Skip to content

Instantly share code, notes, and snippets.

@GarKoZ
Last active May 25, 2019 12:25
Show Gist options
  • Save GarKoZ/baa6ffa9d1107e23bf9b7b8bd8ba342e to your computer and use it in GitHub Desktop.
Save GarKoZ/baa6ffa9d1107e23bf9b7b8bd8ba342e to your computer and use it in GitHub Desktop.
#!/bin/bash
# Tested with elasticsearch version 6.1.3
# Delete the existed one
curl -XDELETE localhost:9200/test?pretty
# Put new mapping
curl -XPUT localhost:9200/test?pretty -H 'Content-Type: application/json' -d '{
"settings": {
"analysis": {
"analyzer": {
"trigrams": {
"tokenizer": "trigram_tokenizer",
"filter": [
"lowercase"
]
}
},
"tokenizer": {
"trigram_tokenizer": {
"type": "ngram",
"min_gram": 3,
"max_gram": 3,
"token_chars": []
}
}
}
},
"mappings": {
"product": {
"properties": {
"name": {
"type": "text",
"analyzer": "trigrams"
}
}
}
}
}'
# Add some docs to the index
curl -H 'Content-Type: application/json' -XPOST localhost:9200/test/product/1?pretty -d '{"name" : "VGA (การ์ดแสดงผล) ASUS CERBERUS GTX1070TI A8G"}'
curl -H 'Content-Type: application/json' -XPOST localhost:9200/test/product/2?pretty -d '{"name" : "VGA (การ์ดแสดงผล) INNO3D GTX 1070 Ti X2 V2 8GB ( N107T-2SDN-P5DS )"}'
curl -H 'Content-Type: application/json' -XPOST localhost:9200/test/product/3?pretty -d '{"name" : "VGA (การ์ดแสดงผล) MSI GTX 1080 Ti ARMOR 11G OC"}'
curl -H 'Content-Type: application/json' -XPOST localhost:9200/test/product/3?pretty -d '{"name" : "8 GB MICRO SD CARD (ไมโครเอสดีการ์ด) KINGSTON (SDC4/8GB)"}'
# Take some time before content can be search after post
sleep 4
# Analyze API to check out trigrams
curl -H 'Content-Type: application/json' -XGET localhost:9200/test/_analyze?pretty -d '{ "analyzer": "trigrams", "text":"GTX:1070"}' | grep "token"
curl -H 'Content-Type: application/json' -XGET localhost:9200/test/_analyze?pretty -d '{ "analyzer": "trigrams", "text":"ceberus"}' | grep "token"
curl -H 'Content-Type: application/json' -XGET localhost:9200/test/_analyze?pretty -d '{ "analyzer": "trigrams", "text":"cerberus"}' | grep "token"
# Check ranking from query with difference keyword
curl -H 'Content-Type: application/json' -XPOST localhost:9200/test/product/_search?pretty -d '{"query" : {"match" : {"name" : "GTX"}}}' | grep -E "name|score"
curl -H 'Content-Type: application/json' -XPOST localhost:9200/test/product/_search?pretty -d '{"query" : {"match" : {"name" : "GTX:1070"}}}' | grep -E "name|score"
curl -H 'Content-Type: application/json' -XPOST localhost:9200/test/product/_search?pretty -d '{"query" : {"match" : {"name" : { "query":"ceberus"}}}}' | grep -E "name|score"
curl -H 'Content-Type: application/json' -XPOST localhost:9200/test/product/_search?pretty -d '{"query" : {"match" : {"name" : { "query":"ไมโครเอสดีการ์ด"}}}}' | grep -E "name|score"
curl -H 'Content-Type: application/json' -XPOST localhost:9200/test/product/_search?pretty -d '{"query" : {"match" : {"name" : { "query":"ไมโครเอสดีการ์ด", "minimum_should_match":"50%"}}}}' | grep -E "name|score"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment