Last active
August 29, 2015 14:03
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}} | |
} | |
} | |
} | |
}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"linear": { | |
"date": { | |
"scale": "700d", | |
"decay": 0.5 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | |
} | |
} | |
} | |
}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float score = 0; | |
for (term : terms) { | |
if ( _index[field][term] != null && _index[field][term].tf() > 0) { | |
score +=1; | |
}; | |
} | |
return score; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def score=0; | |
for(term in terms){ | |
if ( _index[field][term].tf() > 0){ | |
score +=1 | |
} | |
}; | |
score; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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