-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctfQParser.java
113 lines (101 loc) · 4.52 KB
/
ctfQParser.java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* COPYRIGHT (C) 2015 Gavin Ruddy. All Rights Reserved. */
/**
* Calls preparatory queries in ctfBase or ctfClickBase, adds 2y items to query string, parses query & calls ctfScorer.
* @author Gavin Ruddy
* contact [email protected]
* @version 1.0.1 2015/8/01
*/
package com.ctf;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.ArrayList;
import org.apache.lucene.search.Query;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.QParser;
import static org.apache.solr.search.QParser.getParser;
import org.apache.solr.search.QParserPlugin;
import org.apache.solr.search.SyntaxError;
public class ctfQParser extends QParserPlugin {
private static long CSstarttime;
private static NamedList<String> ctfParams = new NamedList<String>();
private static ArrayList<String> primary = new ArrayList<String>();
private static ArrayList<String> secondary = new ArrayList<String>();
private static Integer period = 1000;
@SuppressWarnings("unchecked")
public void init(NamedList args) {
ctfParams.addAll(args);
SolrParams params = SolrParams.toSolrParams(args);
}
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
String base = localParams.get("base", params.get("base", (String) ctfParams.get("base")));
boolean restrict = Boolean.valueOf(localParams.get("restrict", params.get("restrict", (String) ctfParams.get("restrict"))));
boolean extend = Boolean.valueOf(localParams.get("extend", params.get("extend", (String) ctfParams.get("extend"))));
boolean only2y = Boolean.valueOf(localParams.get("only2y", params.get("only2y", (String) ctfParams.get("only2y"))));
String improvements = "off";
if ( req.getParams().get("improvements") != null ) {
improvements = req.getParams().get("improvements");
}
ctfDataHandler dh = new ctfDataHandler();
if (base.equals("matches")) {
ctfBase ctfbase = new ctfBase();
primary = ctfbase.initialScores(qstr,localParams,params,req,ctfParams);
period = ctfbase.getPeriod(primary);
secondary = ctfbase.clickSampler(primary,period,localParams,params,req);
if ( improvements.equals("on") && !only2y ) {
String done = ctfbase.getInitialPosns(qstr,localParams,params,req);
}
}
else {
ctfClickBase ctfbase = new ctfClickBase();
period = ctfbase.getPeriod(qstr,localParams,params,req,ctfParams);
primary = ctfbase.clickSampler(period,qstr,localParams,params,req);
secondary = ctfbase.findSecondary(primary,period,localParams,params,req);
if ( improvements.equals("on") && !only2y ) {
String done = ctfbase.getInitialPosns(qstr,localParams,params,req);
}
}
String primaryObjs = String.join(",",primary);
String secondaryObjs = String.join(",",secondary);
if ( secondary.size() > 0 ){
if ( !only2y ) {
qstr = "((" + qstr + ") ({!terms f="+dh.getDOCID()+"}" + secondaryObjs + "))";
}
else {
qstr = "(({!terms f="+dh.getDOCID()+"}-1) ({!terms f="+dh.getDOCID()+"}"+secondaryObjs+"))";
dh.primaryObjClear();
}
}
else {
qstr = "((" + qstr + ") ({!terms f="+dh.getDOCID()+"}-1))";
}
if ( restrict ) {
NamedList<Object> adjParams = req.getParams().toNamedList();
String rfq = "({!terms f="+dh.getDOCID()+"}"+primaryObjs+") ({!terms f="+dh.getDOCID()+"}"+secondaryObjs+")";
adjParams.add(CommonParams.FQ, rfq );
req.setParams(SolrParams.toSolrParams(adjParams));
}
CSstarttime = System.currentTimeMillis();
return new ctfParser(qstr, localParams, params, req);
}
private static class ctfParser extends QParser {
private Query innerQuery;
public ctfParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
super(qstr, localParams, params, req);
try {
QParser parser = getParser(qstr, "lucene", getReq());
this.innerQuery = parser.parse();
} catch (SyntaxError ex) {
throw new RuntimeException("error parsing query", ex);
}
ctfDataHandler dh = new ctfDataHandler();
dh.addCSQtime( System.currentTimeMillis() - CSstarttime );
}
public Query parse() throws SyntaxError {
return new ctfScorer(innerQuery);
}
}
}