Elasticsearch boost query for a certain date value


(Malini) #1

I have the following query. I need to add boost factor when the pubdate is greater than 2013-01.
How can I achieve that?

curl -XGET 'http://localhost:9200/cs/test/_search?pretty=true' -d '
{

"from" : 0,
"size" : 10,
"query" : {
"function_score" : {
"query" : {
"filtered" : {
"query" : {
"multi_match" : {
"query" : "java",
"fields" : [ "title" ]
}
},
"filter" : {
"and" : {
"filters" : [ {
"terms" : {
"dbname" : [ "pubs", "pub2"]
}
}, {
"range" : {
"pubdate" : {
"from" : "1980-01",
"to" : "2015-06",
"include_lower" : true,
"include_upper" : true
}
}
} ]
}
}
}
},
"functions" : [ {
"filter" : {
"terms" : {
"dbname" : [ "pub2" ]
}
},
"boost_factor" : -10.0
} ]
}
},
"sort" : [ {
"_score" : {
"order" : "desc"
}
}, {
"pubdate" : {
"order" : "desc"
}
} ]
}'


(Jason Bryan) #2

Hi Malini,

I'll bite. I tried this out with some Stackoverflow data I have and came up with this:

POST /stack/_search
{
  "query": {
    "function_score": {
      "query": {"match": {
        "body": "java"
      }},
      "functions": [
        {
          "filter": {
            "range": {
              "creation_date": {
                "gte": "2014-09-09"
              }
            }
          }
          , "weight": 5
        }
      ]
    }
  }
}

Here, I tell ES I want to modify the scoring of my query by multiplying the document score by the "weight" value for the documents in the given filter. Please try it out and post back if you get expected results.

--
Jason


(Malini) #3

Thanks for you response.


(system) #4