2017-07-30 479 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 
     }) 
    ); 
} 

回答

相關問題