使用elasticsearch.js时出现CORS错误

时间:2015-01-23 00:12:59

标签: javascript angularjs elasticsearch cors

我正在尝试从本地计算机测试索引。我创建了一个带有查询框的简单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"
                }
            }
        }
    }
}

的index.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>

app.js

angular.module('portal', [
    'controllers',
]);

var client = new elasticsearch.Client({
    host: 'http://localhost:9200',
    apiVersion: '1.3',

});

controller.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!')}
            );
        };
    }
);

2 个答案:

答案 0 :(得分:3)

我不知道为什么你开始时会遇到跨域错误。你打开你的前端文件:// ....?完全随机猜测并不确定它是否重要。如果您想要通过Web服务器运行UI,可以打开终端,cd进入该目录并运行'python -m SimpleHTTPServer 7000'。现在您的UI正在localhost: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 console,net选项卡查看您要回复的标题。

答案 1 :(得分:0)

将以下行添加到您的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

在此主题响应belongs_to

上演示了在基于UNIX的系统上杀死弹性搜索Java进程的过程