Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-2208 Use Query objects instead of Filters in spatial search
- Loading branch information
Showing
10 changed files
with
546 additions
and
341 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
engine/src/main/java/org/hibernate/search/spatial/impl/ConstantScoreScorer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Hibernate Search, full-text search for your domain model | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.search.spatial.impl; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.lucene.search.DocIdSetIterator; | ||
import org.apache.lucene.search.Scorer; | ||
import org.apache.lucene.search.TwoPhaseIterator; | ||
import org.apache.lucene.search.Weight; | ||
|
||
|
||
/** | ||
* A copy of Lucene's ConstantScoreWeight implementation, | ||
* necessary because the one in Lucene is marked as "internal". | ||
* | ||
* @author Yoann Rodiere | ||
*/ | ||
final class ConstantScoreScorer extends Scorer { | ||
|
||
private final float score; | ||
private final TwoPhaseIterator twoPhaseIterator; | ||
private final DocIdSetIterator disi; | ||
|
||
/** | ||
* Constructor based on a {@link DocIdSetIterator} which will be used to drive iteration. Two phase iteration will | ||
* not be supported. | ||
* | ||
* @param weight the parent weight | ||
* @param score the score to return on each document | ||
* @param disi the iterator that defines matching documents | ||
*/ | ||
public ConstantScoreScorer(Weight weight, float score, DocIdSetIterator disi) { | ||
super( weight ); | ||
this.score = score; | ||
this.twoPhaseIterator = null; | ||
this.disi = disi; | ||
} | ||
|
||
/** | ||
* Constructor based on a {@link TwoPhaseIterator}. In that case the {@link Scorer} will support two-phase | ||
* iteration. | ||
* | ||
* @param weight the parent weight | ||
* @param score the score to return on each document | ||
* @param twoPhaseIterator the iterator that defines matching documents | ||
*/ | ||
public ConstantScoreScorer(Weight weight, float score, TwoPhaseIterator twoPhaseIterator) { | ||
super( weight ); | ||
this.score = score; | ||
this.twoPhaseIterator = twoPhaseIterator; | ||
this.disi = TwoPhaseIterator.asDocIdSetIterator( twoPhaseIterator ); | ||
} | ||
|
||
@Override | ||
public DocIdSetIterator iterator() { | ||
return disi; | ||
} | ||
|
||
@Override | ||
public TwoPhaseIterator twoPhaseIterator() { | ||
return twoPhaseIterator; | ||
} | ||
|
||
@Override | ||
public int docID() { | ||
return disi.docID(); | ||
} | ||
|
||
@Override | ||
public float score() throws IOException { | ||
return score; | ||
} | ||
|
||
@Override | ||
public int freq() throws IOException { | ||
return 1; | ||
} | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
engine/src/main/java/org/hibernate/search/spatial/impl/ConstantScoreWeight.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Hibernate Search, full-text search for your domain model | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.search.spatial.impl; | ||
|
||
import java.io.IOException; | ||
import java.util.Set; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.index.Term; | ||
import org.apache.lucene.search.Explanation; | ||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.search.Scorer; | ||
import org.apache.lucene.search.TwoPhaseIterator; | ||
import org.apache.lucene.search.Weight; | ||
|
||
|
||
/** | ||
* A copy of Lucene's ConstantScoreWeight implementation, | ||
* necessary because the one in Lucene is marked as "internal". | ||
* | ||
* @author Yoann Rodiere | ||
*/ | ||
abstract class ConstantScoreWeight extends Weight { | ||
|
||
private float boost; | ||
private float queryNorm; | ||
private float queryWeight; | ||
|
||
protected ConstantScoreWeight(Query query) { | ||
super( query ); | ||
normalize( 1f, 1f ); | ||
} | ||
|
||
@Override | ||
public void extractTerms(Set<Term> terms) { | ||
// most constant-score queries don't wrap index terms | ||
// eg. geo filters, doc values queries, ... | ||
// override if your constant-score query does wrap terms | ||
} | ||
|
||
@Override | ||
public final float getValueForNormalization() throws IOException { | ||
return queryWeight * queryWeight; | ||
} | ||
|
||
@Override | ||
public void normalize(float norm, float boost) { | ||
this.boost = boost; | ||
queryNorm = norm; | ||
queryWeight = queryNorm * boost; | ||
} | ||
|
||
protected final float score() { | ||
return queryWeight; | ||
} | ||
|
||
@Override | ||
public Explanation explain(LeafReaderContext context, int doc) throws IOException { | ||
final Scorer s = scorer( context ); | ||
final boolean exists; | ||
if ( s == null ) { | ||
exists = false; | ||
} | ||
else { | ||
final TwoPhaseIterator twoPhase = s.twoPhaseIterator(); | ||
if ( twoPhase == null ) { | ||
exists = s.iterator().advance( doc ) == doc; | ||
} | ||
else { | ||
exists = twoPhase.approximation().advance( doc ) == doc && twoPhase.matches(); | ||
} | ||
} | ||
|
||
if ( exists ) { | ||
return Explanation.match( | ||
queryWeight, getQuery().toString() + ", product of:", | ||
Explanation.match( boost, "boost" ), Explanation.match( queryNorm, "queryNorm" ) ); | ||
} | ||
else { | ||
return Explanation.noMatch( getQuery().toString() + " doesn't match id " + doc ); | ||
} | ||
} | ||
} |
Oops, something went wrong.