javascript elasticsearch - 适用于本地实例,但不适用于服务器?

时间:2018-03-27 23:04:30

标签: javascript http elasticsearch server cors

我有一个javascript可视化脚本,它使用在弹性搜索中索引的推文(由tweepy库消化),推文正在通过弹性搜索,一切正常,但是可视化只适用于我的本地弹性搜索实例(甚至如果我通过服务器连接到它并且弹性搜索的两个实例都在运行),如果我断开本地elasticsearch并尝试运行javascript它不起作用并且我得到TypeError: response is undefinedUnable to revive connection: http://localhost:9200/

我需要更改什么才能使用弹性搜索实例而不是本地实例?

这是javascript代码:

    $(function() {
    // Data
    var colors = ["#8dd3c7","#ffffb3","#bebada","#fb8072","#80b1d3","#fdb462","#b3de69","#fccde5","#d9d9d9","#bc80bd","#ccebc5","#ffed6f"];
    var client = new elasticsearch.Client({
        host: 'localhost:9200',
        log: 'trace'
    });
    var index = "tw"; 
    var loadData = function() {

        var data_count = 10000;
        client.search({
            index: index,
            q: "*.*",
            size: data_count,
            sort : 'timestamp:desc'
//                        sort: '_id'

        }, function (error, response) {
            hits = response.hits.hits;
            data = hits.map(function(element, index, array) {
                return element._source
            });
//                        console.log(data);

            formatTWEEPY(updateVis);
//            
        });
    };

    loadData();

    // Instantiate vis
    var timesets = sm.vis.timesets()
        .colors(colors)
        // .setMode("background")
        // .applyLayout(false)
        .verticalMode("ellipse")
        .elementMode("gradient")
        .showSettings(true)
        .margin({ top: 0, right: 180, bottom: 0, left: 25 });

    // Update the vis
    var updateVis = function() {
        // Update dimension
        var width = $("svg").width();
        var height = $("svg").height();
        timesets.width(width).height(height - 30);

        d3.select(".sm-timesets-demo").attr("transform", "translate(0, " + height + ")");

        redraw();
    };


    function redraw() {
        d3.select(".sm-timesets-demo").datum(data).call(timesets);
    }


    function formatTWEEPY(callback) {

        data.forEach(d => {
            d.themes = d.hashtags.map(function(t) {return t.text});
            d.time = new Date(d.timestamp);
            d.title = d.message;
        });

//        console.log(data)
        var hashtags = flatten(data.map(function(t){return t.themes}))
//        console.log(hashtags)
        var dict = hashtags.reduce(function (p, c) {
            p[c] = (p[c] || 0) + 1;
            return p;
        }, {});

        var items = Object.keys(dict).map(function(key) {
            return [key, dict[key]];
        });

        themes = items.sort((a, b) => d3.descending(a[1], b[1])).slice(0, 10).map(e => e[0])


        data = { events: data.slice(), themes: themes };
        callback();
    }
});

function flatten(arr) {
  return arr.reduce(function (flat, toFlatten) {
    return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
  }, []);
}

我曾尝试将IP添加到服务器而不是host: 'localhost:9200',但它没有任何区别,但它仍无法正常工作。我已将这些行添加到我的elasticsearch.yml服务器实例和本地服务器上,因此它允许连接:

http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-methods : OPTIONS, HEAD, GET, POST

我在弹性网站上尝试了这个例子,我得到了弹性搜索集群的响应,但不确定如何修复它,所以我得到了正确的用途:

    client.ping({
  requestTimeout: 30000,
}, function (error) {
  if (error) {
    console.error('elasticsearch cluster is down!');
  } else {
    console.log('All is well');
  }
});

跑步中的弹性搜索是6.2

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

请提供systemctl​ ​status​ ​elasticsearch输出

编辑/etc/elasticsearch/elasticsearch.yml

后,

对我有用

  

http.cors.enabled:true

     

http.cors.allow-origin:“*”

$​ ​apt-get​ ​install​​ elasticsearch-6.2.deb
$​ systemctl​ enable​​ elasticsearch  # important    
$​ systemctl​ ​start​ ​elasticsearch