如何使用 Postman 编写多行 Elasticsearch 脚本 [英] How to write multiline Elasticsearch scripts with Postman

查看:66
本文介绍了如何使用 Postman 编写多行 Elasticsearch 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Postman 使用非常简单的脚本执行 Elasticsearch GET 查询.当脚本全部在一行上时,它可以工作,但是如果我尝试执行多行,则会出现错误

我将数据作为 JSON 发送,内容类型为:application/json 在标题中

示例 - 作品:

<代码>{询问":{match_all":{}},脚本字段":{我的自定义字段":{脚本": {"lang": "无痛","source": "int count = 1; return count;"}}}}

示例 - 产生错误:

<代码>{询问":{match_all":{}},脚本字段":{我的自定义字段":{脚本": {"lang": "无痛",来源": """整数计数 = 1;返回计数;"""}}}}

错误:

 "Unexpected character ('\"' (code 34)): 期望用逗号分隔对象条目\n [来源:org.elasticsearch.transport.netty4.ByteBufStreamInput@20493763;第 9 行,第 18 列]"

我认为邮递员可能会在幕后添加换行符.

解决方案

JSON 中的三重引号在技术上无效 -- 请参阅

I'm trying to do an Elasticsearch GET query with a very simple script using Postman. When the script is all on one line it works but if I try to do multiple lines I get an error

I'm sening the data as JSON with Content-Type: application/json in the header

Example - Works:

{
    "query":{
        "match_all": {}
    },
    "script_fields": {
        "my_custom_field":{
            "script": {
                "lang": "painless",
                "source": "int count = 1; return count;"
            }
        }
    }
}

Example - Produces Error:

{
    "query":{
        "match_all": {}
    },
    "script_fields": {
        "my_custom_field":{
            "script": {
                "lang": "painless",
                "source": """
                   int count = 1;
                   return count;
                """
            }
        }
    }
}

The error:

 "Unexpected character ('\"' (code 34)): was expecting comma to separate Object entries\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@20493763; line: 9, column: 18]"

I think postman may be adding line breaks in behind the scenes.

解决方案

Triple-quotes in JSON are not technically valid -- see this thread for more info.

There are essentially three options:

  • Write a script which takes in a multiline 'JSON' and produces a \n-separated, valid JSON (what I often did prior to multiline backtick strings in JavaScript and still do in php:

function compactifyMultilineString( $input_string )
{
    return str_replace( array( "\r", "\n", "\t" ), " ", $input_string );
}

  • Use postman's own pre-request scripts
  • Or, probably the most reasonable option, set up Kibana right next to your ElasticSearch server. Kibana is great for testing out queries and it also supports a postman-ready copy feature:

这篇关于如何使用 Postman 编写多行 Elasticsearch 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