Created
July 28, 2011 07:42
-
-
Save pfeiffer/1111165 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
"custom_score": { | |
"query": { | |
"filtered": { | |
"query": { | |
"match_all": { } | |
}, | |
"filter": { | |
"and": [ | |
{ | |
"term": { | |
"hidden": false | |
} | |
}, | |
{ | |
"range": { | |
"current_sign_in_at": { | |
"gt": "2011-04-29" | |
} | |
} | |
}, | |
{ | |
"term": { | |
"sex": "female" | |
} | |
}, | |
{ | |
"term": { | |
"has_image": true | |
} | |
}, | |
{ | |
"bool": { | |
"must_not": [ | |
{ | |
"terms": { | |
"_id": [ | |
1, | |
4, | |
6, | |
10, | |
4999, | |
391034, | |
1839 | |
] | |
} | |
} | |
] | |
} | |
} | |
] | |
} | |
} | |
}, | |
"script": "_score - Math.abs(doc['age'].value - age)/4.0 + (doc['partner_status'].value == 'single' ? 10 : 0) + (doc['has_image'].value == 'true' ? 40 : 0) + (doc['online'].value == 'true' ? 5 : 0) + (doc['boost'].value == 'true' ? 7 : 0) + (doc['current_sign_in_at'].value > stale_date ? 5 : 0) - (doc['location'].empty ? 20 : (doc['location'].distanceInKm(55.714619, 12.5287294) * 0.14))", | |
"params": { | |
"age": 24, | |
"weight": null, | |
"height": null, | |
"stale_date": 1311199200000 | |
} | |
} | |
} |
Second, pass the lat/lon to the scripts as parameters as well, so it won't compile each time:
{
"custom_score": {
"query": {
"filtered": {
"query": {
"match_all": { }
},
"filter": {
"and": [
{
"term": {
"hidden": false
}
},
{
"range": {
"current_sign_in_at": {
"gt": "2011-04-29"
}
}
},
{
"term": {
"sex": "female"
}
},
{
"term": {
"has_image": true
}
},
{
"not" : {
"ids" : {
"type" : "users",
"ids" : [1, 4, 6, 10, 4999, 391034, 1839]
}
}
}
]
}
}
},
"script": "_score - Math.abs(doc['age'].value - age)/4.0 + (doc['partner_status'].value == 'single' ? 10 : 0) + (doc['has_image'].value == 'true' ? 40 : 0) + (doc['online'].value == 'true' ? 5 : 0) + (doc['boost'].value == 'true' ? 7 : 0) + (doc['current_sign_in_at'].value > stale_date ? 5 : 0) - (doc['location'].empty ? 20 : (doc['location'].distanceInKm(lat, lon) * 0.14))",
"params": {
"age": 24,
"weight": null,
"height": null,
"stale_date": 1311199200000,
"lat" : 55.714619,
"lon" : 12.5287294
}
}
}
Third step can be to cache the tuple of certain filters for better perf, for example, created a tuple of hidden
, sex
, and has_image
into a single cached element, so the and
filter will be faster:
{
"custom_score": {
"query": {
"filtered": {
"query": {
"match_all": { }
},
"filter": {
"and": [
{
"bool" : {
"must" : [
{
"term": {
"hidden": false
}
},
{
"term": {
"sex": "female"
}
},
{
"term": {
"has_image": true
}
}
],
"_cache" : true
}
}
{
"range": {
"current_sign_in_at": {
"gt": "2011-04-29"
}
}
},
{
"not" : {
"ids" : {
"type" : "users",
"ids" : [1, 4, 6, 10, 4999, 391034, 1839]
}
}
}
]
}
}
},
"script": "_score - Math.abs(doc['age'].value - age)/4.0 + (doc['partner_status'].value == 'single' ? 10 : 0) + (doc['has_image'].value == 'true' ? 40 : 0) + (doc['online'].value == 'true' ? 5 : 0) + (doc['boost'].value == 'true' ? 7 : 0) + (doc['current_sign_in_at'].value > stale_date ? 5 : 0) - (doc['location'].empty ? 20 : (doc['location'].distanceInKm(lat, lon) * 0.14))",
"params": {
"age": 24,
"weight": null,
"height": null,
"stale_date": 1311199200000,
"lat" : 55.714619,
"lon" : 12.5287294
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First step: change bool filter to not: