gpt4 book ai didi

node.js - 删除后仍然找到Elasticsearch文件?

转载 作者:行者123 更新时间:2023-12-03 01:44:09 25 4
gpt4 key购买 nike

我有一个Express运行的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?

关于node.js - 删除后仍然找到Elasticsearch文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45400577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com