From 7203ae34ba6be40e551fdaa38b1e0da30d5ef2f0 Mon Sep 17 00:00:00 2001 From: Julie Tibshirani Date: Fri, 15 Apr 2022 15:39:54 -0700 Subject: [PATCH] Remove custom KnnVectorFieldExistsQuery This query was added to Lucene in version 9.1, so we can delete our version in favor of using the Lucene one. --- .../mapper/DenseVectorFieldMapper.java | 2 +- .../query/KnnVectorFieldExistsQuery.java | 77 ------------------- .../mapper/DenseVectorFieldMapperTests.java | 2 +- 3 files changed, 2 insertions(+), 79 deletions(-) delete mode 100644 x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/KnnVectorFieldExistsQuery.java diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapper.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapper.java index 05974edf26b7c..0bf3eec74027f 100644 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapper.java +++ b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapper.java @@ -14,6 +14,7 @@ import org.apache.lucene.document.KnnVectorField; import org.apache.lucene.index.VectorSimilarityFunction; import org.apache.lucene.search.DocValuesFieldExistsQuery; +import org.apache.lucene.search.KnnVectorFieldExistsQuery; import org.apache.lucene.search.KnnVectorQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.BytesRef; @@ -39,7 +40,6 @@ import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser.Token; -import org.elasticsearch.xpack.vectors.query.KnnVectorFieldExistsQuery; import org.elasticsearch.xpack.vectors.query.VectorIndexFieldData; import java.io.IOException; diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/KnnVectorFieldExistsQuery.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/KnnVectorFieldExistsQuery.java deleted file mode 100644 index 3b273bf106ae9..0000000000000 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/KnnVectorFieldExistsQuery.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -package org.elasticsearch.xpack.vectors.query; - -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.search.ConstantScoreScorer; -import org.apache.lucene.search.ConstantScoreWeight; -import org.apache.lucene.search.DocIdSetIterator; -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.Scorer; -import org.apache.lucene.search.Weight; - -import java.io.IOException; -import java.util.Objects; - -public class KnnVectorFieldExistsQuery extends Query { - - private final String field; - - /** Create a query that will match documents which have a value for the given {@code field}. */ - public KnnVectorFieldExistsQuery(String field) { - this.field = Objects.requireNonNull(field); - } - - public String getField() { - return field; - } - - @Override - public boolean equals(Object other) { - return sameClassAs(other) && field.equals(((KnnVectorFieldExistsQuery) other).field); - } - - @Override - public int hashCode() { - return 31 * classHash() + field.hashCode(); - } - - @Override - public String toString(String field) { - return "KnnVectorFieldExistsQuery [field=" + this.field + "]"; - } - - @Override - public void visit(QueryVisitor visitor) { - if (visitor.acceptField(field)) { - visitor.visitLeaf(this); - } - } - - @Override - public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) { - return new ConstantScoreWeight(this, boost) { - @Override - public Scorer scorer(LeafReaderContext context) throws IOException { - DocIdSetIterator iterator = context.reader().getVectorValues(field); - if (iterator == null) { - return null; - } - return new ConstantScoreScorer(this, score(), scoreMode, iterator); - } - - @Override - public boolean isCacheable(LeafReaderContext ctx) { - return true; - } - }; - } -} diff --git a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapperTests.java b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapperTests.java index 66975a15e28ff..3f5819c4c080c 100644 --- a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapperTests.java +++ b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapperTests.java @@ -16,6 +16,7 @@ import org.apache.lucene.document.KnnVectorField; import org.apache.lucene.index.IndexableField; import org.apache.lucene.search.DocValuesFieldExistsQuery; +import org.apache.lucene.search.KnnVectorFieldExistsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.BytesRef; import org.elasticsearch.Version; @@ -33,7 +34,6 @@ import org.elasticsearch.xpack.vectors.DenseVectorPlugin; import org.elasticsearch.xpack.vectors.mapper.DenseVectorFieldMapper.DenseVectorFieldType; import org.elasticsearch.xpack.vectors.mapper.DenseVectorFieldMapper.VectorSimilarity; -import org.elasticsearch.xpack.vectors.query.KnnVectorFieldExistsQuery; import java.io.IOException; import java.nio.ByteBuffer;