Question

How can I use elasticsearch to search for people which are around a specific age? So if I enter 28 as age, I want the people who are 28 to have the highest score, but also want people who are 27 to be shown, but with a lower score. The birthdate is stored in the following format yyyy-mm-dd, so I will have to convert age to date, but this is no problem.

I have the following so far:

{
    "query": {
        "fuzzy": {
            "birthdate": {
                "value": "1985-10-01",
                "min_similarity": "1096d"
            }
        }
    }
}

The min_similarity of 1096d means that people who are born on the 1st of October 1985 +/- 3 years.

So all people who are born between 1982 and 1988 are shown - this works great, but they all have the same score of 1.0. How can I get the highest score for the entry with the birthdate nearest to 1985-10-01 ?

Was it helpful?

Solution

You can calculate a custom score using a script. This script uses SimpleDateFormat to parse your date (1985-10-01), then calculates the absolute value of that date (in ms) minus the document's date (in ms). You want the lowest value (closest to target date) first, so sort by score ascending instead of the default descending.

{
  "query": {
    "custom_score": {
      "query": {
        "fuzzy": {
          "birthdate": {
            "value": "1985-10-01",
            "min_similarity": "1096d"
          }
        }
      },
      "script": "abs(new \
java.text.SimpleDateFormat('yyyy-MM-dd').parse('1985-10-01').getTime() - \
doc['birthdate'].date.getMillis())"
    }
  },
  "sort": [
      { "_score": "asc" }
  ]
}

More info on custom scoring is http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query/

OTHER TIPS

It can be done as well with Decay functions instead of a custom script:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top