Reputation: 1840
I'm setting up a project with node (v 12.4.0) and elasticsearch (7.4.0) using the official module.
I'm attempting to search using
import { Client, RequestParams, ApiResponse } from '@elastic/elasticsearch'
const client = new Client({ node: 'http://localhost:9200' })
const params: RequestParams.Search = { index: 'doc', body: { query: { match: { title: "castle" } } } };
const response: ApiResponse = await client.search(params);
This gives a 200 response, but no results.
Attempting the same thing using Postman returns the 1 result.
POST http://localhost:9200/doc/_search
{
"query": {
"match": { "title": "castle" }
}
}
I'm not having any luck figuring out why the search
function is not working. I've also tested get
, add
, and delete
, which all work.
To create the index, I used:
await client.indices.create({ index: "doc" });
To add a document, I use:
await client.index({
index: 'doc',
body: {
title: "Castle Title",
body: "This is text that is not the title."
}
});
What am I doing wrong?
Upvotes: 3
Views: 719
Reputation: 17745
I've tested this and it works, the only thing is that elasticsearch is near real-time searchable. You need to wait 1 second before the document becomes searchable. Basically, if you are running a test or something, where you save the record just before searching you need to either:
Upvotes: 2