Skip to content

Commit b083c62

Browse files
committed
HSEARCH-5010 Use a custom priority queue in text facet counts
1 parent 708b292 commit b083c62

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

backend/lucene/src/main/java/org/hibernate/search/backend/lucene/lowlevel/facet/impl/TextMultiValueFacetCounts.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.lucene.search.DocIdSetIterator;
3030
import org.apache.lucene.util.BytesRef;
3131
import org.apache.lucene.util.LongValues;
32+
import org.apache.lucene.util.PriorityQueue;
3233

3334
/**
3435
* Copied with some changes from {@code org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts}
@@ -84,33 +85,33 @@ private FacetResult getTopChildrenSortByCount(int topN) throws IOException {
8485
topN = ordCount;
8586
}
8687

87-
TopOrdAndIntQueue q = null;
88+
HibernateSearchTopOrdAndIntQueue q = null;
8889

8990
int bottomCount = 0;
9091

9192
int totCount = 0;
9293
int childCount = 0;
9394

94-
TopOrdAndIntQueue.OrdAndValue reuse = null;
95+
TopOrdAndIntQueue.OrdAndInt reuse = null;
9596

9697
for ( int ord = 0; ord < ordCount; ord++ ) {
9798
if ( counts[ord] > 0 ) {
9899
totCount += counts[ord];
99100
childCount++;
100101
if ( counts[ord] > bottomCount ) {
101102
if ( reuse == null ) {
102-
reuse = new TopOrdAndIntQueue.OrdAndValue();
103+
reuse = new TopOrdAndIntQueue.OrdAndInt();
103104
}
104105
reuse.ord = ord;
105106
reuse.value = counts[ord];
106107
if ( q == null ) {
107108
// Lazy init, so we don't create this for the
108109
// sparse case unnecessarily
109-
q = new TopOrdAndIntQueue( topN );
110+
q = new HibernateSearchTopOrdAndIntQueue( topN );
110111
}
111112
reuse = q.insertWithOverflow( reuse );
112113
if ( q.size() == topN ) {
113-
bottomCount = q.top().value;
114+
bottomCount = ( q.top() ).value;
114115
}
115116
}
116117
}
@@ -122,7 +123,7 @@ private FacetResult getTopChildrenSortByCount(int topN) throws IOException {
122123

123124
LabelAndValue[] labelValues = new LabelAndValue[q.size()];
124125
for ( int i = labelValues.length - 1; i >= 0; i-- ) {
125-
TopOrdAndIntQueue.OrdAndValue ordAndValue = q.pop();
126+
TopOrdAndIntQueue.OrdAndInt ordAndValue = q.pop();
126127
final BytesRef term = dv.lookupOrd( ordAndValue.ord );
127128
labelValues[i] = new LabelAndValue( term.utf8ToString(), ordAndValue.value );
128129
}
@@ -255,4 +256,20 @@ public List<FacetResult> getAllDims(int topN) throws IOException {
255256
return Collections.singletonList( getTopChildren( topN, field ) );
256257
}
257258

259+
/**
260+
* While there is a `TopOrdAndIntQueue` in Lucene, unfortunately it works with OrdAndValue objects (in API).
261+
* And there's no access to the value, leading to casting any type value has to be accessed. Hence, this impl:
262+
*/
263+
private static class HibernateSearchTopOrdAndIntQueue extends PriorityQueue<TopOrdAndIntQueue.OrdAndInt> {
264+
265+
public HibernateSearchTopOrdAndIntQueue(int maxSize) {
266+
super( maxSize );
267+
}
268+
269+
@Override
270+
protected boolean lessThan(TopOrdAndIntQueue.OrdAndInt a, TopOrdAndIntQueue.OrdAndInt b) {
271+
return a.lessThan( b );
272+
}
273+
}
274+
258275
}

backend/lucene/src/main/java/org/hibernate/search/backend/lucene/search/predicate/impl/LuceneCommonMinimumShouldMatchConstraints.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public Query apply(Query query) {
7373
}
7474
if ( query instanceof BooleanQuery ) {
7575
BooleanQuery booleanQuery = (BooleanQuery) query;
76-
int shouldClauses = (int) booleanQuery.clauses().stream().map( BooleanClause::getOccur )
76+
int shouldClauses = (int) booleanQuery.clauses().stream().map( BooleanClause::occur )
7777
.filter( BooleanClause.Occur.SHOULD::equals )
7878
.count();
7979
int minimumShouldMatch = minimumShouldMatch( shouldClauses );

backend/lucene/src/main/java/org/hibernate/search/backend/lucene/search/predicate/impl/LuceneCommonQueryStringPredicate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public void minimumShouldMatchPercent(int ignoreConstraintCeiling, int matchingC
158158
protected Query addMatchAllForBoolMustNotOnly(Query query) {
159159
if ( query instanceof BooleanQuery ) {
160160
BooleanQuery booleanQuery = (BooleanQuery) query;
161-
long notMustNot = booleanQuery.clauses().stream().map( BooleanClause::getOccur )
161+
long notMustNot = booleanQuery.clauses().stream().map( BooleanClause::occur )
162162
.filter( Predicate.not( BooleanClause.Occur.MUST_NOT::equals ) )
163163
.count();
164164

v5migrationhelper/engine/src/main/java/org/hibernate/search/query/dsl/impl/BooleanQueryBuilder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ private void replaceLastMustWith(Occur replacementOccur) {
5959
if ( lastClause == null ) {
6060
return;
6161
}
62-
if ( !lastClause.getOccur().equals( Occur.MUST ) ) {
63-
throw new AssertionFailure( "Cannot negate or disable scoring on class: " + lastClause.getOccur() );
62+
if ( !lastClause.occur().equals( Occur.MUST ) ) {
63+
throw new AssertionFailure( "Cannot negate or disable scoring on class: " + lastClause.occur() );
6464
}
6565
final int lastIndex = clauses.size() - 1;
66-
clauses.set( lastIndex, new BooleanClause( lastClause.getQuery(), replacementOccur ) );
66+
clauses.set( lastIndex, new BooleanClause( lastClause.query(), replacementOccur ) );
6767
}
6868

6969
@Override
@@ -123,8 +123,8 @@ private SearchPredicate createPredicate() {
123123
BooleanPredicateClausesStep<?> step = factory.bool();
124124
for ( BooleanClause clause : clauses ) {
125125
SearchPredicate predicate = factory.extension( LuceneExtension.get() )
126-
.fromLuceneQuery( clause.getQuery() ).toPredicate();
127-
switch ( clause.getOccur() ) {
126+
.fromLuceneQuery( clause.query() ).toPredicate();
127+
switch ( clause.occur() ) {
128128
case MUST:
129129
step = step.must( predicate );
130130
break;

0 commit comments

Comments
 (0)