Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch Java API Function Score Query with geo and time gauss function

I am currently trying to build this function score query with the Java API of elasticsearch:

{
   "query": {
      "function_score": {
         "functions": [
            {
               "gauss": {
                  "location": {
                     "origin": {
                        "lat": 52.55,
                        "lon": 13.69
                     },
                     "offset": "30km",
                     "scale": "10km",
                     "decay": 0.9
                  }
               }
            },
            {
               "gauss": {
                  "createdAt": {
                     "origin": "2015-06-14T15:50:00",
                     "scale": "8h",
                     "offset": "4h",
                     "decay": 0.75
                  }
               }
            }
         ]
      }
   }
}

But I can't find any documentation regarding the java api and the function score queries. This is what I have so far:

elasticsearch.client
  .prepareSearch(config.offerIndex.value)
  .setQuery(
     QueryBuilders.functionScoreQuery(
       ScoreFunctionBuilders
         .gaussDecayFunction("location", ???, ???).setDecay(0.9)
     )
  )

The second and the third parameter of the gaussDecayFunction are named origin and scale. But they have the any type and I have no idea how I have to provide my location and time values there. And the next question is how I can provide to functions in the FunctionScore Builder

like image 893
MeiSign Avatar asked Jun 14 '15 09:06

MeiSign


1 Answers

I have found this solution but I am not sure if this is the clean way to do it. Would appreciate if someone can approve it.

val lat = 52.52
val lon = 13.402

QueryBuilders
  .functionScoreQuery(
     ScoreFunctionBuilders.gaussDecayFunction("location", new GeoPoint(lat, lon), "10km")
       .setDecay(0.9)
       .setOffset("30km"))
  .add(
     ScoreFunctionBuilders.gaussDecayFunction("createdAt", new DateTime(), "8h")
       .setDecay(0.75)
       .setOffset("4h"))
)
like image 111
MeiSign Avatar answered Sep 28 '22 09:09

MeiSign