Olga Gorun
Olga Gorun

Reputation: 329

Problems with maps in elasticsearch scripts Map parameters

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?

Upvotes: 1

Views: 938

Answers (1)

Val
Val

Reputation: 217504

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
              }
           }
        }
      }
    }
  } 
}

Upvotes: 2

Related Questions