转载 http://13shu.iteye.com/blog/743825
1、请求被solr的过滤器拦截转发到RequestHandlerBase中的handleRequest()
2、handleRequest()中调用handleRequestBody()(抽象的)
根据请求参数qt=standard查找solrconfig.xml配置文件(SolrConfig初始化)中找到SearchHandler类
Xml代码
- <requestHandler name="standard" class="solr.SearchHandler"
- default="true">
- <!-- default values for query parameters -->
- <lst name="defaults">
- <str name="echoParams">explicit</str>
- <!--
- <int name="rows">10</int>
- <str name="fl">*</str>
- <str name="version">2.1</str>
- -->
- </lst>
- </requestHandler>
3、调用SearchHandler中handleRequestBody()去遍历查询组件集合
Java代码
- for( SearchComponent c : components ) {
- c.prepare(rb);
- }
4、以QueryComponent为例执行prepare(),根据defType参数得到QParser(默认=lucene)
Java代码
- QParser parser = QParser.getParser(rb.getQueryString(), defType, req);
5、通过defType参数获得QParserPlugin
Java代码
- public static QParser getParser(String qstr, String defaultType, SolrQueryRequest req) throws ParseException {
- SolrParams localParams = QueryParsing.getLocalParams(qstr, req.getParams());
- String type;
- if (localParams == null) {
- type = defaultType;
- } else {
- String localType = localParams.get(QueryParsing.TYPE);
- type = localType == null ? defaultType : localType;
- qstr = localParams.get("v");
- }
- type = type==null ? QParserPlugin.DEFAULT_QTYPE : type;
- <span style="color: #ff0000;">QParserPlugin qplug = req.getCore().getQueryPlugin(type);</span>
- return qplug.createParser(qstr, localParams, req.getParams(), req);
- }
6、QueryComponent中调用解析
Java代码
- QParser parser = QParser.getParser(rb.getQueryString(), defType, req);
- rb.setQuery( <span style="color: #ff0000;">parser.getQuery()</span> );
7、解析完成返回
Java代码
- public Query parse() throws ParseException {
- String qstr = getString();
- String defaultField = getParam(CommonParams.DF);
- if (defaultField==null) {
- defaultField = getReq().getSchema().getDefaultSearchFieldName();
- }
- lparser = new SolrQueryParser(this, defaultField);
- // these could either be checked & set here, or in the SolrQueryParser constructor
- String opParam = getParam(QueryParsing.OP);
- if (opParam != null) {
- lparser.setDefaultOperator("AND".equals(opParam) ? QueryParser.Operator.AND : QueryParser.Operator.OR);
- } else {
- // try to get default operator from schema
- QueryParser.Operator operator = getReq().getSchema().getSolrQueryParser(null).getDefaultOperator();
- lparser.setDefaultOperator(null == operator ? QueryParser.Operator.OR : operator);
- }
- return lparser.parse(qstr);
- }