3

I'm currently writing a website in JavaScript that graphs various fields from data in ElasticSearch, and I've run into a strange issue. (I don't want to use Kibana for a variety of reasons.)

My search query in JavaScript has been returning hits properly up until a bit ago. I made no changes to it, but suddenly the query returns no hits. However, when I follow the URL generated by the call, the hits I want appear.

Here's the query:

        client.search({
        index: "chamber-data",
        type: "Soak1",
        size: 1000,
        scroll: "30s",
        sort: ["_doc"],
        _source: ["@timestamp", "datetime", [...this.props.fields]],
        body: {
            query: {
                bool: {
                    filter: {
                        range: {
                            "datetime": {
                                lte: "now",
                                gte: "now-12H"
                            }
                        }
                    }
                }
            }
        }
    }).then(this.process_promise, this.handle_error);

Here is what the script gives me in the console of my website:

  {
"_scroll_id": "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAA1NrFlZYTEJLYW12UUFTaWowMllJZVcyalEAAAAAAANTbRZWWExCS2FtdlFBU2lqMDJZSWVXMmpRAAAAAAADU28WVlhMQkthbXZRQVNpajAyWUllVzJqUQAAAAAAA1NsFlZYTEJLYW12UUFTaWowMllJZVcyalEAAAAAAANTbhZWWExCS2FtdlFBU2lqMDJZSWVXMmpR",
"took": 0,
"timed_out": false,
"_shards": {
  "total": 5,
  "successful": 5,
  "failed": 0
},
"hits": {
  "total": 0,
  "max_score": null,
  "hits": []
}

}

And here's what the URL gives me:

{"_scroll_id":"DnF1ZXJ5VGhlbkZldGNoBQAAAAAAA1N2FlZYTEJLYW12UUFTaWowMllJZVcyalEAAAAAAANTdBZWWExCS2FtdlFBU2lqMDJZSWVXMmpRAAAAAAADU3cWVlhMQkthbXZRQVNpajAyWUllVzJqUQAAAAAAA1NzFlZYTEJLYW12UUFTaWowMllJZVcyalEAAAAAAANTdRZWWExCS2FtdlFBU2lqMDJZSWVXMmpR",
    "took":9,
    "timed_out":false,
    "_shards":{"total":5,"successful":5,"failed":0},
    "hits":
    {"total":107565,"max_score":null,"hits":[...]}
    }

I have absolutely no idea as to what has happened, and I have no idea how to solve it. Anyone have thoughts?

  • I do not understand what the difference between your searches is. I do not know what `the URL gives me` is meant with. Maybe you can be more verbose. Also, can you please provide curl examples of your query, so you can check if it is reproducible on the command, without invoking two different HTTP clients? – alr Jul 13 '17 at 07:11
  • In addition to @alr can you confirm that the query you use manually and from your js program are actually the same when sent to the ES server? – Adonis Jul 13 '17 at 11:21
  • I'm experiencing the same situation where executing the same query (say, a `matchAll` query) via the js client and an external client (Postman) returns zero hits in one case and a bunch in the second. Where you able to solve your problem? I'm able to call other APIs (create/delete index, bulk, ...) via the client js with success. – Diego Ferri Oct 31 '17 at 16:06

1 Answers1

0

In my case, I have a test script which loads a bunch of records with the bulk api and queries ES just after that. All ok with the external/manual client (Postman), but no-hits with the js client, called in the script just after the insertions.

The problem lies in the fact that the index needs to be refreshed for the newly inserted documents to appear in searches (the same is true for Index, Update and Delete API).

I solved forcing refresh after the bulk call.

client.bulk({
  refresh: true, // This is the important part
  body: [
    // Bunch of documents/actions
  ]
}, function (err, resp) {
  // ...
})
Diego Ferri
  • 2,657
  • 2
  • 27
  • 35