1

I cannot find a way to pass Map as a named script parameter. Groovy-style "[1:0.2,3:0.4]" and json-style {1:0.2, 3:0.4} result in syntax error. Examples:

    GET tt/clip/_search
    {
      "query": {
        "function_score": {
          "script_score": {
            "script": {
              "lang": "painless",
              "source": "return 0",
              "params": {
                "full_text_tfidf": [1:0.2,3:0.4]
             }
          }
         }
    }
  } 
}

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[script] failed to parse field [params]",
        "line": 9,
        "col": 35
      }
    ],
    "type": "parsing_exception",
    "reason": "[script] failed to parse field [params]",
    "line": 9,
    "col": 35,
    "caused_by": {
      "type": "json_parse_exception",
      "reason": "Unexpected character (':' (code 58)): was expecting comma to separate Array entries\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@75a6a7c1; line: 9, column: 37]"
    }
  },
  "status": 400
}

GET tt/clip/_search
{
  "query": {
      "function_score": {
        "script_score": {
          "script": {
            "lang": "painless",
            "source": "return 0",
            "params": {
              "full_text_tfidf": {1:0.2,3:0.4}
           }
        }
      }
    }
  } 
}

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[script] failed to parse field [params]",
        "line": 9,
        "col": 34
      }
    ],
    "type": "parsing_exception",
    "reason": "[script] failed to parse field [params]",
    "line": 9,
    "col": 34,
    "caused_by": {
      "type": "json_parse_exception",
      "reason": "Unexpected character ('1' (code 49)): was expecting double-quote to start field name\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@6ed9faa9; line: 9, column: 36]"
    }
  },
  "status": 400
}

On the other hand, I cannot say that params know to work only with primitive types. Nested arrays are accepted successfully. Is it possible to pass maps as parameters?

2
  • Please show the full example you've tried and the associated error message for each. Commented Mar 5, 2019 at 8:23
  • @Val, I edited the post to add Map parameter examples Commented Mar 5, 2019 at 8:43

1 Answer 1

2

The correct way to specify a map in the parameters is simply by using a JSON hash (you're missing the double quotes around the keys):

GET tt/clip/_search
{
  "query": {
      "function_score": {
        "script_score": {
          "script": {
            "lang": "painless",
            "source": "return 0",
            "params": {
              "full_text_tfidf": {
                "1": 0.2,
                "3" :0.4
              }
           }
        }
      }
    }
  } 
}
Sign up to request clarification or add additional context in comments.

7 Comments

thank you. I hoped there is a way to pass maps with integer keys, but it definitely answers the second part of the question.
Well, once you iterate over the keys, you can still parse them as integers if you feel like it.
Yes, sure. That's what I'll do.
anything else needed?
Yes. Answer to the first question, about work with maps inside painless script (problem with checking/receiving values by given key)
|

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.