Match all in js sdk does not return values, while curl does

Hi elastic wizards.
I have strange issue with elasticsearch (7.11). I have an index called "test_index", and when I query it using curl and "match_all" it returns a value, but when I use the node js client, it does not.
Here is the curl:

 curl https://some_url/test_index/_search?pretty -H 'Content-type: application/json' -d '{"size":100,"from":0,"sort":[{"time":"desc"}],"query":{"match_all":{}}}'

and here is the nodejs query:

{"index":"test_index","size":100,"body":{"sort":[{"time":"desc"}],"query":{"match_all":{}}},"from":0}

After creating the query obj in node, I just use
esClient.search(EntireObj).
The nodejs syntax was taken from:
Search | Elasticsearch Node.js client [7.x] | Elastic

There are no errors, just empty hits array in the nodejs case.
What is wrong in my nodejs syntax that causes the "match_all" search to return nothing?

Thanks!

What does console.log(body) return?

console.log(entireObj.body)
shows:
{ sort: [ { time: 'desc' } ], query: { match_all: {} } }

If you meant the search resutls body, its:

{
      took: 9,
      timed_out: false,
      _shards: { total: 5, successful: 5, skipped: 0, failed: 0 },
      hits: { total: { value: 0, relation: 'eq' }, max_score: null, hits: [] }
    }

Yes that's what I meant. Thanks.

Are you sure you are hitting the same node (some_url ) with curl and the JS code?

What happens if you send instead the following?

{"index":"test_index"}

Actually it's even weirder. It happens even without a complex query.
Let me tell the full story:
This is a part of a test suite. I create a new doc (creating on the fly a new index), and then I search for it with "match_all" and with term query (this is where I use the nodejs client).
I tried now to see the results of the creation and see if the doc was accualy inserted, and it seems that is not.
Here is the call that creates the doc:

    const res = await axios.put(
      `${elasticUrl}/test_index/_doc/1`,
      logdata,
      config
    );

Where the logData is JSON and config are auth headers.

The result is:

{ _index: 'test_index',
_type: '_doc',
_id: '1',
_version: 1,
result: 'created',
_shards: { total: 2, successful: 1, failed: 0 },
_seq_no: 0,
_primary_term: 1 }

After this, I do an empty search:

const res2 = await axios.get(`${elasticUrl}/test_index/_search`);

and I get:

{ took: 0,
  timed_out: false,
  _shards: { total: 5, successful: 5, skipped: 0, failed: 0 },
  hits:
   { total: { value: 0, relation: 'eq' },
     max_score: null,
     hits: [] } }

So it seems there is an underline issue, event before it reaches the "match_all" query.
What is going on?

That's better.

You probably did not refresh the index before searching.

Thank you for all your help.
I still don't know what is the cause, but adding a delay of a few ms before querying solved the issue, so it seems elastic wasn't done indexing the doc when I tried to query it.

Don't rely on adding a pause but instead call explicitly the refresh API.

Use the refresh API to explicitly refresh one or more indices. If the request targets a data stream, it refreshes the stream’s backing indices. A refresh makes all operations performed on an index since the last refresh available for search.

Or use the refresh=wait_for option to your last index operation.
But for tests, forcing a refresh is ok.

1 Like