Skip to content

Instantly share code, notes, and snippets.

@adrienjoly
Created October 3, 2012 13:44
Show Gist options
  • Save adrienjoly/3826977 to your computer and use it in GitHub Desktop.
Save adrienjoly/3826977 to your computer and use it in GitHub Desktop.
Spotify-like search/filter using ElasticSearch
# to store in ./config
index:
analysis:
analyzer:
full_text_analyzer:
type: custom
tokenizer: standard
filter: [standard, lowercase, asciifolding]
partial_text_analyzer :
type : custom
tokenizer : standard
filter : [standard, lowercase, asciifolding, my_edge_ngram]
filter :
my_edge_ngram :
type : edgeNGram
min_gram : 1
max_gram : 10
side : front
/* to store in ./config/templates */
{
"my_template" : {
"template" : "*",
"settings" : {},
"mappings" : {
"_default_" : {
"properties": {
"uid": {
"type": "string",
"index": "not_analyzed",
"include_in_all": false
},
"title": {
"type": "multi_field",
"fields": {
"title": {
"type": "string",
"index_analyzer": "partial_text_analyzer",
"search_analyzer": "full_text_analyzer",
"boost": 1
},
"full": {
"type": "string",
"analyzer": "full_text_analyzer",
"boost": 10
}
}
}
}
}
}
}
}
elasticsearch -f -Xmx2g -Xms2g -Des.config=config/elasticsearch.yml
# curl -XDELETE 'http://localhost:9200/myIndex/'
# curl -XPUT 'http://localhost:9200/_bulk' --data-binary @myIndex.json
curl -XPUT 'http://localhost:9200/myIndex/post/' -d '{
"title" : "hello world",
"uId" : "001"
}'
curl -XGET 'http://localhost:9200/myIndex/post/' -d '{
"query": {
"filtered": {
"query":{
"match": {
"title": {
"query": "hell wor",
"operator": "AND"
}
}
},
"filter": { "uId": "001" }
}
},
"from": q.from || 0,
"size": q.from || 50,
"sort": q.sort || [{'_score': 'desc'}],
"facets": q.facets || {}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment