solr的cache在SolrIndexSearcher中的使用

先说一下,我使用的sorl的版本是5.5.3

 

上一个博客说完了solr中的cache,只是介绍了他的实现原理以及配置,并没有发现程序是如何使用他的,这个博客就是说这个,SolrIndexSearcher中对cache的使用。先说一下SolrIndexSearcher,这个就是对lucene的indexSearcher的封装,然后再查询的时候先从缓存中查,如果没有再从lucene中查找,然后放到cache中,

在这个类中的cache的意思为:

1、filterCache:key是query,value是docSet,即对应的倒排表,但是他是无序的,很好理解,可能存在key是booleanQuery(包含多个query),value是对应的倒排表,但是是没有排序的,这个用来做不排序时候的缓存(几乎不用),更多的是做docSet的来源(当facet的时候会很有用)。

2、queryResultCache:key是根据query、filterquery、sort封装的对象,value是结果,即最终要返回给前端的结果

3、documentCache:对应于lucene的doc(int id)方法的返回结果,只不过这个是从缓存中查找。

4、fieldValueCache:这个我暂时没有搞懂他的用处,在solrConfig的构造器中可以发现即使在配置中没有配置也会自动创建一个。

 

我们看看他的search方法:

  public QueryResult search(QueryResult qr, QueryCommand cmd) throws IOException {
    getDocListC(qr,cmd);//调用的是getDocListC,C就是cache的意思,即优先从缓存中查找结果。
    return qr;
  }

 所以最终的就是这个getDocListC方法:

 private void getDocListC(QueryResult qr, QueryCommand cmd) throws IOException {
    DocListAndSet out = new DocListAndSet();//有的查询不仅仅是要docList,即排好序的doc,也需要docSet,即一个集合,比如在做facet的时候就需要这个。
    qr.setDocListAndSet(out);
    QueryResultKey key=null;
    int maxDocRequested = cmd.getOffset() + cmd.getLen();//一共需要多少doc,即偏移量+rows。
    // check for overflow, and check for # docs in index
    if (maxDocRequested < 0 || maxDocRequested > maxDoc()) maxDocRequested = maxDoc();
    int supersetMaxDoc= maxDocRequested;
    DocList superset = null;

    int flags = cmd.getFlags();//这个flag是根据我们查询时的参数定义的。
    Query q = cmd.getQuery();
    if (q instanceof ExtendedQuery) {//一般的查询不会进入if
      ExtendedQuery eq = (ExtendedQuery)q;
      if (!eq.getCache()) {
        flags |= (NO_CHECK_QCACHE | NO_SET_QCACHE | NO_CHECK_FILTERCACHE);
      }
    }
   //如果这个SolrIndexSearcher是带有缓存的并且此次查询没有指定docSet的filter(并不是我们在查询时的fq),并且没有进制使用cache和更新cache。
    if (queryResultCache != null && cmd.getFilter()==null
        && (flags & (NO_CHECK_QCACHE|NO_SET_QCACHE)) != ((NO_CHECK_QCACHE|NO_SET_QCACHE)))
    {
        // all of the current flags can be reused during warming,
        // so set all of them on the cache key.
        key = new QueryResultKey(q, cmd.getFilterList(), cmd.getSort(), flags);//封装此次的query、filter、sort进入queryKey,即在缓存中的key。
        if ((flags & NO_CHECK_QCACHE)==0) {//如果没有禁止使用cache
          superset = queryResultCache.get(key);//此处使用了queryResultCache,即根据查询的query、filter、sort来查询,

          if (superset != null) {//cache中有命中
            if ((flags & GET_SCORES)==0 || superset.hasScores()) {//如果请求中没有要求计算分数或者是之前在缓存中的结果已经时计算得分的了,即一定会满足我们的要求
              out.docList = superset.subset(cmd.getOffset(),cmd.getLen());//根据查询的要求,从中选择一部分,根据偏移量(start)和rows来选取要返回的结果(结果是docList,还没有涉及到docSet)
            }
          }
         //上面的可能因为start(即偏移量太大)而没有结果(因为queryKey不包含start和rows),或者根本就没有进入if(superset != null)而没有结果,即docList==null

          if (out.docList != null) {//如果上面的操作有了结果。
            if (out.docSet==null && ((flags & GET_DOCSET)!=0) ) {//需要获得docSet
              if (cmd.getFilterList()==null) {
                out.docSet = getDocSet(cmd.getQuery());//getDocSet中就使用了filterQuery,等会再看。
              } else {
                List<Query> newList = new ArrayList<>(cmd.getFilterList().size()+1);
                newList.add(cmd.getQuery());
                newList.addAll(cmd.getFilterList());
                out.docSet = getDocSet(newList);
              }
            }
            return;
          }
        }
      if ((flags & NO_SET_QCACHE) == 0) {
        if (maxDocRequested < queryResultWindowSize) {//queryResultWindowSize在solrConf.xml中也有配置,因为要做缓存,这个值指定每一次从索引中至少查找的最小值,这样在前几页浏览的时候就不用从索引中再次查找了。
          supersetMaxDoc=queryResultWindowSize;
        } else {
          supersetMaxDoc = ((maxDocRequested -1)/queryResultWindowSize + 1)*queryResultWindowSize;//这么做的理由是将要查询的doc的数量变为queryResultWindowSize的倍数。
          if (supersetMaxDoc < 0) supersetMaxDoc=maxDocRequested;
        }
      } else {
        key = null;  // we won't be caching the result
      }
    }
    cmd.setSupersetMaxDoc(supersetMaxDoc);

    //走到这里说明上面没有命中缓存或者即使命中了但是没有指定的部分(比如start太大),所以必须要从索引中查找了
    boolean useFilterCache=false;//能不能使用filterCache作为返回结果,前提是不排序,因为filterCache是没有排序的
    if ((flags & (GET_SCORES|NO_CHECK_FILTERCACHE))==0 && useFilterForSortedQuery && cmd.getSort() != null && filterCache != null) {//如果满足这一堆条件,useFilterForSortedQuery默认是不满足的,通常情况下也会使用得分排序,所以这个不满足
      useFilterCache=true;
      SortField[] sfields = cmd.getSort().getSort();
      for (SortField sf : sfields) {
        if (sf.getType() == SortField.Type.SCORE) {
          useFilterCache=false;
          break;
        }
      }
    }
    if (useFilterCache) {//几乎都不满足这个
      if (out.docSet == null) {
        out.docSet = getDocSet(cmd.getQuery(),cmd.getFilter());
        DocSet bigFilt = getDocSet(cmd.getFilterList());
        if (bigFilt != null) out.docSet = out.docSet.intersection(bigFilt);
      }
      sortDocSet(qr, cmd);
    } else {
      // do it the normal way...
      if ((flags & GET_DOCSET)!=0) {
        DocSet qDocSet = getDocListAndSetNC(qr,cmd);//NC,即not cache,即从索引中查找docSet和docList
        //存放的是query的docSet,是没有filter的
        if (qDocSet!=null && filterCache!=null && !qr.isPartialResults()) filterCache.put(cmd.getQuery(),qDocSet);//filterCache中存放的是query+docSet,是不排序的,
      } else {
        getDocListNC(qr,cmd);
      }
      assert null != out.docList : "docList is null";
    }

   //这中间有一段cursor的,由于没有接触cursor,所以删了这一部分。

    // lastly, put the superset in the cache if the size is less than or equal to queryResultMaxDocsCached
    if (key != null && superset.size() <= queryResultMaxDocsCached && !qr.isPartialResults()) {//queryResultMaxDocsCached 也是在solrConf中配置的,表示可以缓存的最大的doc的数量,如果太大就不能缓存了。
      queryResultCache.put(key, superset);//放入queryResultCache中去,key是封装的query、filter、sort的对象。
    }
  }

 

