原文出自:http://blog.csdn.net/flyingpig4/article/details/6305488

<pre name="code" class="java">SearchHandler.handleRequestBody():

solr搜索主流程

1.调用:RequestHandlerBase

|--handleRequest(req, rsp);

2.源码分析:

|-prepare() |前期的请求参数准备工作,QueryComponent可以在schemal文件中设置,易扩展

|-- SearchComponent c : components   

               |--queryComponent.prepare()  





|--FQ,FL,SHARDS,Q.SHARDS.ROWS,SHARDS.START  |设置查询的条件,包括分片的查询条件  

|--设置fieldFlags返回的field值

|--设置defType值 |默认lucene,实际指向的是lucenePlugin,可在schema文件中扩展

|--解析QueryParser |对queryString进行解析

      |--解析fq过滤值,生成filter过滤query集

|--设定shards分片信息

|--statComponent.prepare()   

      |--debugComponent.prepare()









|--process()   |单组无shard情况处理

|--String ids = params.get(ShardParams.IDS); |获取shardDoc的id集合,不为空往下执行

|--通过ids获取文档id的集合,并为下一步获取DocList做准备

|--int id = req.getSearcher().getFirstMatch(   

                |--new Term(idField.getName(), idField.getType().toInternal(idArr.get(i))));

|-- luceneIds[docs++] = id;  

|-- res.docList = new DocSlice(0, docs, luceneIds, null, docs, 0);





                |--if (rb.isNeedDocSet()) 存在DocSet需求

|--queries.add(rb.getQuery()); |queryString的查询条件

|--queries.addAll(filters);    |filter查询条件

|--res.docSet = searcher.getDocSet(queries);  |通过queries条件进行DocSet查询

|--rb.setResults(res);  |设置结果返回值

       |--rsp.add("response",rb.getResults().docList);

|--return; |获取结果集后返回

 

|--SolrIndexSearcher.QueryCommand cmd = rb.getQueryCommand();  |获取查询封装的QueryCommand对象

|--searcher.search(result,cmd);

|--getDocListC(qr,cmd); 

|--rb.setResult( result );   |与上同

    |--rsp.add("response",rb.getResults().docList);





|--boolean fsv = req.getParams().getBool(ResponseBuilder.FIELD_SORT_VALUES,false);   |设定fsv,用于排序字段处理

|--SortField[] sortFields  |获取排序的域

|--Field field = new Field("dummy", "", Field.Store.YES, Field.Index.NO); //  仿冒域

|--SolrIndexReader[] readers = reader.getLeafReaders();   |获取所有的IndexReader结点

|--if (readers.length==1)  subReader = readers[0]; |直接处理

|--for (SortField sortField: sortFields)  |循环处理各个排序域

|--FieldComparator comparators[] = (readers==null) ? null : new FieldComparator[readers.length]; |声明域比较器

|--DocIterator it = rb.getResults().docList.iterator(); |过滤docList集合

|--while(it.hasNext())

|--int doc = it.nextDoc();

|--idx = SolrIndexReader.readerIndex(doc, offsets); |获取id所在的Indexreader

|--comparator.copy(0, doc); 

|--Object val = comparator.value(0); |获取comparator的value值

|-- vals.add(val);  |添加至vals当中

|--sortVals.add(fieldname, vals);  |添加至排序值当中

|--rsp.add("sort_values", sortVals); |设置响应对象Response的sort_values的排序值









|--process()   |多组shard处理

|--初始化操作

|-- HttpCommComponent comm = new HttpCommComponent();

|-- rb.outgoing = new LinkedList<ShardRequest>();

|-- rb.finished = new ArrayList<ShardRequest>();

|--while (nextStage != Integer.MAX_VALUE) |循环执行,设置stage状态

|-- for( SearchComponent c : components ) {

                 |--nextStage = Math.min(nextStage, c.distributedProcess(rb)); |distributedProcess(rb)根据rb的stage状态做处理

       

|-- while (rb.outgoing.size() > 0)

|--while (rb.outgoing.size() > 0)

|--ShardRequest sreq = rb.outgoing.remove(0); |取第一个请求值

|--for (String shard : sreq.actualShards)  

|--ModifiableSolrParams params = new ModifiableSolrParams(sreq.params); |设置参数

|--comm.submit(sreq, shard, params); |提交http请求,执行一个Task任务,获取Response响应

|--while (rb.outgoing.size() == 0)  

|--ShardResponse srsp = comm.takeCompletedOrError();  |获取响应

|--rb.finished.add(srsp.getShardRequest());   |完成此次搜索

|--for(SearchComponent c : components)        |完成返回的Response的处理

             
|--c.handleResponses(rb, srsp.getShardRequest());

|-- for(SearchComponent c : components) 

          |-- c.finishStage(rb);  |rb请求响应完成处理

         

         







|--SolrIndexSearcher.getDocSet(List<query>)

|--if (queries.size()==1) return getDocSet(queries.get(0)) |Query集合为1则直接处理

|--for (int i=0; i<sets.length; i++)  |集合存在多个query,且query可能为not查询

|--Query q = queries.get(i);  |取到第i个query

|--Query posQuery = QueryUtils.getAbs(q);  |如果为negative,则为not,则返回相应的补集

|--sets[i] = getPositiveDocSet(posQuery);  |查询query获取DocSet集合

|--if (q==posQuery) |判断是否为negative

|--nge[i] = false;    |为后续判断

|--answer = sets[i];  |是postivi查询设置返回的answer值

|--neg[i] = true;     |否则为正向

|--if(answer == null) answer = getPositiveDocSet(matchAllDocsQuery);  |如果answer为空,则无positive query

|--for (int i=0; i<sets.length; i++)   

|-- if (neg[i]) answer = answer.andNot(sets[i]);    |合并negative查询的answer结果集,并取补集

for (int i=0; i<sets.length; i++) 

|-- if (!neg[i] && i!=smallestIndex) 

|--answer = answer.intersection(sets[i]);   |--循环set集合,取交集生成新的DocSet,详见HashDocSet

|--return answer;

    





|--SolrIndexSearcher.getPositiveDocSet(Query q)

|--if (filterCache != null)

|--answer = (DocSet)filterCache.get(q);

|--else

|--answer = getDocSetNC(q,null);  |normal way to get DocSet

|--if (filterCache != null) filterCache.put(q,answer);
|保存至filterCache当中





 

|--getDocListC(QueryResult qr, QueryCommand cmd)  |通过QueryCommmand进行查找 

|--if (queryResultCache != null && cmd.getFilter()==null) |从queryResultCache中进行查找

|--key = new QueryResultKey(cmd.getQuery(), cmd.getFilterList(), cmd.getSort(), cmd.getFlags());  |计算key值

|--if ((cmd.getFlags() & NO_CHECK_QCACHE)==0)

|--superset = (DocList)queryResultCache.get(key);  |从cache中取出DocList集合

|--out.docList = superset.subset(cmd.getOffset(),cmd.getLen());  |如果子集合不为空

|--if (out.docSet==null && ((cmd.getFlags() & GET_DOCSET)!=0) ) 

               |--if (cmd.getFilterList()==null)    |如果filter为null,则直接查询

|--out.docSet = getDocSet(cmd.getQuery()); |获取DocSet集合

|--else 

|--newList.addAll(cmd.getFilterList());  |添加filterList的Doc集合

|--out.docSet = getDocSet(newList);    |获取DocSet集合

|--return ;查询完成返回

|--boolean useFilterCache=false;  |检查是否需要filterCache

|--for (SortField sf : sfields)   |如果存在SortField,则返回false

|--if (sf.getType() == SortField.SCORE) 

|--useFilterCache=false;

|--break;

|--if(useFilterCache)

|--if (out.docSet == null)  |获取真实的DocSet集合

       |--out.docSet = getDocSet(cmd.getQuery(),cmd.getFilter());

|--DocSet bigFilt = getDocSet(cmd.getFilterList());

|--if (bigFilt != null) out.docSet = out.docSet.intersection(bigFilt);

|--superset = sortDocSet(out.docSet,cmd.getSort(),supersetMaxDoc);  |取出子的设定的返回Doc

|--out.docList = superset.subset(cmd.getOffset(),cmd.getLen());     |设置docList

|--else          |此时无任何缓存,为正常处理模式

|--if ((cmd.getFlags() & GET_DOCSET)!=0) 

|--DocSet qDocSet = getDocListAndSetNC(qr,cmd);

|--else 

|--getDocListNC(qr,cmd);

|--superset = out.docList;

     |--out.docList = superset.subset(cmd.getOffset(),cmd.getLen());

|--if (key != null && superset.size() <= queryResultMaxDocsCached && !qr.isPartialResults())  |设置是否需要设置queryResultCache

       |--queryResultCache.put(key, superset);

    



|--getDocListAndSetNC(QueryResult qr,QueryCommand cmd)

|--DocSet filter = cmd.getFilter()!=null ? cmd.getFilter() : getDocSet(cmd.getFilterList()); |获取filter过滤信息

|--Query query = QueryUtils.makeQueryable(cmd.getQuery()); |设定query为positive的Query条件

|--final Filter luceneFilter = filter==null ? null : filter.getTopFilter();   

|--if (lastDocRequested<=0)

|--if (!needScores)  |是否需要打分

|--collector = setCollector = new DocSetCollector(smallSetSize, maxDoc);

|--else

|--collector = setCollector = new DocSetDelegateCollector(smallSetSize, maxDoc, new Collector()

|--super.search(query, luceneFilter, collector);  |执行查询条件,获取collector信息,前面操作均为构造查询条件,并作相应的缓存的存取处理

|--set = setCollector.getDocSet();

|--else 

|--处理过程与上类似,但参数设置不同,如下 

|--totalHits = topCollector.getTotalHits();

|--TopDocs topDocs = topCollector.topDocs(0, len);

|--ids[i] = scoreDoc.doc;

|--qr.setDocList(new DocSlice(0,sliceLen,ids,scores,totalHits,maxScore));

|--qr.setDocSet(set);

|--return filter==null ? qr.getDocSet() : null;



</pre><br>

Solr查询过程源码分析的更多相关文章

  1. Mybatis执行流程源码分析

    第一部分:项目结构 user_info表:只有id和username两个字段 User实体类: public class User { private String username; private ...

  2. Android系统默认Home应用程序(Launcher)的启动过程源码分析

    在前面一篇文章中,我们分析了Android系统在启动时安装应用程序的过程,这些应用程序安装好之后,还须要有一个Home应用程序来负责把它们在桌面上展示出来,在Android系统中,这个默认的Home应 ...

  3. [Android]从Launcher开始启动App流程源码分析

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5017056.html 从Launcher开始启动App流程源码 ...

  4. [Android]Android系统启动流程源码分析

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5013863.html Android系统启动流程源码分析 首先 ...

  5. Android Content Provider的启动过程源码分析

    本文參考Android应用程序组件Content Provider的启动过程源码分析http://blog.csdn.net/luoshengyang/article/details/6963418和 ...

  6. Android应用程序绑定服务(bindService)的过程源码分析

    Android应用程序组件Service与Activity一样,既能够在新的进程中启动,也能够在应用程序进程内部启动:前面我们已经分析了在新的进程中启动Service的过程,本文将要介绍在应用程序内部 ...

  7. Spring加载流程源码分析03【refresh】

      前面两篇文章分析了super(this)和setConfigLocations(configLocations)的源代码,本文来分析下refresh的源码, Spring加载流程源码分析01[su ...

  8. 【高速接口-RapidIO】5、Xilinx RapidIO核例子工程源码分析

    提示:本文的所有图片如果不清晰,请在浏览器的新建标签中打开或保存到本地打开 一.软件平台与硬件平台 软件平台: 操作系统:Windows 8.1 64-bit 开发套件:Vivado2015.4.2 ...

  9. 转:Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析

    原文地址:Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析 前言 本文将分析mybatis与spring整合的MapperScannerConfigur ...

随机推荐

  1. [置顶] kubernetes1.7新特性:新增StorageOS卷插件和Local持久存储

    背景介绍 在Kubernetes中卷的作用在于提供给POD存储,这些存储可以挂载到POD中的容器上,进而给容器提供存储. 从图中可以看到结构体PodSpec有个属性是Volumes,通过这个Volum ...

  2. C++复制构造函数的实现

    复制构造函数是一种特殊的构造函数,有一般构造函数的特性.它的功能是用一个已知的对象来初始化一个被创建的同类对象.复制构造函数的参数传递方式必须按引用来进行传递,请看实例: #include <i ...

  3. 机器学习(九)—FP-growth算法

    本来老师是想让我学Hadoop的,也装了Ubuntu,配置了Hadoop,一时间却不知从何学起,加之自己还是想先看点自己喜欢的算法,学习Hadoop也就暂且搁置了,不过还是想问一下园子里的朋友有什么学 ...

  4. 定时框架quartz的一些问题总结

    1 什么是Quartz Quartz是OpenSymphony开源组织在Job scheduling领域的开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单 ...

  5. kali视频学习(11-15)

    第四周kali视频(11-15)学习 11.漏洞分析之OpenVAS使用 12.漏洞分析之扫描工具 13.漏洞分析之WEB爬行 14.漏洞分析之WEB漏洞扫描(一) 15.漏洞分析之WEB漏洞扫描(二 ...

  6. asp.net core microservices 架构之Task 事务一致性 事件源 详解

    一 aspnetcore之task的任务状态-CancellationToken 我有一篇文章讲解了asp.net的线程方面的知识.我们知道.net的针对于多线程的一个亮点就是Task,net clr ...

  7. Hibernate中 一 二级缓存及查询缓存(2)

    缓存:缓存是什么,解决什么问题?  位于速度相差较大的两种硬件/软件之间的,用于协调两者数据传输速度差异的结构,均可称之为缓存Cache.缓存目的:让数据更接近于应用程序,协调速度不匹配,使访问速度更 ...

  8. 剑指offer-第四章解决面试题思路(复杂链表的复制)

    题目:请写一个函数clone(ComplexListNode pHead),实现复杂链表的复制. 复杂链表的数据结构如下:public class ComplexListNode{int m_nVal ...

  9. win10开始键点击无效果

    1.在键盘上按下win+R键,或在开始菜单图标上点击右键选择运行: 2.输入powershell,按下“确定”运行:3.在窗口里输入或复制粘贴以下命令,注意只有一行: Get-AppxPackage ...

  10. assembly 需要 unload 和 update 的时候怎么办?

    我正在开发公司的业务组件平台,组件池的灵活性要求很高,业务组件都是可以立即更新和及时装配的;目前完成这些功能,有待测试.用appDomain.unload 拆卸assembly 可以,只是用起来比较麻 ...