Skip to content

Instantly share code, notes, and snippets.

@jinkyou
Last active February 27, 2017 10:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jinkyou/ac92c0d9fc53860b703ac773af03b0da to your computer and use it in GitHub Desktop.
Save jinkyou/ac92c0d9fc53860b703ac773af03b0da to your computer and use it in GitHub Desktop.
ElasticSearch autocomplete
PUT music
{
"settings": {
"index": {
"analysis": {
"filter": {
"autocompleteFilter": {
"max_shingle_size": "5",
"min_shingle_size": "2",
"type": "shingle"
}
},
"analyzer": {
"autocomplete": {
"filter": [
"lowercase",
"autocompleteFilter"
],
"char_filter": [
"html_strip"
],
"type": "custom",
"tokenizer": "standard"
}
}
}
}
}
}
PUT music/_mapping/song
{
"song": {
"properties": {
"autocomplete": {
"type": "string",
"analyzer": "autocomplete",
"fielddata": true
},
"title": {
"type": "string",
"copy_to": [
"autocomplete"
]
}
}
}
}
POST /music/song/1
{
"title": "Real sold my in call."
}
POST /music/song/2
{
"title": "Use securing confined his shutters."
}
POST /music/song/3
{
"title": "Sense child do state to defer mr of forty."
}
POST /music/song/4
{
"title": "His having within saw become ask passed misery giving. "
}
GET music/song/_search
{
"size": 0,
"aggs": {
"autocomplete": {
"terms": {
"field": "autocomplete",
"order": {
"_count": "desc"
},
"include": {
"pattern": "c.*"
}
}
}
},
"query": {
"prefix": {
"autocomplete": {
"value": "c"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment