【问题标题】:Ajax 400 bad request from an elastic search filtered query来自弹性搜索过滤查询的 Ajax 400 错误请求
【发布时间】:2017-10-15 20:03:24
【问题描述】:

当查询有过滤器时,我想使用 Ajax 从 elasticsearch 检索数据。 我正在使用 GET。

我的代码如下:

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
    $(document).ready(function(){
        $.ajax({
            type: 'GET',
            url : 'HOST/tomo2o/shop/_search',
            crossDomain: true,
            data: JSON.stringify({"query":{"match":{"catlev1":"Motors"}}}),
            contentType:'application/json; charset=utf-8',
            dataType: 'json',
            success: function(data) {
                console.log(data);
            },
            error: function(e) {
                console.log(e);
            }
        });
    });
</script>

我做了人们建议做的三件事:

  1. 字符串化 json 过滤器
  2. 设置数据类型为json
  3. 设置内容类型为application/json

但我仍然收到以下错误:

GET HOST/tomo2o/shop/_search?{%22query%22:{%22match%22:{%22catlev1%22:%22Motors%22}}} 400 (Bad Request)

【问题讨论】:

    标签: json ajax elasticsearch get bad-request


    【解决方案1】:

    当你要传递一个DSL query in the query string时,你需要在source参数中传递它,并且还要添加一个source_content_type参数来指示内容类型

    所以你可以这样做:

        $.ajax({
            type: 'GET',
            url : 'HOST/tomo2o/shop/_search',
            crossDomain: true,
            data: {
               source: JSON.stringify({"query":{"match":{"catlev1":"Motors"}}}),
               source_content_type: "application/json"
            },
            contentType:'application/json; charset=utf-8',
            dataType: 'json',
            success: function(data) {
                console.log(data);
            },
            error: function(e) {
                console.log(e);
            }
        });
    

    【讨论】:

    • 太棒了,很高兴它有帮助!
    • 这对于参考查询格式和 JSON.stringify 非常有帮助
    猜你喜欢
    • 2015-01-25
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多