Elasticsearch https cors 已启用,但仍然收到请求的资源上没有“Access-Control-Allow-Origin”标头

es3*_*746 3 javascript http cors elasticsearch

我已经在名为 myIndex 的索引中启用了 cors 设置,以下是它的设置。但是当我尝试使用一些简单的 Javascript 从 elasticsearch 中提取数据(其中设置了 cors 标头)时,我收到错误

XMLHttpRequest 无法加载http://elasticSearchDomain.com:9200/myIndex/_search/。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。来源' http://localhost:8000因此,不允许访问

我通过将 JavaScript 作为脚本运行,当我通过 python 服务器打开 html 页面时运行该脚本。

{
      "state": "open",
      "settings": {
        "index": {
          "http": {
            "cors": {
              "allow-credentials": "true",
              "enabled": "true",
              "allow-origin": "*"
            }
          },
          "creation_date": "1461087830891",
          "number_of_shards": "5",
          "number_of_replicas": "1",
          "version": {
            "created": "1070499"
          },
          "uuid": "2JeIgB7IRs6_DzEb6PLx-w"
        }
      },
      "mappings": {
        "tx": {
          "properties": {
            "next": {
              "format": "dateOptionalTime",
              "type": "date"
            },
            "eid": {
              "index": "not_analyzed",
              "type": "string"
            },
            "95percent_time": {
              "type": "double"
            },
            "total_count": {
              "type": "long"
            },
            "failure_count": {
              "type": "long"
            },
            "pool_id": {
              "type": "long"
            },
            "pool_name": {
              "index": "not_analyzed",
              "type": "string"
            },
            "failure_rate": {
              "type": "double"
            },
            "report_time": {
              "format": "dateOptionalTime",
              "type": "date"
            },
            "txn_type": {
              "index": "not_analyzed",
              "type": "string"
            },
            "status_default": {
              "type": "long"
            },
            "txn_name": {
              "index": "not_analyzed",
              "type": "string"
            },
            "frequency_type": {
              "index": "not_analyzed",
              "type": "string"
            },
            "status_2": {
              "type": "long"
            },
            "status_1": {
              "type": "long"
            },
            "avg_time": {
              "type": "double"
            },
            "datacenter_id": {
              "type": "long"
            },
            "datacenter_name": {
              "index": "not_analyzed",
              "type": "string"
            },
            "server_type": {
              "type": "string"
            }
          }
        }
      },
      "aliases": [

      ]
    }
Run Code Online (Sandbox Code Playgroud)

这是我的 Javascript,我试图用它从 elasticsearch 中提取数据

var url = "http://elasticSearchDomain.com:9200/myIndex/_search/";
var method = "POST";
var postData = '{"query": { "filtered": { "query": { "query_string": { "query": "*", "analyze_wildcard": true } }, "filter": { "bool": { "must": [ { "query": { "query_string": { "query": "*", "analyze_wildcard": true } } }, { "range": { "report_time": { "gte": 1458409392443, "lte": 1461001392443 } } } ], "must_not": [] } } } }, "size": 0, "aggs": { "2": { "date_histogram": { "field": "report_time", "interval": "12h", "pre_zone": "-07:00", "pre_zone_adjust_large_interval": true, "min_doc_count": 1, "extended_bounds": { "min": 1458409392443, "max": 1461001392443 } }, "aggs": { "3": { "terms": { "field": "pool_name", "size": 20, "order": { "_count": "desc" } } } } } } }';

// You REALLY want async = true.
// Otherwise, it'll block ALL execution waiting for server response.
var async = true;

var request = new XMLHttpRequest();

// specifies how the HTTP response will be handled. 
request.onload = function () {

   // You can get all kinds of information about the HTTP response.
   var status = request.status; // HTTP response status, e.g., 200 for "200 OK"
   var data = request.responseText; // Returned data, e.g., an HTML document.
}

request.open(method, url, async);
request.setRequestHeader('Access-Control-Allow-Headers', '*');
request.setRequestHeader('Access-Control-Allow-Origin', '*');
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

// Actually sends the request to the server.
request.send(postData);
Run Code Online (Sandbox Code Playgroud)

小智 7

为了某些特殊性(以及对于任何在正则表达式部分遇到问题的人),我必须在 elasticsearch.yml 中添加这些配置:

http.cors.enabled: true
http.cors.allow-origin: /https?:\/\/(localhost)?(127.0.0.1)?(:[0-9]+)?/
Run Code Online (Sandbox Code Playgroud)

不完美的正则表达式,但我的搜索应用程序现在可以运行。