【问题标题】:Elasticsearch document still found after deletion?删除后仍然找到Elasticsearch文档?
【发布时间】:2017-07-30 22:07:43
【问题描述】:

我有一个使用 express 运行的 API。我有两个资源(搜索和删除)。 我正在运行查询以删除文档。删除后,删除成功将记录到控制台。 删除请求完成后,我对 API 运行另一个请求以刷新 UI 中的列表。这些文档仍然包含先前已删除的文档(已明确删除,因为我收到了 ES 的回复,即它已被删除)。我保证,搜索是在删除后运行的。它仍然包含之前删除的文档。

当我再次运行 same 搜索查询时,已删除的文档不再出现在文档中。

现在的问题是……是我的错吗?这是 ES.js 库的缓存问题吗? lucene 处理删除是否存在时间问题?

一些环境信息:

后端:express ^4.13.3,使用 express-promise-router ^2.0.0,节点 8.1.2

前端:axios 0.16.2

这是我的休息资源:

api.get('/searchDocuments', async (req, res) => {
    const result = await DocumentService.searchDocuments(req.query.searchQuery, req.query.from || 0, req.query.size || 10);
    console.log(result.search.hits);
    res.reply({
        search: {
            hits: result.search.hits.total,
            data: result.search.hits.hits.map(hit => hit._source)
        }
    });
});

api.delete('/:documentId', async (req, res) => {
    console.log(`Deleting document ${req.params.documentId}`);
    const result = await DocumentService.deleteDocument(req.params.documentId);
    console.log(`Done deleting document ${req.params.documentId}: ${result}`);
    if (result && result.found) {
        res.reply(undefined, 'deleted');
    } else {
        res.reply('not found', 404);
    }
});

这是我的服务中的代码:

async searchDocuments(searchQuery: string, from: number = 0, size: number = 10) {
    const result = {};
    const operations = [];
    const query = {
        index: 'documents',
        body: {
            from: from * size,
            size: size
        }
    };
    if (searchQuery) {
        const targetFields = [
            'title^4',
            'tags^5',
            'metadata.key^2',
            'metadata.value^3',
            '_all'
        ];
        query.body.query = {
            simple_query_string : {
                query: searchQuery,
                analyzer: 'simple',
                fields: targetFields,
                default_operator: 'and'
            }
        };

        operations.push(async () => {
            result.suggestions = await ElasticsearchService.wrap(
                ElasticsearchService.client.search({
                    index: 'documents',
                    body: {
                        size: 0,
                        aggs: {
                            autocomplete: {
                                terms: {
                                    field: 'autocomplete',
                                    order: {
                                        _count: 'desc'
                                    },
                                    include: {
                                        pattern: `${searchQuery}.*`
                                    }
                                }
                            }
                        },
                        query: {
                            prefix: {
                                autocomplete: {
                                    value: searchQuery
                                }
                            }
                        }
                    }
                })
            )
        });

    }
    operations.push(async () => {
        result.search = await ElasticsearchService.wrap(
            ElasticsearchService.client.search(query)
        );
    });

    await Promise.all(operations.map(o => o()));
    return result;
}

async deleteDocument(documentId: string) {
    const document = await this.getDocument(documentId);
    const deleteTagActions = [];
    if (document && document.found) {
        for (const tag of document._source.tags) {
            deleteTagActions.push((async () => {
                const tagCountResult = await TagService.countDocumentsWithTag(tag);
                if (tagCountResult.count === 1) {
                    await TagService.deleteTag(tag);
                    console.log(`Deleting tag ${tag}`);
                }
            })());
        }
    }
    await Promise.all(deleteTagActions);
    return await ElasticsearchService.wrap(
        ElasticsearchService.client.delete({
            index: 'documents',
            type: 'documents',
            id: documentId
        })
    );
}

【问题讨论】:

    标签: node.js express elasticsearch axios


    【解决方案1】:

    我在一个相关问题中找到了答案。对我的代码稍作改动就可以解决问题 - 删除后我只是刷新索引,因为删除尚未刷新(GET 是实时的,其他一些 API 不是)。

    Elasticsearch: Found the deleted document?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-13
      • 2021-06-09
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      • 2017-05-22
      • 2010-10-19
      • 1970-01-01
      相关资源
      最近更新 更多