Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-4489 Add matchNone() predicate
- add matchNone() to DSL - update documentation to mention new predicate
- Loading branch information
1 parent
4794169
commit edbff0b
Showing
15 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
...e/search/backend/elasticsearch/search/predicate/impl/ElasticsearchMatchNonePredicate.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,49 @@ | ||
/* | ||
* 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 org.hibernate.search.backend.elasticsearch.gson.impl.JsonAccessor; | ||
import org.hibernate.search.backend.elasticsearch.gson.impl.JsonObjectAccessor; | ||
import org.hibernate.search.backend.elasticsearch.search.common.impl.ElasticsearchSearchIndexScope; | ||
import org.hibernate.search.engine.search.predicate.SearchPredicate; | ||
import org.hibernate.search.engine.search.predicate.spi.MatchNonePredicateBuilder; | ||
|
||
import com.google.gson.JsonObject; | ||
|
||
|
||
class ElasticsearchMatchNonePredicate extends AbstractElasticsearchPredicate { | ||
|
||
private static final JsonObjectAccessor MATCH_NONE_ACCESSOR = JsonAccessor.root().property( "match_none" ) | ||
.asObject(); | ||
|
||
private ElasticsearchMatchNonePredicate(Builder builder) { | ||
super( builder ); | ||
} | ||
|
||
@Override | ||
public void checkNestableWithin(String expectedParentNestedPath) { | ||
// Nothing to do | ||
} | ||
|
||
@Override | ||
protected JsonObject doToJsonQuery(PredicateRequestContext context, | ||
JsonObject outerObject, JsonObject innerObject) { | ||
MATCH_NONE_ACCESSOR.set( outerObject, innerObject ); | ||
return outerObject; | ||
} | ||
|
||
static class Builder extends AbstractBuilder implements MatchNonePredicateBuilder { | ||
Builder(ElasticsearchSearchIndexScope<?> scope) { | ||
super( scope ); | ||
} | ||
|
||
@Override | ||
public SearchPredicate build() { | ||
return new ElasticsearchMatchNonePredicate( this ); | ||
} | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
...a/org/hibernate/search/backend/lucene/search/predicate/impl/LuceneMatchNonePredicate.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,43 @@ | ||
/* | ||
* 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.lucene.search.predicate.impl; | ||
|
||
import org.hibernate.search.backend.lucene.search.common.impl.LuceneSearchIndexScope; | ||
import org.hibernate.search.engine.search.predicate.SearchPredicate; | ||
import org.hibernate.search.engine.search.predicate.spi.MatchNonePredicateBuilder; | ||
|
||
import org.apache.lucene.search.MatchNoDocsQuery; | ||
import org.apache.lucene.search.Query; | ||
|
||
|
||
class LuceneMatchNonePredicate extends AbstractLuceneSearchPredicate { | ||
|
||
private LuceneMatchNonePredicate(Builder builder) { | ||
super( builder ); | ||
} | ||
|
||
@Override | ||
public void checkNestableWithin(String expectedParentNestedPath) { | ||
// Nothing to do | ||
} | ||
|
||
@Override | ||
protected Query doToQuery(PredicateRequestContext context) { | ||
return new MatchNoDocsQuery(); | ||
} | ||
|
||
static class Builder extends AbstractBuilder implements MatchNonePredicateBuilder { | ||
Builder(LuceneSearchIndexScope<?> scope) { | ||
super( scope ); | ||
} | ||
|
||
@Override | ||
public SearchPredicate build() { | ||
return new LuceneMatchNonePredicate( this ); | ||
} | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
...in/java/org/hibernate/search/engine/search/predicate/dsl/MatchNonePredicateFinalStep.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,13 @@ | ||
/* | ||
* 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.engine.search.predicate.dsl; | ||
|
||
/** | ||
* The initial and final step in "match none" predicate definition. | ||
*/ | ||
public interface MatchNonePredicateFinalStep extends PredicateFinalStep { | ||
} |
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
30 changes: 30 additions & 0 deletions
30
...rg/hibernate/search/engine/search/predicate/dsl/impl/MatchNonePredicateFinalStepImpl.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,30 @@ | ||
/* | ||
* 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.engine.search.predicate.dsl.impl; | ||
|
||
import org.hibernate.search.engine.search.predicate.SearchPredicate; | ||
import org.hibernate.search.engine.search.predicate.dsl.MatchNonePredicateFinalStep; | ||
import org.hibernate.search.engine.search.predicate.dsl.spi.AbstractPredicateFinalStep; | ||
import org.hibernate.search.engine.search.predicate.dsl.spi.SearchPredicateDslContext; | ||
import org.hibernate.search.engine.search.predicate.spi.MatchNonePredicateBuilder; | ||
|
||
|
||
public final class MatchNonePredicateFinalStepImpl extends AbstractPredicateFinalStep | ||
implements MatchNonePredicateFinalStep { | ||
|
||
private final MatchNonePredicateBuilder matchNoneBuilder; | ||
|
||
public MatchNonePredicateFinalStepImpl(SearchPredicateDslContext<?> dslContext) { | ||
super( dslContext ); | ||
this.matchNoneBuilder = dslContext.scope().predicateBuilders().matchNone(); | ||
} | ||
|
||
@Override | ||
protected SearchPredicate build() { | ||
return matchNoneBuilder.build(); | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
...main/java/org/hibernate/search/engine/search/predicate/spi/MatchNonePredicateBuilder.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,11 @@ | ||
/* | ||
* 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.engine.search.predicate.spi; | ||
|
||
public interface MatchNonePredicateBuilder extends SearchPredicateBuilder { | ||
|
||
} |
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
87 changes: 87 additions & 0 deletions
87
...te/search/integrationtest/backend/tck/search/predicate/MatchNonePredicateSpecificsIT.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.integrationtest.backend.tck.search.predicate; | ||
|
||
import static org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery; | ||
|
||
import org.hibernate.search.engine.backend.document.IndexFieldReference; | ||
import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement; | ||
import org.hibernate.search.engine.backend.types.dsl.IndexFieldTypeFactory; | ||
import org.hibernate.search.engine.search.predicate.dsl.SearchPredicateFactory; | ||
import org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper; | ||
import org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
|
||
public class MatchNonePredicateSpecificsIT { | ||
|
||
private static final String DOCUMENT_1 = "1"; | ||
private static final String STRING_1 = "aaa"; | ||
|
||
private static final String DOCUMENT_2 = "2"; | ||
private static final String STRING_2 = "bbb"; | ||
|
||
private static final String DOCUMENT_3 = "3"; | ||
private static final String STRING_3 = "ccc"; | ||
|
||
@ClassRule | ||
public static final SearchSetupHelper setupHelper = new SearchSetupHelper(); | ||
|
||
private static final SimpleMappedIndex<IndexBinding> index = SimpleMappedIndex.of( IndexBinding::new ); | ||
|
||
@BeforeClass | ||
public static void setup() { | ||
setupHelper.start().withIndex( index ).setup(); | ||
|
||
initData(); | ||
} | ||
|
||
@Test | ||
public void matchNone() { | ||
assertThatQuery( index.query() | ||
.where( SearchPredicateFactory::matchNone ) ) | ||
.hasNoHits(); | ||
} | ||
|
||
@Test | ||
public void matchNoneWithinBoolPredicate() { | ||
//check that we will find something with a single match predicate | ||
assertThatQuery( index.query() | ||
.where( | ||
f -> f.bool() | ||
.must( f.match().field( "string" ).matching( STRING_1 ).toPredicate() ) | ||
) | ||
).hasTotalHitCount( 1 ); | ||
|
||
// make sure that matchNone will "override" the other matching predicate | ||
assertThatQuery( index.query() | ||
.where( | ||
f -> f.bool() | ||
.must( f.match().field( "string" ).matching( STRING_1 ).toPredicate() ) | ||
.must( f.matchNone() ) | ||
) | ||
).hasNoHits(); | ||
} | ||
|
||
private static void initData() { | ||
index.bulkIndexer() | ||
.add( DOCUMENT_1, document -> document.addValue( index.binding().string, STRING_1 ) ) | ||
.add( DOCUMENT_2, document -> document.addValue( index.binding().string, STRING_2 ) ) | ||
.add( DOCUMENT_3, document -> document.addValue( index.binding().string, STRING_3 ) ) | ||
.join(); | ||
} | ||
|
||
private static class IndexBinding { | ||
final IndexFieldReference<String> string; | ||
|
||
IndexBinding(IndexSchemaElement root) { | ||
string = root.field( "string", IndexFieldTypeFactory::asString ).toReference(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.