使用 elasticsearch.js 时出现 CORS 错误

CORS error when using elasticsearch.js

本文关键字:CORS 错误 elasticsearch js 使用      更新时间:2023-09-26

我正在尝试从本地机器测试索引。我创建了一个带有查询框的简单 HTML 页面,该查询框使用 elasticsearch.js 客户端将查询发送到 ES。索引和浏览器都在我的桌面上,所以应该没有跨源问题,但我不断收到一个错误,指出:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:9200/portal/book/_search?q=title%3Ahistoire. This can be fixed by moving the resource to the same domain or enabling CORS.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:9200/. This can be fixed by moving the resource to the same domain or enabling CORS.

我尝试启用 CORS,但收到相同的错误。以下是索引的设置:

{
    "portal": {
        "settings": {
            "index": {
                "creation_date": "1421788614558",
                "uuid": "jg-iHjnSTDGHODY0_x4Ysw",
                "number_of_replicas": "1",
                "http": {
                    "cors": {
                        "enabled": "true",
                        "allow-origin": "/(http://)?localhost(:[0-9]+)?/"
                    }
                },
                "number_of_shards": "5",
                "version": {
                    "created": "1040299"
                }
            }
        }
    }
}

索引.html

<!DOCTYPE html >
<html ng-app="portal">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
        <script src="bower_components/angular/angular.min.js"></script>
        <script src="bower_components/elasticsearch/elasticsearch.min.js"></script>
        <script src="js/app.js"></script>
        <script src="js/controllers.js"></script>
    </head>
    <body ng-controller="SearchCtrl">
        <div class="container-fluid">
            <div class="row-fluid">
                <span class="span3">
                    <input class="input-block-level" ng-model="queryTerm" type="text">
                </span>&nbsp;
                <button ng-click="search()" class="btn" type="button">Search</button>
            </div>
            <div class="row-fluid">
                Found {{ results.hits.total }}
                <div class="span4">
                    <li ng-repeat="doc in results.hits.hits">
                        {{ doc._source.title }}
                    </li>
                </div>
            </div>
        </div>
    </body>
</html>

应用.js

angular.module('portal', [
    'controllers',
]);
var client = new elasticsearch.Client({
    host: 'http://localhost:9200',
    apiVersion: '1.3',
});

控制器.js

angular.module('controllers', []).controller('SearchCtrl',
    function($scope) {
        $scope.search = function(query) {
            $scope.results = client.search({
                    index: 'portal',
                    type: 'book',
                    q: 'title:' + $scope.queryTerm
                }, function (error, response) {console.log('could not execute query!')}
            );
        };
    }
);

将以下行添加到您的配置 yml 文件中

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
http.cors.allow-credentials: true

并在基于 Unix 的系统上杀死弹性搜索 Java 进程,如本主题响应 https://stackoverflow.com/a/41644614/11079315

我不知道

为什么一开始就出现跨域错误。你打开你的前端是 file://...吗?完全随机猜测,不确定是否重要。如果你想通过Web服务器运行你的UI,你可以打开一个终端,cd到那个目录并运行'python -m SimpleHTTPServer 7000'。现在您的 UI 正在本地主机:7000 上运行。

有关 CORS 设置,请参阅 http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-http.html,如果您使用的是 Chrome,http://www.williamjohnbert.com/2013/06/allow-cors-with-localhost-in-chrome/可能会有所帮助。

在您的配置中,您至少应该设置"http.cors.enabled: true"和"http.cors.allow-credentials: true"。其他默认值应该是明智的。您可能还需要修改"http.cors.max-age"设置,以防缓存旧设置。

使用浏览器中的 js 控制台、网络选项卡查看您要返回的标头。