-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate out NeuralKNNQuery and use builder pattern for NeuralKNNQuer…
…yBuilder Signed-off-by: Junqiu Lei <junqiu@amazon.com>
- Loading branch information
1 parent
6a1f045
commit defb108
Showing
7 changed files
with
228 additions
and
185 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/java/org/opensearch/neuralsearch/query/NeuralKNNQuery.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,68 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.neuralsearch.query; | ||
|
||
import lombok.Getter; | ||
import org.apache.lucene.index.IndexReader; | ||
import org.apache.lucene.search.IndexSearcher; | ||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.search.QueryVisitor; | ||
import org.apache.lucene.search.ScoreMode; | ||
import org.apache.lucene.search.Weight; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Wraps KNN Lucene query to support neural search extensions. | ||
* Delegates core operations to the underlying KNN query. | ||
*/ | ||
@Getter | ||
public class NeuralKNNQuery extends Query { | ||
private final Query knnQuery; | ||
|
||
public NeuralKNNQuery(Query knnQuery) { | ||
this.knnQuery = knnQuery; | ||
} | ||
|
||
@Override | ||
public String toString(String field) { | ||
return knnQuery.toString(field); | ||
} | ||
|
||
@Override | ||
public void visit(QueryVisitor visitor) { | ||
// Delegate the visitor to the underlying KNN query | ||
knnQuery.visit(visitor); | ||
} | ||
|
||
@Override | ||
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException { | ||
// Delegate weight creation to the underlying KNN query | ||
return knnQuery.createWeight(searcher, scoreMode, boost); | ||
} | ||
|
||
@Override | ||
public Query rewrite(IndexReader reader) throws IOException { | ||
Query rewritten = knnQuery.rewrite(reader); | ||
if (rewritten == knnQuery) { | ||
return this; | ||
} | ||
return new NeuralKNNQuery(rewritten); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (this == other) return true; | ||
if (other == null || getClass() != other.getClass()) return false; | ||
NeuralKNNQuery that = (NeuralKNNQuery) other; | ||
return Objects.equals(knnQuery, that.knnQuery); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(knnQuery); | ||
} | ||
} |
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
Oops, something went wrong.