-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQryIopWindow.java
More file actions
148 lines (117 loc) · 5.13 KB
/
Copy pathQryIopWindow.java
File metadata and controls
148 lines (117 loc) · 5.13 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.io.*;
import java.util.*;
/**
* Created by akshatgaur on 2/19/17.
*/
/**
* The Window operator for all retrieval models.
*/
public class QryIopWindow extends QryIop {
/**
* Evaluate the query operator; the result is an internal inverted
* list that may be accessed via the internal iterators.
* @throws IOException Error accessing the Lucene index.
*/
private int operatorDistance;
public QryIopWindow(int operatorDistance){
this.operatorDistance = operatorDistance;
}
protected void evaluate () throws IOException {
// Create an empty inverted list. If there are no query arguments,
// that's the final result.
this.invertedList = new InvList (this.getField());
if (args.size () == 0) {
return;
}
// Each pass of the loop adds 1 document to result inverted list
// until all of the argument inverted lists are depleted.
boolean documentLeft = true;
while ( documentLeft) {
//First get the doc in which all the arguments are present.
// Once the doc is available get the location of each argument and check if it satisfies the near operator.
boolean notAllInDoc = false;
Qry q_0 = this.args.get(0);
if(!((QryIop)q_0).docIteratorHasMatch(null)){
notAllInDoc = true;
break;
}
int maxDocid = q_0.docIteratorGetMatch();
for (int i = 1; i < this.args.size(); i++) {
Qry q_i = args.get(i);
if (q_i.docIteratorHasMatch(null)) {
int q_iDocid = q_i.docIteratorGetMatch();
if (maxDocid != q_iDocid) {
notAllInDoc = true;
if (maxDocid < q_iDocid) {
maxDocid = q_iDocid;
}
}
} else{
notAllInDoc = true;
break;
}
}
//If all arguments are not in the doc then advance ahead of the max value of docID of that argument we have
// as there will be no matching doc ID before the that doc. Continue looking for all docs in next iteration.
if (notAllInDoc) {
for (Qry q_i : this.args) {
if (q_i.docIteratorHasMatch(null)){
q_i.docIteratorAdvanceTo(maxDocid);
}else{
documentLeft = false;
}
}
continue;
}
// Create a new posting by storing the position that satisfies near operator constraint for the arguments.
List<Integer> position = new ArrayList<Integer>();
while (true) {
boolean locRemaining = true;
// get the location of curr argument and next argument and check if it satisfies
//if it does not advance the argument with smaller loc to next location and start iterating back from the beginning.
int loc = 0;
int min_loc = ((QryIop) this.args.get(0)).locIteratorGetMatch();
int max_loc = min_loc;
int min_idx = 0;
for (int i = 0; i < this.args.size(); i++) {
Qry q = this.args.get(i);
if (min_loc > ((QryIop)q).locIteratorGetMatch() ){
min_loc = ((QryIop)q).locIteratorGetMatch();
min_idx = i;
}else if (max_loc < ((QryIop)q).locIteratorGetMatch() ) {
max_loc = ((QryIop) q).locIteratorGetMatch();
}
}
if(1 + max_loc - min_loc <= operatorDistance){
position.add(max_loc);
for (Qry query : this.args){
((QryIop) query).locIteratorAdvance();
if(!((QryIop) query).locIteratorHasMatch()){
locRemaining = false;
break;
}
}
}else{
QryIop q_min = (QryIop)this.args.get(min_idx);
(q_min).locIteratorAdvance();
if(!q_min.locIteratorHasMatch()){
locRemaining = false;
}
}
//if there is no location remaining/matching then break and get the next document
if (!locRemaining) {
break;
}
//if there is a set of location satisfying near operator then add to position arraylist
}
// if there is are entries in position then append that to the inverted list.
if (position.size() > 0){
this.invertedList.appendPosting(maxDocid, position);
}
//once a document is checked advance all the arguments past that document.
for (Qry queries : this.args) {
queries.docIteratorAdvancePast(maxDocid);
}
}
}
}