Solr实现 并集式、多值、复杂 过滤查询的权限【转】
公司开发使用的搜索引擎核心是Solr,但是应为业务原因,需要相对复杂权限机制。
1)通过Solr的filterQuery可以实现field过滤,实现过滤项的效果。索引
A{filter1:a,field2:a,field3:a,field4:1}
B{filter1:b,field2:b,field3:b,field4:2}
C{filter1:c,field2:c,field3:c,field4:3}
过滤条件
fq=field:b
结果
B{filter1:b,field2:b,field3:b,field4:b}
2)通过Solr的数字区间,也能方便实现密级的权限方式。索引
A{filter1:a,field2:a,field3:a,field4:1}
B{filter1:b,field2:b,field3:b,field4:2}
C{filter1:c,field2:c,field3:c,field4:3}
过滤条件
fq=field4:[2 TO *]
结果
B{filter1:b,field2:b,field3:b,field4:2}
C{filter1:c,field2:c,field3:c,field4:3}
3)但是真正常用的是多条件的、并集式的过滤项的效果(如:过滤出包涵filed1:a和filed1:b的所有索引)。
      在网上搜索了很久,也问了一些做搜索很长时间的同事,都没有找到解决方法,郁闷啊!最后决定自己仔细的把Solr源码读一读,看看有没有解决方法(一直觉得开发Solr大牛不太可能没有开发这个功能),功夫不负有心人,花了1天时间终于找到了完美的解决方法。
先说一下Solr的过滤机制是怎么实现的:
1、查询时将参数传入Request,经过处理,会调用SolrCore.execute()方法。
2、SolrCore.execute()会调用RequestHandlerBase.handleRequest()方法。
3、RequestHandlerBase.handleRequest()会调用Component,默认有6个Component。分别是QueryComponent、FacetComponent、MoreLikeThisComponent、HighlightComponent、StatsComponent、DebugComponent
4、fq是在QueryComponent解析的,
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
 | 
String[] fqs = req.getParams().getParams(CommonParams.FQ);if (fqs!=null && fqs.length!=0) {  List<Query> filters = rb.getFilters();  if (filters==null) {    filters = new ArrayList<Query>(fqs.length);  }  for (String fq : fqs) {    if (fq != null && fq.trim().length()!=0) {      QParser fqp = QParser.getParser(fq, null, req);      filters.add(fqp.getQuery());    }  }  // only set the filters if they are not empty otherwise  // fq=&someotherParam= will trigger all docs filter for every request   // if filter cache is disabled  if (!filters.isEmpty()) {    rb.setFilters( filters );  }} | 
实现对fq的解析,往里跟踪发现,是通过QueryParser.parse()将过滤条件转换成Query的。这个就是问题关键,既然filed:value形式的过滤条件可以实现,那filed:{value1 or value2}不就可以实现我想要的多值并集试的过滤吗?(QueryParser.parse()支持查询语法).
问题又来了,通过多次测试,发现value不能支持写查询表达式(纠结啊)。再一次耐心把代码看一下,突然发现一段奇怪的代码,QueryParsing.parseLocalParams()方法
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
 | 
int off = start;if (!txt.startsWith(LOCALPARAM_START, off)) return start;StrParser p = new StrParser(txt, start, txt.length());p.pos += 2; // skip over "{!"for (; ; ) {  /*  if (p.pos>=txt.length()) {    throw new ParseException("Missing '}' parsing local params '" + txt + '"');  }  */  char ch = p.peek();  if (ch == LOCALPARAM_END) {    return p.pos + 1;  }  String id = p.getId();  if (id.length() == 0) {    throw new ParseException("Expected identifier '}' parsing local params '" + txt + '"');  }  String val = null;  ch = p.peek();  if (ch != '=') {    // single word... treat {!func} as type=func for easy lookup    val = id;    id = TYPE;  } else {    // saw equals, so read value    p.pos++;    ch = p.peek();    boolean deref = false;    if (ch == '$') {      p.pos++;      ch = p.peek();      deref = true;  // dereference whatever value is read by treating it as a variable name    }    if (ch == '\"' || ch == '\'') {      val = p.getQuotedString();    } else {      // read unquoted literal ended by whitespace or '}'      // there is no escaping.      int valStart = p.pos;      for (; ; ) {        if (p.pos >= p.end) {          throw new ParseException("Missing end to unquoted value starting at " + valStart + " str='" + txt + "'");        }        char c = p.val.charAt(p.pos);        if (c == LOCALPARAM_END || Character.isWhitespace(c)) {          val = p.val.substring(valStart, p.pos);          break;        }        p.pos++;      }    }    if (deref) {  // dereference parameter      if (params != null) {        val = params.get(val);      }    }  }  if (target != null) target.put(id, val);} | 
仔细看了一下,发现用于解析类似{!dismax v=$auth}这样的字符传(一直都不太想看这段代码,太长了),作用是将$auth替换为传入solr的参数,解析为{!dismax v=field4:2},后用dismax转换为Query,添加到过滤查询里。
最终解决方法是:索引
A{filter1:a,field2:a,field3:a,field4:1}
B{filter1:b,field2:b,field3:b,field4:2}
C{filter1:c,field2:c,field3:c,field4:3}
| 
 1 
2 
3 
4 
5 
 | 
Map<String,Object> paramMap = new HashMap<String,Object>();SearchParams params = new SearchParams(paramMap) ;paramMap.put("auth","filed4:1 filed4:3"); //将过滤查询语句放入参数里query.add(params);query.addFilterQuery("{!dismax v=$auth}");//通过dismax转换为query,加入查询语句,大功告成 | 
结果:
A{filter1:a,field2:a,field3:a,field4:1}
C{filter1:c,field2:c,field3:c,field4:3}
附录
文中使用类的全路径
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
 | 
org.apache.solr.core.SolrCoreorg.apache.solr.handler.StandardRequestHandlerorg.apache.solr.handler.RequestHandlerBase.handleRequestorg.apache.solr.handler.StandardRequestHandlerorg.apache.solr.handler.component.SearchHandler.handleRequestBodyorg.apache.solr.handler.component.QueryComponentorg.apache.solr.handler.component.FacetComponentorg.apache.solr.handler.component.MoreLikeThisComponentorg.apache.solr.handler.component.HighlightComponentorg.apache.solr.handler.component.StatsComponentorg.apache.solr.handler.component.DebugComponentorg.apache.lucene.queryParser.QueryParserorg.apache.solr.search.QueryParsing | 
作者:明翼(XGogo)
-------------
公众号:TSparks
微信:shinelife
扫描关注我的微信公众号感谢
-------------

                
            
浙公网安备 33010602011771号