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
Resolves #1691: Lucene negative queries #1694
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
104 changes: 104 additions & 0 deletions
104
...ecord-layer-lucene/src/main/java/com/apple/foundationdb/record/lucene/LuceneNotQuery.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,104 @@ | ||
/* | ||
* LuceneNotQuery.java | ||
* | ||
* This source file is part of the FoundationDB open source project | ||
* | ||
* Copyright 2022 Apple Inc. and the FoundationDB project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.apple.foundationdb.record.lucene; | ||
|
||
import com.apple.foundationdb.annotation.API; | ||
import com.apple.foundationdb.record.EvaluationContext; | ||
import com.apple.foundationdb.record.PlanHashable; | ||
import com.apple.foundationdb.record.metadata.Index; | ||
import com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreBase; | ||
import com.apple.foundationdb.record.query.plan.cascades.explain.Attribute; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import org.apache.lucene.search.BooleanClause; | ||
import org.apache.lucene.search.BooleanQuery; | ||
import org.apache.lucene.search.MatchAllDocsQuery; | ||
import org.apache.lucene.search.Query; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Binder for a negation of clauses. | ||
* Because of the way Lucene {@link BooleanQuery} works, this actually represents set subtraction, | ||
* with a set of positive and negative clauses. For the same reason, there is no disjunctive analogue. | ||
*/ | ||
@API(API.Status.UNSTABLE) | ||
public class LuceneNotQuery extends LuceneBooleanQuery { | ||
@Nonnull | ||
private final List<LuceneQueryClause> negatedChildren; | ||
|
||
public LuceneNotQuery(@Nonnull List<LuceneQueryClause> children, @Nonnull List<LuceneQueryClause> negatedChildren) { | ||
super(children, BooleanClause.Occur.MUST); | ||
this.negatedChildren = negatedChildren; | ||
} | ||
|
||
public LuceneNotQuery(@Nonnull LuceneQueryClause negatedChild) { | ||
this(Collections.emptyList(), Collections.singletonList(negatedChild)); | ||
} | ||
|
||
@Nonnull | ||
protected List<LuceneQueryClause> getNegatedChildren() { | ||
return negatedChildren; | ||
} | ||
|
||
@Override | ||
public Query bind(@Nonnull FDBRecordStoreBase<?> store, @Nonnull Index index, @Nonnull EvaluationContext context) { | ||
BooleanQuery.Builder builder = new BooleanQuery.Builder(); | ||
if (getChildren().isEmpty()) { | ||
// Lucene cannot handle all negated clauses. | ||
builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST); | ||
} else { | ||
for (LuceneQueryClause child : getChildren()) { | ||
builder.add(child.bind(store, index, context), BooleanClause.Occur.MUST); | ||
} | ||
} | ||
for (LuceneQueryClause child : negatedChildren) { | ||
builder.add(child.bind(store, index, context), BooleanClause.Occur.MUST_NOT); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
@Override | ||
public void getPlannerGraphDetails(@Nonnull ImmutableList.Builder<String> detailsBuilder, @Nonnull ImmutableMap.Builder<String, Attribute> attributeMapBuilder) { | ||
super.getPlannerGraphDetails(detailsBuilder, attributeMapBuilder); | ||
for (LuceneQueryClause child : negatedChildren) { | ||
child.getPlannerGraphDetails(detailsBuilder, attributeMapBuilder); | ||
} | ||
} | ||
|
||
@Override | ||
public int planHash(@Nonnull final PlanHashKind hashKind) { | ||
return super.planHash() - PlanHashable.iterablePlanHash(hashKind, negatedChildren); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Stream.concat( | ||
getChildren().stream().map(Objects::toString), | ||
negatedChildren.stream().map(c -> "NOT " + c) | ||
).collect(Collectors.joining(" AND ")); | ||
} | ||
} |
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
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.
If someone did something like
lucene_clause_a OR NOT lucene_clause_b
, could this transformation lead to this being translated into something like(lucene_clause_a) -(lucene_clause_b)
(effectively turning theOR
to anAND
)?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.
I do not think so.
LuceneNotQuery
always has occur =MUST
, so it is not possible fornegatedChildren
to be added to in theSHOULD
case. Note how flattening only happens when occur matches.