Search code examples
scalaelasticsearchelastic4s

elastic4s query search function score


how I can create the query with client elastic4s scala?

I call using marvel/sense

GET /business/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "name": "my text"
        }
      },
      "script_score": {
        "script": "_score  +  log(doc['reviews'].value + 1 )",
        "lang": "groovy"
      }
    }
  },
  "facets": {
    "industry": {
      "terms": {
        "fields": ["type", "industry"]

      }
    }
  },
  "size": 10
}

But how I can create the query with elastic4s?


Solution

  • You can do a function score query like this:

    val req = search in "marvel/sense" query {
     functionScoreQuery(matchQuery("name", "my_text")).scorers(
      scriptScore("_score  +  log(doc['reviews'].value + 1 )")
     )
    }
    

    Then of course add in the facets, etc following the instructions at https://github.com/sksamuel/elastic4s/blob/master/guide/search.md