【问题标题】:Node Elasticsearch module search not returning results节点 Elasticsearch 模块搜索不返回结果
【发布时间】:2019-10-25 17:31:08
【问题描述】:

我正在使用the official module 设置一个包含节点 (v 12.4.0) 和弹性搜索 (7.4.0) 的项目。

我正在尝试使用搜索

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

这会给出 200 响应,但没有结果。

使用 Postman 尝试相同的操作会返回 1 个结果。

POST http://localhost:9200/doc/_search
{
    "query": {
        "match": { "title": "castle" }
    }
}

我不知道为什么search 函数不起作用。我还测试了get、add 和delete,它们都可以工作。

为了创建索引,我使用了:

await client.indices.create({ index: "doc" });

要添加文档,我使用:

await client.index({
    index: 'doc',
    body: {
        title: "Castle Title",
        body: "This is text that is not the title."
    }
});

我做错了什么?

【问题讨论】:

    标签: node.js elasticsearch


    【解决方案1】:

    我已经对此进行了测试,它可以工作,唯一的问题是 elasticsearch 几乎可以实时搜索。您需要等待 1 秒钟,然后文档才可搜索。 基本上,如果您正在运行测试或其他东西,在搜索之前保存记录的位置,您需要:

    1. 搜索前等待 1 秒
    2. 触发手动刷新https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html
    3. 保存时等待刷新https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-refresh.html

    【讨论】:

    • 这就是问题所在。我有测试设置通过删除和重新创建索引来清除数据库。添加await client.indices.refresh({ index: "doc" }); 可以解决所有问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 2015-11-11
    相关资源
    最近更新 更多