0

below is my query, I want to change score calculation using function_score feature:

{
  "size": 1,
  "query":{
      "function_score": {
    "query": {
      "bool": {
        "must": [
          {
            "match": {
              "messageText": "car"
            }
          }
        ]
      }
    },
    "script_score" : {
        "script" : "doc['time_views'].values[doc['time_views'].values.length-1]"
    }
    ,
    "boost_mode": "replace"
  }
  },
  "from": 0
}

but I got this error response

{
    "error": {
        "root_cause": [
            {
                "type": "script_exception",
                "reason": "failed to run inline script [doc['time_views'].values[doc['time_views'].values.length-1]] using lang [groovy]"
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "datacollection",
                "node": "TWeZV3R6Rq-WYQ2YIHjILQ",
                "reason": {
                    "type": "script_exception",
                    "reason": "failed to run inline script [doc['time_views'].values[doc['time_views'].values.length-1]] using lang [groovy]",
                    "caused_by": {
                        "type": "illegal_argument_exception",
                        "reason": "No field found for [time_views] in mapping with types [message]"
                    }
                }
            }
        ]
    },
    "status": 500
}

some solutions says using quotation in "doc['time_views']" causes the problem when query has been send from command prompt tools. I don't know why! I don't use any command prompt tools. I create the query in java code directly

EDIT

this is my index mapping:

"mappings": {
    "message": {
        "properties": {
            "text": {
               "type": "string"
            },
            "time_views": {
                "type": "nested",
                   "properties": {
                      "backupTimestamp": {
                         "type": "long"
                      },
                      "views": {
                         "type": "integer"
                      }
                   }
                }
             }
        }
    }
}

I want to use "views" of last item of "time_views". so I try below scripts too, but each of them throw different error:

"doc['time_views.views'].values[doc['time_views.views'].values.length-1]"

error: java.util.ArrayList cannot be cast to java.lang.Number

"doc['time_views.views'].values[doc['time_views.views'].values.size()-1]"

error: failed to run inline script [doc['time_views.views'].values[doc['time_views.views'].values.size()-1]] using lang [groovy]

"doc['time_views'].values[doc['time_views'].values.size()-1].views"

error: failed to run inline script [doc['time_views'].values[doc['time_views'].values.size()-1].views] using lang [groovy]"
1
  • Can you try this instead doc.time_views.values[-1] ? Of course the time_views field needs to be declared in your mapping. Commented Jul 26, 2016 at 14:43

1 Answer 1

1

I'm really new in elasticsearch and groovy language. I didn't care about that "time_views" is nested Object, also I don't know syntax of groovy exactly, after some affort I found my mistakes and the solution:

{
  "size": 1,
  "query":{
      "function_score": {
    "query": {
      "bool": {
        "must": [
          {
            "match": {
              "messageText": "car"
            }
          }
        ]
      }
    },
    "script_score" : {
        "script" : "doc['time_views.views'].values.get(doc['time_views.views'].values.size()-1)"
    }
    ,
    "boost_mode": "replace"
  }
  },
  "from": 0
}

It's work as I expected

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.