还有一个方法,即getDocSet,

  public DocSet getDocSet(Query query) throws IOException {
    if (query instanceof ExtendedQuery) {//不进入这个if
      ExtendedQuery eq = (ExtendedQuery)query;
      if (!eq.getCache()) {
        if (query instanceof WrappedQuery) {
          query = ((WrappedQuery)query).getWrappedQuery();
        }
        query = QueryUtils.makeQueryable(query);
        return getDocSetNC(query, null);
      }
    }

//将这个query变为正query,因为某些query可能是-name:james,即名字不是james的,要变为name:james
    Query absQ = QueryUtils.getAbs(query);
    boolean positive = query==absQ;
    if (filterCache != null) {
      DocSet absAnswer = filterCache.get(absQ);//尝试从filterCache中获得结果
      if (absAnswer!=null) {
        if (positive) return absAnswer;
        else return getPositiveDocSet(matchAllDocsQuery).andNot(absAnswer);
      }
    }

    DocSet absAnswer = getDocSetNC(absQ, null);//如果没有找到,则从索引中查找,
    DocSet answer = positive ? absAnswer : getPositiveDocSet(matchAllDocsQuery).andNot(absAnswer);

    if (filterCache != null) {//找到后放入到缓存中,可以发现filterQuery只是根据query进行的操作,不会有filter什么事情
      filterCache.put(absQ, absAnswer);
    }
    return answer;
  }

 

还有个cache,documentCache没有用到,这个就更简单了,在SolrIndexSearcher中有个doc方法,doc(int id),里卖的实现就是先从这个documentCache中招,找不到再调用lucene的indexSearcher从索引中找

 

至此,已经完全搞懂三个缓存的意义了:

1、filterCache用于docSet,用于处理fq参数(在facet和group中也会用到它),如果在查询的时候开启了fq,就会查询这个fiterCache(这一部分的代码很多我没有贴)

2、queryResultCache:用于缓存根据query+filter+sort+flag组成的query进行缓存。与start和row无关

3、documentCache:用于缓存最后的document。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值