Skip to content

Instantly share code, notes, and snippets.

@babadofar
Last active August 29, 2015 14:03
Show Gist options
  • Save babadofar/85138ca283157f4db3ad to your computer and use it in GitHub Desktop.
Save babadofar/85138ca283157f4db3ad to your computer and use it in GitHub Desktop.
Elasticsearch function_score for recipe app. Updated with using groovy as scripting language, Elasticsearch version 1.3.0
curl -XPOST "http://localhost:9200/seasonalfoods/_search" -d'
{
"size": 0,
"aggs": {
"ingredients": {
"filter": { "terms": {
"ingredients": [
"jordbær",
"rips",
"solbær",
"poteter",
"timian",
"gressløk",
"mynte",
"basilikum"
]
}},
"aggs": {
"filteredIngs":{
"terms": {
"field": "ingredients",
"size": 100
}}
}
}
}
}'
"linear": {
"date": {
"scale": "700d",
"decay": 0.5
}
}
curl - XPOST "http://localhost:9200/seasonalfoods/_search" - d '
{
"query": {
"function_score": {
"score_mode": "sum",
"boost_mode": "replace",
"filter": {
"bool": {
"must": {
"terms": {
"ingredients": ["jordbær","rips","solbær","poteter","timian","gressløk","mynte","basilikum"]
}
}
}
},
"functions": [{
"linear": {
"date": {
"scale": "700d",
"decay": 0.5
}
}
}, {
"script_score": {
"params": {
"field": "ingredients",
"terms": ["jordbær","rips","solbær","poteter","timian","gressløk","mynte","basilikum"]
},
"script": "def score=0;for(term in terms){ if ( _index[field][term].tf() > 0){ score +=1}}; score;",
"lang": "groovy"
}
}]
}
},
"highlight": {
"pre_tags": [""],
"post_tags": [""],
"fields": {
"ingredients": {}
},
"highlight_query": {
"terms": {
"ingredients": ["jordbær","rips","solbær","poteter","timian","gressløk","mynte","basilikum"]
}
}
}
}'
float score = 0;
for (term : terms) {
if ( _index[field][term] != null && _index[field][term].tf() > 0) {
score +=1;
};
}
return score;
def score=0;
for(term in terms){
if ( _index[field][term].tf() > 0){
score +=1
}
};
score;
curl -XPOST "http://localhost:9200/seasonalfoods/_search" -d'
{ "explain":true,
"query": {
"terms": {
"ingredients": ["jordbær","rips","solbær","poteter","timian","gressløk","mynte","basilikum"]
}
},
"highlight": {
"pre_tags": [
""
],
"post_tags": [
""
],
"fields": {
"ingredients": {}
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment