solr DocumentCache的問題



發現如果不使用Document Cache的時候,solr內部還是會調用兩次 

第一次,在QueryComponent裏會有處理搜索,取得result,除了內部lucene id外,會調用doPrefetch方法,取出doc文檔 放在DocumentCache緩存裏,


便於下次使用,如果當前沒有使用DocumentCache的話,發現還是會調用該方法去拿Document出來。。









responseWriter的時候也會調用doc(int ,Set<String>)方法取Document,如果有使用document Cache的話,可以得到之前已緩存的數據,不用再去索引裏取數據,但如果沒有用cache的話,就會去查詢。這樣,會重複調用兩次取document數據

先看正向取文檔時的代碼如下:

  public Document doc(int i, Set<String> fields) throws IOException {
    
    Document d;
    if (documentCache != null) {
      d = documentCache.get(i);
      if (d!=null) return d;
    }

    if(!enableLazyFieldLoading || fields == null) {
      d = getIndexReader().document(i);
    } else {
      d = getIndexReader().document(i, 
             new SetNonLazyFieldSelector(fields));
    }

    if (documentCache != null) {
      documentCache.put(i, d);
    }

    return d;
  }

或者應該修改的地方,應該在沒有使用document cache的地方不調用該方法:

doPrefetch(rb);

應該修改判定條件

  protected void doPrefetch(ResponseBuilder rb) throws IOException
  {
    SolrQueryRequest req = rb.req;
    SolrQueryResponse rsp = rb.rsp;
    //pre-fetch returned documents
    if (!req.getParams().getBool(ShardParams.IS_SHARD,false) && rb.getResults().docList != null && rb.getResults().docList.size()<=50) {
      // TODO: this may depend on the highlighter component (or other components?)
      SolrPluginUtils.optimizePreFetchDocs(rb.getResults().docList, rb.getQuery(), req, rsp);
    }
  }


改爲:

  protected void doPrefetch(ResponseBuilder rb) throws IOException
  {
    SolrQueryRequest req = rb.req;
    SolrQueryResponse rsp = rb.rsp;
    //pre-fetch returned documents
    if (documentCache != null&&
!req.getParams().getBool(ShardParams.IS_SHARD,false) && rb.getResults().docList != null && rb.getResults().docList.size()<=50) {
      // TODO: this may depend on the highlighter component (or other components?)
      SolrPluginUtils.optimizePreFetchDocs(rb.getResults().docList, rb.getQuery(), req, rsp);
    }
  }






發佈了94 篇原創文章 · 獲贊 77 · 訪問量 60萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章