-
-
Notifications
You must be signed in to change notification settings - Fork 246
HSEARCH-4489 Add matchNone() predicate #2964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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(); | ||
| } | ||
|
|
||
|
Comment on lines
+45
to
+51
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe just add another test with a bool predicate containing one predicate that does match a document, and also the |
||
| @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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I didn't include the original use case in the ticket; here it is: https://discourse.hibernate.org/t/fail-fast-predicate/6062?u=yrodiere
The best solution to avoid running a search query is simply to not call
.fetch()at all, but in some cases people will have methods dedicated to the creation of inner predicates, and will not be able to prevent the query to run; in that case, they can just create amatchNonepredicate and the end result will be the same (though it will unfortunately involve some pointless I/O).So, as you can see, it's a very specific use case, for advanced users. I'm not sure we need further explanation than what you did here, but if you think you can add something clear and concise, feel free to give it a shot :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that's an interesting case... Knew of
match_allusage within dynamic filters but not thematch_none. nice 😃As for the text, I gave it a try ... aaaand the text becomes too cumbersome 😄 but as you've said - it's for advanced users. So those who need it would know its meaning, and others are probably safer without using it 😃