user606521
user606521

Reputation: 15474

How to convert simple groovy script to lucene expression (or use different method)?

I am using following options for search:

scriptSort = 
    _script:
        script: "if(doc['user.roles'].value=='contributor') return 1; else return 2;",
        type: "number",
        order: "asc"

options =
    query: ...
    size: ...
    from: ...
    aggs: ...
    sort: [scriptSort]

As you can see I am using _script option for sorting results. The problem is that search service that I am using dropped support for groovy script language and I have to rewrite this script somehow to something called Lucene expressions.

Upvotes: 1

Views: 733

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52366

Just an attempt, it should be a pretty general approach though. Use a function_score to define your own filters that should be rated differently, based on the value of user.roles field. In my example, I think you should replace "match_all": {} with whatever you have under query (this is the reason why I asked about the full query):

{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "filter": {
            "term": {
              "user.roles": "contributor"
            }
          },
          "weight": 1
        },
        {
          "filter": {
            "bool": {
              "must_not": [
                {
                  "term": {
                    "user.roles": "contributor"
                  }
                }
              ]
            }
          },
          "weight": 2
        }
      ],
      "boost_mode": "replace"
    }
  },
  "sort": [
    {
      "_score": {
        "order": "asc"
      }
    }
  ]
}

Upvotes: 1

Related Questions