5

Currently, on ES 5.6, we are using groovy inline scripting to get tf of a given term in a field for given documents like -

GET document/_search
{
  "size": 114,
  "query": {"terms": {
    "doc_id": [1840, 2160]
  }}, 
        "script_fields": {
          "tf": {
            "script": {
              "lang": "groovy",
              "inline": "_index['text'][term_value].tf()",
              "params": {
                "term_value": "hello"
              }
            }
          }
        }
      }

So it returns me response like -

 "hits": {
    "total": 36,
    "max_score": 1,
    "hits": [
      {
        "_index": "document",
        "_type": "sample",
        "_id": "41707",
        "_score": 1,
        "fields": {
          "tf": [
            3
          ]
        }
      }]

But after ES 6.0 groovy support has dropped and it seems scripting engine is the only solution left, And with the lack of proper understanding of Elasticsearch classes and internal behaviour it became really difficult to figure out the implementation.

Based On Scripting Engine documentation, I need to implement

private static class MyExpertScriptEngine implements ScriptEngine {
    @Override
    public String getType() {
        return "string";
    }

    @Override
    public <T> T compile(String scriptName, String scriptSource, ScriptContext<T> context, Map<String, String> params) {}

   **What goes here?**

}

Implementation for this class or some other way to achieve the required output would be great help.

xrage
  • 4,690
  • 4
  • 25
  • 31
  • 1
    You can find a sample implementation [here](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/modules-scripting-engine.html) – Val Jun 29 '18 at 07:10
  • Yes, but that is for boosting docs based on freq, I need tf to be returned. – xrage Jun 29 '18 at 07:13

0 Answers0