我有一个使用express运行的API。我有两个资源(搜索和删除)。 我正在运行删除文档的查询。删除后,删除成功将记录到控制台。 删除请求完成后,我针对API运行另一个请求以刷新UI中的列表。这些文件仍然包含先前删除的文件(该文件已被删除,因为我从ES得到了它被删除的回复)。我保证,搜索是在删除后运行的。它仍包含先前删除的文档。
当我再次运行相同搜索查询时,已删除的文档不再出现在文档中。
现在的问题是......这是我的错吗?它是ES.js库中的缓存问题吗?是否是lucene处理删除的时间问题?
一些环境信息:
后端:表达^ 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
})
);
}
答案 0 :(得分:1)
我在相关问题中找到了答案。对我的代码做了一点改动就成了诀窍 - 删除后我只是刷新索引,因为删除时尚未刷新(GET是实时的,其他一些API不是)。