2017-07-30 41 views
0

我有一个使用express运行的API。我有两个资源(搜索和删除)。 我正在运行查询以删除文档。删除后,删除成功记录到控制台。 删除请求完成后,我运行另一个针对API的请求来刷新UI中的列表。这些文档仍然包含之前删除的文档(该文档已经被删除,因为我从ES获得了它被删除的响应)。我保证,搜索是在删除后运行的。它仍然包含之前删除的文档。Elasticsearch文件在删除后仍然找到?

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

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

一些环境信息:

后端:快递^ 4.13.3,使用快递 - 诺路由器^ 2.0.0,8.1.2节点

前端:爱可信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); 
    } 
}); 

这里是代码FR om我的服务:

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

回答

相关问题