Skip to content

Instantly share code, notes, and snippets.

@pfeiffer
Created July 28, 2011 07:42
Show Gist options
  • Save pfeiffer/1111165 to your computer and use it in GitHub Desktop.
Save pfeiffer/1111165 to your computer and use it in GitHub Desktop.
{
"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
}
}
}
@kimchy
Copy link

kimchy commented Jul 28, 2011

First step: change bool filter to not:

{
      "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(55.714619, 12.5287294) * 0.14))", 
            "params": {
                  "age": 24, 
                  "weight": null, 
                  "height": null, 
                  "stale_date": 1311199200000
            }
      }
}

@kimchy
Copy link

kimchy commented Jul 28, 2011

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
            }
      }
}

@kimchy
Copy link

kimchy commented Jul 28, 2011

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