Skip to content

Instantly share code, notes, and snippets.

@sashaegorov
Created January 22, 2018 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sashaegorov/ca086dedae7bd7c998376eccaa726fc3 to your computer and use it in GitHub Desktop.
Save sashaegorov/ca086dedae7bd7c998376eccaa726fc3 to your computer and use it in GitHub Desktop.
ElasticSearch setup for searching tags
// DELETE my-index
// PUT my-index
{
"settings": {
"analysis": {
"analyzer": {
"auto_completion_analyzer": {
"type": "custom",
"filter": [
"lowercase",
"auto_completion_filter"
],
"tokenizer": "keyword"
}
},
"filter": {
"auto_completion_filter": {
// "type": "edge_ngram",
"type": "ngram",
"min_gram": 1,
"max_gram": 24
}
}
}
},
"mappings": {
"product": {
"properties": {
"tag": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
},
"completion": {
"type": "text",
"analyzer": "auto_completion_analyzer",
"search_analyzer": "standard"
}
}
}
}
}
}
}
// POST my-index/product/_bulk
{"index":{}}
{"heroname": "John Deep", tag":"super"}
{"index":{}}
{"heroname": "Johny Stalone", "tag":"supreme"}
{"index":{}}
{"heroname": "Tony Stoned", "tag":"bad"}
{"index":{}}
{"heroname": "Stony Stoned", "tag":"badass"}
{"index":{}}
{"heroname": "Andy Deep", tag":"super"}
{"index":{}}
{"heroname": "Andy Stalone", "tag":"supreme"}
{"index":{}}
{"heroname": "Andy Stoned", "tag":"bad"}
{"index":{}}
{"heroname": "Andy Stored", "tag":"badass"}
{"index":{}}
{"heroname": "Ben Deep", "tag":"SuPeR"}
{"index":{}}
{"heroname": "Ben Stalone", "tag":"sUpReMe"}
{"index":{}}
{"heroname": "Ben Stoned", "tag":"BAD_"}
{"index":{}}
{"heroname": "Ben Stored", "tag":"BaDa$$_"}
//GET my-index/product/_search
{
"query": {"match_all": {}}
}
// GET my-index/product/_search
{
"query": {
"term": {
"tag.completion": "ad"
}
},
"size": 0,
"aggs": {
"suggestions": {
"terms": {
"field": "tag.raw"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment