简体   繁体   English

Elasticsearch:cURL到Ajax请求

[英]Elasticsearch: cURL to Ajax request

I am using Ajax request for elasticsearch to get search results. 我正在使用弹性搜索的Ajax请求来获取搜索结果。 Finally, I have found the query which I have to go for. 最后,我找到了我必须要查询的查询。 (This is a follow up question to link ) (这是一个后续问题链接

Here is the query in cURL: 这是cURL中的查询:

[~]$ curl -XGET 'localhost:9200/firebase/_search?pretty' -H 'Content-Type: application/json' -d'
> {"query": {"match": {"song": {"query": "i am in", "operator": "and"}}}}'

Result: 结果:

{
  "took" : 286,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.8630463,
    "hits" : [
      {
        "_index" : "firebase",
        "_type" : "song",
        "_id" : "0001",
        "_score" : 0.8630463,
        "_source" : {
          "song" : "i am in california",
          "song_name" : "hello",
          "song_url" : "https://s3.ap-south-1.amazonaws.com/..."
        }
      }
    ]
  }
}

This query gives me result exactly what I need. 这个查询给我的结果正是我需要的。 When I am using the same request with AJAX, it gives me a bad request. 当我在AJAX中使用相同的请求时,它给了我一个糟糕的请求。

Ajax.js Ajax.js

Case 1: 情况1:

$(function() {
    $('#message').keyup(function() {    
        var query = {
            "query": {
                "match": {
                    "song": {
                        "query": $("#message").val(),
                        "operator": "and"
                    }
                }
            }
        };

        console.log(JSON.stringify(query));

        $.ajax({
            type: "GET",
            url: "http://localhost:9200/firebase/_search",
            contentType: 'application/json',
            data: query,
            success: searchSuccess,
            dataType: 'json'
        });

    });

});

Result in console: 控制台中的结果:

在此输入图像描述

Case 2 : If I convert the 'data' using JSON.stringify, it gives me: 案例2 :如果我使用JSON.stringify转换'数据',它会给我: 在此输入图像描述

Case 3 : If I change the query structure as following and use JSON.stringify on 'data': 案例3 :如果我将查询结构更改为如下并在'data'上使用JSON.stringify:

var query = {
    query: {
        match: {
            song: {
                query: $("#message").val(),
                operator: "and"
            }
        }
    }
};

Result is: 结果是:

在此输入图像描述

I am not sure what's the issue here, the query is correct because using the same with cURL I am getting correct results in bash. 我不确定这里的问题是什么,查询是正确的,因为使用相同的cURL我在bash中得到了正确的结果。 Question is how to correctly create the Ajax request here. 问题是如何在这里正确创建Ajax请求。 Pointers would be appreciated. 指针将不胜感激。

Edit : And yes I have enabled CORS in elasticsearch.yml: 编辑 :是的,我在elasticsearch.yml中启用了CORS:

http.cors.enabled : true
http.cors.allow-origin : "*"
http.cors.allow-methods : OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers : X-Requested-With,X-Auth-Token,Content-Type, Content-Length

Edit1 : Chrome, Network data underneath so as to provide more information: Edit1 :Chrome,下方的网络数据,以便提供更多信息: 在此输入图像描述

Edit2 : Full error response is below: Edit2 :完整的错误响应如下:

{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"request [/firebase/_search] contains unrecognized parameters: [query[match][song][operator]], [query[match][song][query]]"}],"type":"illegal_argument_exception","reason":"request [/firebase/_search] contains unrecognized parameters: [query[match][song][operator]], [query[match][song][query]]"},"status":400}

From my experience the behavior of GET methods with a body is sometimes hard to predict. 根据我的经验,GET方法与身体的行为有时难以预测。 Actually, elastic search supports GET with body but I also had some strange results depending on the configuration of the servers, proxies and so on... The documentation even highlights the problems with javascript ( search with GET ). 实际上,弹性搜索支持GET与body,但我也有一些奇怪的结果,具体取决于服务器,代理等的配置...文档甚至突出了javascript( 使用GET搜索 )的问题。

Usually, using POST instead of GET helps to resolve these problems or at least to have a further debug option. 通常,使用POST而不是GET有助于解决这些问题,或者至少有一个进一步的调试选项。

For those coming from Google, another way to solve this problem - or even if the POST solution doesn't work - is to use source parameter in the URL, as mentioned in docs[1]. 对于那些来自谷歌的人来说,解决这个问题的另一种方法 - 或者即使POST解决方案不起作用 - 也是在URL中使用source参数,如文档[1]中所述。

$.ajax({
  type: "GET",
  url: "http://localhost:9200/firebase/_search?source_content_type=application/json&source=" + JSON.stringify(query),
  contentType: 'application/json',
  success: searchSuccess,
  dataType: 'json'
});

[1] https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#_request_body_in_query_string [1] https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#_request_body_in_query_string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM