来自弹性搜索过滤查询的Ajax 400错误请求

问题描述:

我想在查询具有过滤器时使用Ajax从elasticsearch检索数据。 我正在使用GET。来自弹性搜索过滤查询的Ajax 400错误请求

我的代码如下:

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

当你想通过你需要将它传递的参数source,并添加一个source_content_type参数指示内容类型

一个DSL query in the query string所以你可以这样做:

$.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); 
     } 
    }); 
+0

很多感谢的人,按预期工作 – woshitom

+0

真棒,很高兴它帮助! – Val