Skip to content

Commit 471179e

Browse files
committed
code refactoring
1 parent ae3bfb6 commit 471179e

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,23 @@ public final Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, fl
133133
throws IOException {
134134

135135
if (this.equalValues) { // lowerPoint==upperPoint
136-
return new SinglePointConstantScoreWeight(this, scoreMode, boost);
136+
return new SinglePointRangeQueryWeight(this, scoreMode, boost);
137137
}
138138
// We don't use RandomAccessWeight here: it's no good to approximate with "match all docs".
139139
// This is an inverted structure and should be used in the first pass:
140-
return new MultiPointsConstantScoreWeight(this, scoreMode, boost);
140+
return new MultiPointRangeQueryWeight(this, scoreMode, boost);
141141
}
142142

143143
/**
144-
* Essentially, it is to reduce the number of comparisons. This is an optimization, used for the
145-
* case of lowerPoint==upperPoint.
144+
* Single-point range query weight implementation class, used to handle the special case where the
145+
* lower and upper bounds are equal (i.e. single-point query).
146+
*
147+
* <p>Optimize query performance by reducing the number of comparisons between dimensions. This
148+
* implementation is used when the upper and lower bounds of all dimensions are exactly the same.
146149
*/
147-
protected class SinglePointConstantScoreWeight extends MultiPointsConstantScoreWeight {
150+
protected class SinglePointRangeQueryWeight extends PointRangeQueryWeight {
148151

149-
public SinglePointConstantScoreWeight(Query query, ScoreMode scoreMode, float boost) {
152+
protected SinglePointRangeQueryWeight(Query query, ScoreMode scoreMode, float boost) {
150153
super(query, scoreMode, boost);
151154
}
152155

@@ -192,21 +195,20 @@ public Relation relate(byte[] minPackedValue, byte[] maxPackedValue) {
192195
}
193196

194197
/**
195-
* A weight that used for lowerPoint != upperPoint case, the query range may include multiple
196-
* points.
198+
* Multiple-point range query weight implementation class, used to handle the situation where the
199+
* query range contains multiple points.
200+
*
201+
* <p>When the lower bound (lowerPoint) of the query is not equal to the upper bound (upperPoint),
202+
* this implementation is used to check whether each dimension is within the query range.
197203
*/
198-
protected class MultiPointsConstantScoreWeight extends ConstantScoreWeight {
204+
protected class MultiPointRangeQueryWeight extends PointRangeQueryWeight {
199205

200-
protected ScoreMode scoreMode;
201-
protected ByteArrayComparator comparator;
202-
203-
public MultiPointsConstantScoreWeight(Query query, ScoreMode scoreMode, float boost) {
204-
super(query, boost);
205-
this.scoreMode = scoreMode;
206-
this.comparator = ArrayUtil.getUnsignedComparator(bytesPerDim);
206+
protected MultiPointRangeQueryWeight(Query query, ScoreMode scoreMode, float boost) {
207+
super(query, scoreMode, boost);
207208
}
208209

209-
public boolean matches(byte[] packedValue) {
210+
@Override
211+
protected boolean matches(byte[] packedValue) {
210212
int offset = 0;
211213
for (int dim = 0; dim < numDims; dim++, offset += bytesPerDim) {
212214
if (comparator.compare(packedValue, offset, lowerPoint, offset) < 0) {
@@ -221,7 +223,8 @@ public boolean matches(byte[] packedValue) {
221223
return true;
222224
}
223225

224-
public Relation relate(byte[] minPackedValue, byte[] maxPackedValue) {
226+
@Override
227+
protected Relation relate(byte[] minPackedValue, byte[] maxPackedValue) {
225228

226229
boolean crosses = false;
227230
int offset = 0;
@@ -244,6 +247,31 @@ public Relation relate(byte[] minPackedValue, byte[] maxPackedValue) {
244247
return Relation.CELL_INSIDE_QUERY;
245248
}
246249
}
250+
}
251+
252+
/**
253+
* Basic weight class, inherited from {@link ConstantScoreWeight}, subclasses need to implement
254+
* specific point value matching logic and range relationship judgment.
255+
*
256+
* @see SinglePointRangeQueryWeight for the specific implementation of single-point range query.
257+
* @see MultiPointRangeQueryWeight for the specific implementation of multi-point range query.
258+
*/
259+
protected abstract class PointRangeQueryWeight extends ConstantScoreWeight {
260+
261+
protected ScoreMode scoreMode;
262+
protected ByteArrayComparator comparator;
263+
264+
protected PointRangeQueryWeight(Query query, ScoreMode scoreMode, float boost) {
265+
super(query, boost);
266+
this.scoreMode = scoreMode;
267+
this.comparator = ArrayUtil.getUnsignedComparator(bytesPerDim);
268+
}
269+
270+
/** whether the point value matches the query range. */
271+
protected abstract boolean matches(byte[] packedValue);
272+
273+
/** relation between the point value range and the query range. */
274+
protected abstract Relation relate(byte[] minPackedValue, byte[] maxPackedValue);
247275

248276
private IntersectVisitor getIntersectVisitor(DocIdSetBuilder result) {
249277
return new IntersectVisitor() {

lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,12 +2567,12 @@ public void testPointRangeQueryWithEqualValues() throws Exception {
25672567
PointRangeQuery query = (PointRangeQuery) IntPoint.newRangeQuery("int", 0, 1);
25682568
assertFalse(query.isEqualValues());
25692569
Weight weight = query.createWeight(s, ScoreMode.COMPLETE_NO_SCORES, 1f);
2570-
assertTrue(weight instanceof PointRangeQuery.MultiPointsConstantScoreWeight);
2570+
assertTrue(weight instanceof PointRangeQuery.MultiPointRangeQueryWeight);
25712571

25722572
query = (PointRangeQuery) IntPoint.newRangeQuery("int", 0, 0);
25732573
assertTrue(query.isEqualValues());
25742574
weight = query.createWeight(s, ScoreMode.COMPLETE_NO_SCORES, 1f);
2575-
assertTrue(weight instanceof PointRangeQuery.SinglePointConstantScoreWeight);
2575+
assertTrue(weight instanceof PointRangeQuery.SinglePointRangeQueryWeight);
25762576

25772577
assertEquals(zeroCount, s.count(IntPoint.newRangeQuery("int", 0, 0)));
25782578
assertEquals(oneCount, s.count(IntPoint.newRangeQuery("int", 1, 1)));

0 commit comments

Comments
 (0)