-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-3200 Add minimum should match to match predicate
- Loading branch information
1 parent
ebec41f
commit 2b27c27
Showing
23 changed files
with
446 additions
and
308 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
72 changes: 0 additions & 72 deletions
72
.../elasticsearch/search/predicate/impl/ElasticsearchCommonMinimumShouldMatchConstraint.java
This file was deleted.
Oops, something went wrong.
115 changes: 115 additions & 0 deletions
115
...elasticsearch/search/predicate/impl/ElasticsearchCommonMinimumShouldMatchConstraints.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,115 @@ | ||
/* | ||
* 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.backend.elasticsearch.search.predicate.impl; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
import org.hibernate.search.backend.elasticsearch.logging.impl.Log; | ||
import org.hibernate.search.engine.search.predicate.spi.MinimumShouldMatchBuilder; | ||
import org.hibernate.search.util.common.logging.impl.LoggerFactory; | ||
|
||
public final class ElasticsearchCommonMinimumShouldMatchConstraints implements MinimumShouldMatchBuilder { | ||
private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() ); | ||
|
||
private Map<Integer, CommonMinimumShouldMatchConstraint> minimumShouldMatchConstraints; | ||
|
||
@Override | ||
public void minimumShouldMatchNumber(int ignoreConstraintCeiling, int matchingClausesNumber) { | ||
addMinimumShouldMatchConstraint( | ||
ignoreConstraintCeiling, | ||
new CommonMinimumShouldMatchConstraint( matchingClausesNumber, null ) | ||
); | ||
} | ||
|
||
@Override | ||
public void minimumShouldMatchPercent(int ignoreConstraintCeiling, int matchingClausesPercent) { | ||
addMinimumShouldMatchConstraint( | ||
ignoreConstraintCeiling, | ||
new CommonMinimumShouldMatchConstraint( null, matchingClausesPercent ) | ||
); | ||
} | ||
|
||
private void addMinimumShouldMatchConstraint(int ignoreConstraintCeiling, | ||
CommonMinimumShouldMatchConstraint constraint) { | ||
if ( minimumShouldMatchConstraints == null ) { | ||
// We'll need to go through the data in ascending order, so use a TreeMap | ||
minimumShouldMatchConstraints = new TreeMap<>(); | ||
} | ||
Object previous = minimumShouldMatchConstraints.put( ignoreConstraintCeiling, constraint ); | ||
if ( previous != null ) { | ||
throw log.minimumShouldMatchConflictingConstraints( ignoreConstraintCeiling ); | ||
} | ||
} | ||
|
||
public String formatMinimumShouldMatchConstraints() { | ||
StringBuilder builder = new StringBuilder(); | ||
Iterator<Map.Entry<Integer, CommonMinimumShouldMatchConstraint>> iterator = | ||
minimumShouldMatchConstraints.entrySet().iterator(); | ||
|
||
// Process the first constraint differently | ||
Map.Entry<Integer, CommonMinimumShouldMatchConstraint> entry = iterator.next(); | ||
Integer ignoreConstraintCeiling = entry.getKey(); | ||
CommonMinimumShouldMatchConstraint constraint = entry.getValue(); | ||
if ( ignoreConstraintCeiling.equals( 0 ) && minimumShouldMatchConstraints.size() == 1 ) { | ||
// Special case: if there's only one constraint and its ignore ceiling is 0, do not mention the ceiling | ||
constraint.appendTo( builder, null ); | ||
return builder.toString(); | ||
} | ||
else { | ||
entry.getValue().appendTo( builder, ignoreConstraintCeiling ); | ||
} | ||
|
||
// Process the other constraints normally | ||
while ( iterator.hasNext() ) { | ||
entry = iterator.next(); | ||
ignoreConstraintCeiling = entry.getKey(); | ||
constraint = entry.getValue(); | ||
builder.append( ' ' ); | ||
constraint.appendTo( builder, ignoreConstraintCeiling ); | ||
} | ||
|
||
return builder.toString(); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return minimumShouldMatchConstraints == null; | ||
} | ||
|
||
private static final class CommonMinimumShouldMatchConstraint { | ||
private final Integer matchingClausesNumber; | ||
private final Integer matchingClausesPercent; | ||
|
||
CommonMinimumShouldMatchConstraint(Integer matchingClausesNumber, Integer matchingClausesPercent) { | ||
this.matchingClausesNumber = matchingClausesNumber; | ||
this.matchingClausesPercent = matchingClausesPercent; | ||
} | ||
|
||
/** | ||
* Format the constraint according to | ||
* <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html"> | ||
* the format specified in the Elasticsearch documentation | ||
* </a>. | ||
* | ||
* @param builder The builder to append the formatted value to. | ||
* @param ignoreConstraintCeiling The ceiling above which this constraint is no longer ignored. | ||
*/ | ||
void appendTo(StringBuilder builder, Integer ignoreConstraintCeiling) { | ||
if ( ignoreConstraintCeiling != null ) { | ||
builder.append( ignoreConstraintCeiling ).append( '<' ); | ||
} | ||
if ( matchingClausesNumber != null ) { | ||
builder.append( matchingClausesNumber ); | ||
} | ||
else { | ||
builder.append( matchingClausesPercent ).append( '%' ); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.