Class KnnVectorQuery

java.lang.Object
org.apache.lucene.search.Query
org.apache.lucene.search.KnnVectorQuery

public class KnnVectorQuery extends Query
Uses KnnVectorsReader.search(java.lang.String, float[], int, org.apache.lucene.util.Bits, int) to perform nearest neighbour search.

This query also allows for performing a kNN search subject to a filter. In this case, it first executes the filter for each leaf, then chooses a strategy dynamically:

  • If the filter cost is less than k, just execute an exact search
  • Otherwise run a kNN search subject to the filter
  • If the kNN search visits too many vectors without completing, stop and run an exact search
  • Field Details

    • NO_RESULTS

      private static final TopDocs NO_RESULTS
    • field

      private final String field
    • target

      private final float[] target
    • k

      private final int k
    • filter

      private final Query filter
  • Constructor Details

    • KnnVectorQuery

      public KnnVectorQuery(String field, float[] target, int k)
      Find the k nearest documents to the target vector according to the vectors in the given field. target vector.
      Parameters:
      field - a field that has been indexed as a KnnVectorField.
      target - the target of the search
      k - the number of documents to find
      Throws:
      IllegalArgumentException - if k is less than 1
    • KnnVectorQuery

      public KnnVectorQuery(String field, float[] target, int k, Query filter)
      Find the k nearest documents to the target vector according to the vectors in the given field. target vector.
      Parameters:
      field - a field that has been indexed as a KnnVectorField.
      target - the target of the search
      k - the number of documents to find
      filter - a filter applied before the vector search
      Throws:
      IllegalArgumentException - if k is less than 1
  • Method Details