An autocomplete built using Elasticsearch, powered by data from Musicbrainz.
Musicbrainz Postgresql (VM) -> Logstash (jdbc) -> Elasticsearch.
The data is indexed using a custom Autocomplete analyzer (https://www.elastic.co/guide/en/elasticsearch/guide/current/_index_time_search_as_you_type.html):
- Set up the analyzer
curl -XPUT 'localhost:9200/artists?pretty' -d '
{
"settings": {
"number_of_shards": 1,
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 20
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
}
}
}
'
- Alter the index mapping to use the analyzer:
curl -XPUT 'localhost:9200/artists/_mapping/artist' -d '
{
"artist": {
"properties": {
"name": {
"type": "string",
"analyzer": "autocomplete",
"search_analyzer": "standard"
}
}
}
}
'
Autocomplete search results will be returned ranked by a combination of textual relevance and popularity:
curl -XPOST 'http://localhost:9200/artists/_search' -d '
{
"query": {
"function_score": {
"query": {
"match": {
"name": {
"query": e.target.value,
"analyzer": "standard"
}
}
},
"script_score": {
"script": "_score * Math.log(doc['views'].value + 1)"
}
}
}
}
'
cd to Elasticsearch and run:
./bin/elasticsearch
cd to logstash and run:
bin/logstash -f jdbc/musicbrainz.conf
curl -XPUT 'localhost:9200/index-name?pretty'
or specify custom shards/replicas config:
curl -XPUT 'localhost:9200/some-index?pretty' -d '
{
"settings" : {
"index" : {
"number_of_shards" : 5,
"number_of_replicas" : 2
}
}
}
'
curl -XDELETE 'localhost:9200/index-name?pretty'
curl 'http://localhost:9200/index-name/_search?size=100&pretty'
curl 'http://localhost:9200/artists/_count'
curl 'http://localhost:9200/index-name/type-name/some-id?pretty'
curl 'http://localhost:9200/index-name/_mapping/type-name?pretty'
curl -XPUT 'http://localhost:9200/artists/artist/_mapping' -d '
{
“{
"properties" : {
“some-field” : {"type" : “integer”}
}
}
}
'
curl -XPUT 'http://localhost:9200/artists/artist/6fd9967d-5836-475f-9936-8914fa72a82f' -d '
{
"gid": "6fd9967d-5836-475f-9936-8914fa72a82f",
"name": "Elliot Smith",
"views": 1
}
'
curl -XPOST 'http://localhost:9200/artists/artist/a74b1b7f-71a5-4011-9441-d0b5e4122711/_update' -d '
{
"script" : "ctx._source.views+=1"
}
'