Using elasticsearch.js

Using elasticsearch.js

本文关键字:js elasticsearch Using      更新时间:2023-09-26

我已经运行了elasticsearch.bat并在localhost:9200的浏览器中查询了这个。JSON对象返回如下,到目前为止一切正常。

 {
  "status" : 200,
  "name" : "hwils_01_dev",
  "cluster_name" : "elasticsearch_hwils_dev",
  "version" : {
    "number" : "1.7.2",
    "build_hash" : "e43676b1385b8125d647f593f7202acbd816e8ec",
    "build_timestamp" : "2015-09-14T09:49:53Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
}   

我已经下载并通过标签链接到我的index.html的elasticsearch.js

<script src="elasticsearch-js/elasticsearch.js"></script>

(顺便说一下,下载中有超过1个。js -我需要链接它们吗?)

然后在另一个标签中添加代码

var client = new elasticsearch.Client({
    host: 'localhost:9200',
    log: 'trace'
});

并将其输出到控制台-返回JSON对象,因此,到目前为止应该都没问题。

如果我然后运行

client.ping({
    requestTimeout: 30000,
    // undocumented params are appended to the query string
    hello: 'elasticsearch'
    }, function (error) {
    if (error) {
        console.error('elasticsearch cluster is down!');
    } else {
        console.log('All is well');
    }
});

得到返回的错误消息。我不知道为什么。

顺便

var elasticsearch = require('elasticsearch');

返回"未捕获的引用错误:要求未定义",这表明我至少缺少一个具有该函数的其他.js文件?

因为你是在浏览器中,所以你需要使用浏览器构建(目前Angular或jQuery可用)。

假设你要用jQuery的方式,你的<script>应该是这样的:

<script src="elasticsearch-js/elasticsearch.jquery.min.js"></script>

然后你可以这样创建你的客户端:

var client = new $.es.Client({
  hosts: 'localhost:9200',
  log: 'trace'
});

require语句是一个NodeJS函数。如Val所述,您需要下载并在HTMl文件中引用ES的浏览器版本。