0

I've run elasticsearch.bat and queried this in browser with localhost:9200. JSON object returned as follows, so all ok so far.

 {
  "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"
}   

I've downloaded and linked via tag to my index.html the elasticsearch.js

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

(incidentally there's more than 1 .js in the download - do I need to link them all?)

I've then run added to another tag the code

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

and output this to console - JSON object returned so again, presumably all ok up to this point.

If I then run

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

I get the error message returned. And I don't know why.

Incidentally

var elasticsearch = require('elasticsearch');

returns "Uncaught reference error: require is not defined" which would suggest I'm missing at least one other .js file with that function?

2 Answers 2

3

Since you're in a browser, you need to use a browser build (at the moment Angular or jQuery are available).

So let's say you're going the jQuery way, your <script> should look like this:

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

And then you can create your client like this:

var client = new $.es.Client({
  hosts: 'localhost:9200',
  log: 'trace'
});
Sign up to request clarification or add additional context in comments.

Comments

0

The require statement is a NodeJS only function. You need to download and reference the browser build of ES in your HTMl file as Val mentioned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.