dockit icon indicating copy to clipboard operation
dockit copied to clipboard

Not suport multipline of `Painless` script

Open semsphy opened this issue 1 year ago • 2 comments

I was using the multi-line painless script in the request to calculate the distance. after click the play I got the error message

status: 400, details: JSON5: invalid character '\n

Desktop (please complete the following information):

  • OS: macOS
  • Server Opensearch 2.6
  • Version 15.0.1

Reproduce Steps

Just write a multi-line painless script or follow my sample below

GET INDEX/_search 
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "name": "xxx"
          }
        },
        {
          "multi_match": {
            "query": "xxx",
            "fields": [
              "yyy",
              "yyy_2gram",
              "yyy._3gram"  
            ]
          }
        }
      ]
    }
  },
  "size": 100,
  "script_fields": {
    "distance": {
      "script": {
        "lang": "painless",
        "source": """double distance(double lat1, double lon1, double lat2, double lon2){
            double R = 6371; // Earth radius in kilometers
            double dLat = Math.toRadians(lat2 - lat1);
            double dLon = Math.toRadians(lon2 - lon1);
            double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                        Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                        Math.sin(dLon/2) * Math.sin(dLon/2);
            double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

            return R * c;
          }
          return distance(doc['location'].lat, doc['location'].lon, params.lat, params.lon);
        """,
        "params": {
          "lat": 11.0,
          "lon": 11.0
        }
      }
    }
  }
}

Expected behavior No Error of status: 400, details: JSON5: invalid character '\n'

Screenshots image

Additional context Add any other context about the problem here.

semsphy avatar Oct 07 '24 02:10 semsphy

Hi @semsphy thanks for raising the issue, I will looking into it

Blankll avatar Oct 08 '24 12:10 Blankll

Painless scripting scenarios:

  • [ ] _search - script_fields

    GET my-index-000001/_search
    {
    "script_fields": {
    "my_doubled_field": {
     "script": { 
       "source": "doc['my_field'].value * params['multiplier']", 
       "params": {
         "multiplier": 2
       }
     }
    }
    }
    }
    
  • [ ] _search - runtime_mappings

    GET my-index-apache-log/_search
    {
      "runtime_mappings": {
        "clientip": {
          "type": "ip",
          "script": """
            String clientip=grok('%{COMMONAPACHELOG}').extract(doc["message"].value)?.clientip;
            if (clientip != null) emit(clientip); 
          """
        }
      },
      "query": {
        "exists": {
          "field": "clientip"
        }
      },
      "fields" : ["clientip"]
    }
    
  • [ ] _mappings

    PUT my-index/_mappings
    {
      "runtime": {
        "http.clientip": {
          "type": "ip",
          "script": """
            String clientip=grok('%{COMMONAPACHELOG}').extract(doc["message"].value)?.clientip;
            if (clientip != null) emit(clientip); 
          """
        }
      }
    }
    
  • [ ] _update

    POST my-index-000001/_update/2
    {
      "script": {
        "source": "ctx._source.tags.add(params['tag'])",
        "lang": "painless",
        "params": {
          "tag": "blue"
        }
      }
    }
    

Blankll avatar Oct 11 '24 15:10 Blankll

Hi @semsphy , the fix has been released in version 0.4.7, due to the workload, the highlighting and syntax auto-completion will be in the future roadmap, pls download the latest version and try it out, feel free to raise another issues if you meet any unexpected behavior

Blankll avatar Oct 13 '24 15:10 Blankll