Skip to content

Instantly share code, notes, and snippets.

@edwardsmit
Last active August 29, 2015 14:01
Show Gist options
  • Save edwardsmit/fcfbc9ed4ab3547f2020 to your computer and use it in GitHub Desktop.
Save edwardsmit/fcfbc9ed4ab3547f2020 to your computer and use it in GitHub Desktop.
Elasticsearch MVEL score calculation: Multiply when field is present
curl -XPUT localhost:9200/test
curl -XPUT localhost:9200/test/mock/_mapping -d '{mock: {properties: {x: {type: "integer"},y: {type: "integer"}}}}'
curl -XPUT localhost:9200/test/mock/1 -d '{x:5, y:6}'
# Score should be 30
curl 'localhost:9200/test/mock/_search' -d '{query: {function_score: {query: {match_all:{}},functions: [{"script_score": {script: "doc[\"x\"].value * (doc.containsKey(\"y\") ? doc[\"y\"].value : 1)"}}]}}}'
# Score should be 5
curl 'localhost:9200/test/mock/_search' -d '{query: {function_score: {query: {match_all:{}},functions: [{"script_score": {script: "doc[\"x\"].value * (doc.containsKey(\"z\") ? doc[\"z\"].value : 1)"}}]}}}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment