删除后仍然找到 Elasticsearch 文档?

Elasticsearch document still found after deletion?

我有一个 API 运行 快递。我有两个资源(搜索和删除)。 我正在 运行 查询删除文档。删除后,删除成功记录到控制台。 删除请求完成后,我 运行 针对 API 的另一个请求刷新 UI 中的列表。这些文件仍然包含之前删除的文件(肯定是被删除了,因为我从 ES 那里得到了它被删除的回复)。我保证,删除后搜索是运行。而且它仍然包含之前删除的文档。

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

现在的问题是……是我的错吗?是 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
        })
    );
}

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

Elasticsearch: Found the deleted document?