All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dtsx.astra.sdk.AstraDBCollection Maven / Gradle / Ivy

There is a newer version: 1.2.7
Show newest version
package com.dtsx.astra.sdk;

import io.stargate.sdk.core.domain.Page;
import io.stargate.sdk.data.CollectionClient;
import io.stargate.sdk.data.DocumentMutationResult;
import io.stargate.sdk.data.JsonDocumentMutationResult;
import io.stargate.sdk.data.domain.JsonDocument;
import io.stargate.sdk.data.domain.JsonDocumentResult;
import io.stargate.sdk.data.domain.JsonResultUpdate;
import io.stargate.sdk.data.domain.UpdateStatus;
import io.stargate.sdk.data.domain.odm.Document;
import io.stargate.sdk.data.domain.odm.DocumentResult;
import io.stargate.sdk.data.domain.odm.DocumentResultMapper;
import io.stargate.sdk.data.domain.query.DeleteQuery;
import io.stargate.sdk.data.domain.query.Filter;
import io.stargate.sdk.data.domain.query.SelectQuery;
import io.stargate.sdk.data.domain.query.SelectQueryBuilder;
import io.stargate.sdk.data.domain.query.UpdateQuery;
import lombok.Getter;
import lombok.NonNull;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;

/**
 * Operation On a Collection.
 */
@Getter
public class AstraDBCollection {

    /**
     * Stargate Client for a collection
     */
    CollectionClient collectionClient;

    /**
     * Default constructor.
     *
     * @param collectionClient
     *      collection description
     */
    AstraDBCollection(CollectionClient collectionClient) {
        this.collectionClient = collectionClient;
    }

    // --------------------------
    // ---   Insert One      ----
    // --------------------------

    /**
     * Insert with a Json String.
     *
     * @param json
     *      json Strings
     * @return
     *      mutation result with status and id
     */
    public final JsonDocumentMutationResult insertOne(String json) {
        return collectionClient.insertOne(json);
    }

    /**
     * Insert with a Json String and get return asynchronously.
     *
     * @param json
     *      json Strings
     * @return
     *      mutation result with status and id
     */
    public final CompletableFuture insertOneAsync(String json) {
        return collectionClient.insertOneAsync(json);
    }

    /**
     * Insert with a Json Document (schemaless)
     *
     * @param document
     *      current bean
     * @return
     *      mutation result with status and id
     */
    public final JsonDocumentMutationResult insertOne(JsonDocument document) {
        return collectionClient.insertOne(document);
    }

    /**
     * Insert with a Json Document (schemaless)
     *
     * @param document
     *      current bean
     * @return
     *      mutation result with status and id
     */
    public final CompletableFuture insertOneASync(JsonDocument document) {
        return collectionClient.insertOneAsync(document);
    }

    /**
     * Insert with a Json Document object (SchemaFull)
     *
     * @param document
     *      current bean
     * @param 
     *     payload of document
     * @return
     *      mutation result with status and id
     */
    public final  DocumentMutationResult insertOne(Document document) {
        return collectionClient.insertOne(document);
    }

    /**
     * Insert with a Json Document object (SchemaFull)
     *
     * @param document
     *      current bean
     * @param 
     *     payload of document
     * @return
     *      mutation result with status and id
     */
    public final  CompletableFuture> insertOneASync(Document document) {
        return collectionClient.insertOneASync(document);
    }

    // --------------------------
    // ---   Upsert One      ----
    // --------------------------

    /**
     * Insert with a Json String.
     *
     * @param json
     *      json Strings
     * @return
     *      mutation result with status and id
     */
    public final JsonDocumentMutationResult upsertOne(String json) {
        return collectionClient.upsertOne(json);
    }

    /**
     * Insert with a Json String and get return asynchronously.
     *
     * @param json
     *      json Strings
     * @return
     *      mutation result with status and id
     */
    public final CompletableFuture upsertOneASync(String json) {
        return collectionClient.upsertOneAsync(json);
    }

    /**
     * Upsert a document in the collection.
     *
     * @param document
     *      current document
     * @return
     *      document id
     */
    public final JsonDocumentMutationResult upsertOne(JsonDocument document) {
        return collectionClient.upsertOne(document);
    }

    /**
     * Upsert a document in the collection.
     *
     * @param document
     *      current document
     * @return
     *      document id
     */
    public final CompletableFuture upsertOneASync(JsonDocument document) {
        return CompletableFuture.supplyAsync(() -> collectionClient.upsertOne(document));
    }

    /**
     * Upsert a document in the collection.
     *
     * @param document
     *      current document
     * @param 
     *     payload of document
     * @return
     *      document id
     */
    public final  DocumentMutationResult upsertOne(Document document) {
        return collectionClient.upsertOne(document);
    }

    /**
     * Upsert a document in the collection.
     *
     * @param document
     *      current document
     * @param 
     *     payload of document
     * @return
     *      document id
     */
    public final  CompletableFuture> upsertOneASync(Document document) {
        return collectionClient.upsertOneASync(document);
    }

    // --------------------------
    // ---   Insert Many     ----
    // --------------------------

    /**
     * Insert multiple records in one call, up to 20, then use insertManyChunked.
     *
     * @param json
     *      json String
     * @return
     *      for each document its id and insertion status
     */
    public final List insertMany(String json) {
        return collectionClient.insertMany(json);
    }

    /**
     * Insert multiple records in one call, up to 20, then use insertManyChunked asynchronously.
     *
     * @param json
     *      json String
     * @return
     *      for each document its id and insertion status
     */
    public final CompletableFuture> insertManyASync(String json) {
        return collectionClient.insertManyASync(json);
    }

    /**
     * Insert multiple records in one call, up to 20, then use insertManyChunked.
     *
     * @param documents
     *      list of documents
     * @return
     *      for each document its id and insertion status
     */
    public final List insertManyJsonDocuments(List documents) {
        return collectionClient.insertManyJsonDocuments(documents);
    }

    /**
     * Insert multiple records in one call, up to 20, asynchronously then use insertManyChunked.
     *
     * @param documents
     *      list of documents
     * @return
     *      for each document its id and insertion status
     */
    public final CompletableFuture> insertManyJsonDocumentsASync(List documents) {
        return collectionClient.insertManyJsonDocumentsASync(documents);
    }

    /**
     * Low level insertion of multiple records
     *
     * @param documents
     *      list of documents
     * @param 
     *     payload of document
     * @return
     *      list of ids
     */
    public final  List> insertMany(List> documents) {
        return collectionClient.insertMany(documents);
    }

    /**
     * Low level insertion of multiple records
     *
     * @param documents
     *      list of documents
     * @param 
     *     payload of document
     * @return
     *      list of ids
     */
    public final  CompletableFuture>> insertManyASync(List> documents) {
        return collectionClient.insertManyASync(documents);
    }

    // -------------------------------
    // ---   Insert Many Chunked  ----
    // -------------------------------

    /**
     * Low level insertion of multiple records
     *
     * @param documents
     *      list of documents
     * @param chunkSize
     *      size of the block
     * @param concurrency
     *      number of blocks in parallel
     * @param 
     *     represent the pojo, payload of document
     * @return
     *      list of ids
     */
    public final  List> insertManyChunked(List> documents, int chunkSize, int concurrency) {
        return collectionClient.insertManyChunked(documents, chunkSize, concurrency);
    }

    /**
     * Low level insertion of multiple records
     *
     * @param documents
     *      list of documents
     * @param chunkSize
     *      size of the block
     * @param concurrency
     *      number of blocks in parallel
     * @param 
     *     represent the pojo, payload of document
     * @return
     *      list of ids
     */
    public final  CompletableFuture>> insertManyChunkedASync(List> documents, int chunkSize, int concurrency) {
        return CompletableFuture.supplyAsync(() -> insertManyChunked(documents, chunkSize, concurrency));
    }

    /**
     * Enforce Mapping with JsonDocument
     *
     * @param documents
     *      list of documents
     * @return
     *      json document list
     */
    public final List insertManyChunkedJsonDocuments(List documents, int chunkSize, int concurrency) {
        return collectionClient.insertManyJsonDocumentsChunked(documents, chunkSize, concurrency);
    }

    public final CompletableFuture> insertManyChunkedJsonDocumentsAsync(List documents, int chunkSize, int concurrency) {
        return CompletableFuture.supplyAsync(() -> insertManyChunkedJsonDocuments(documents, chunkSize, concurrency));
    }

    // ------------------------------
    // ---      Upsert Many      ----
    // ------------------------------

    /**
     * Upsert any items in the collection.
     *
     * @param json
     *      json String
     * @return
     *      list of statuses
     */
    public final List upsertMany(String json) {
        return collectionClient.upsertMany(json);
    }

    /**
     * Upsert any items in the collection.
     *
     * @param json
     *      json String
     * @return
     *      list of statuses
     */
    public final CompletableFuture> upsertManyASync(String json) {
        return collectionClient.upsertManyASync(json);
    }

    /**
     * Upsert any items in the collection.
     *
     * @param documents
     *      current collection list
     * @return
     *      list of statuses
     */
    public final List upsertManyJsonDocuments(List documents) {
        return collectionClient.upsertManyJsonDocuments(documents);
    }

    /**
     * Upsert any items in the collection.
     *
     * @param documents
     *      current collection list
     * @return
     *      list of statuses
     */
    public final CompletableFuture> upsertManyJsonDocumentsASync(List documents) {
        return collectionClient.upsertManyJsonDocumentsASync(documents);
    }

    /**
     * Upsert any items in the collection.
     * @param documents
     *      current collection list
     * @return
     *      list of statuses
     * @param 
     *     represent the pojo, payload of document
     */
    public final  List> upsertMany(List> documents) {
        return collectionClient.upsertMany(documents);
    }

    /**
     * Upsert any items in the collection asynchronously.
     *
     * @param documents
     *      current collection list
     * @return
     *      list of statuses
     * @param 
     *     represent the pojo, payload of document
     */
    public final  CompletableFuture>> upsertManyASync(List> documents) {
        return collectionClient.upsertManyASync(documents);
    }

    // --------------------------------
    // ---   Upsert Many  Chunked  ----
    // --------------------------------

    /**
     * Low level insertion of multiple records
     *
     * @param documents
     *      list of documents
     * @param chunkSize
     *      size of the block
     * @param concurrency
     *      concurrency
     * @param 
     *     represent the pojo, payload of document
     * @return
     *      list of ids
     */
    public final  List> upsertManyChunked(List> documents, int chunkSize, int concurrency) {
        return collectionClient.upsertManyChunked(documents, chunkSize, concurrency);
    }

    /**
     * Low level insertion of multiple records
     *
     * @param documents
     *      list of documents
     * @param chunkSize
     *      size of the block
     * @param concurrency
     *      concurrency
     * @param 
     *     represent the pojo, payload of document
     * @return
     *      list of ids
     */
    public final  CompletableFuture>> upsertManyChunkedASync(List> documents, int chunkSize, int concurrency) {
        return collectionClient.upsertManyChunkedASync(documents, chunkSize, concurrency);
    }

    /**
     * Low level insertion of multiple records
     *
     * @param documents
     *      list of documents
     * @param chunkSize
     *      size of the block
     * @param concurrency
     *      concurrency
     * @return
     *      list of ids
     */
    public final List upsertManyJsonDocumentsChunked(List documents, int chunkSize, int concurrency) {
        return collectionClient.upsertManyJsonDocumentsChunked(documents, chunkSize, concurrency);
    }

    /**
     * Low level insertion of multiple records
     *
     * @param documents
     *      list of documents
     * @param chunkSize
     *      size of the block
     * @param concurrency
     *      concurrency
     * @return
     *      list of ids
     */
    public final CompletableFuture> upsertManyJsonDocumentsChunkedAsync(List documents, int chunkSize, int concurrency) {
        return collectionClient.upsertManyJsonDocumentsChunkedASync(documents, chunkSize, concurrency);
    }

    // --------------------------
    // ---      Count        ----
    // --------------------------

    /**
     * Count Document request.
     *
     * @return
     *      number of document.
     */
    public Integer countDocuments() {
        return collectionClient.countDocuments();
    }

    /**
     * Count Document request.
     *
     * @param jsonFilter
     *      request to filter for count
     * @return
     *      number of document.
     */
    public Integer countDocuments(Filter jsonFilter) {
        return collectionClient.countDocuments(jsonFilter);
    }

    // --------------------------
    // ---     Find One      ----
    // --------------------------

    /**
     * Check existence of a document from its id.
     * Projection to make it as light as possible.
     *
     * @param id
     *      document identifier
     * @return
     *      existence status
     */
    public boolean isDocumentExists(String id) {
        return collectionClient.isDocumentExists(id);
    }

    /**
     * Find one document matching the query.
     *
     * @param rawJsonQuery
     *      query documents and vector
     * @return
     *      result if exists
     */
    public Optional findOne(String rawJsonQuery) {
        return collectionClient.findOne(rawJsonQuery);
    }

    /**
     * Find one document matching the query.
     *
     * @param query
     *      query documents and vector
     * @return
     *      result if exists
     */
    public Optional findOne(SelectQuery query) {
        return collectionClient.findOne(query);
    }

    /**
     * Find one document matching the query.
     *
     * @param query
     *      query documents and vector
     * @param clazz
     *     class of the document
     * @return
     *      result if exists
     * @param 
     *       class to be marshalled
     */
    public  Optional> findOne(SelectQuery query, Class clazz) {
        return findOne(query).map(r -> new DocumentResult<>(r, clazz));
    }

    /**
     * Find one document matching the query.
     *
     * @param query
     *      query documents and vector
     * @param clazz
     *     class of the document
     * @return
     *      result if exists
     * @param 
     *       class to be marshalled
     */
    public  Optional> findOne(String query, Class clazz) {
        return findOne(query).map(r -> new DocumentResult<>(r, clazz));
    }

    /**
     * Find one document matching the query.
     *
     * @param query
     *      query documents and vector
     * @param mapper
     *      convert a json into expected pojo
     * @return
     *      result if exists
     * @param 
     *       class to be marshalled
     */
    public  Optional> findOne(SelectQuery query, DocumentResultMapper mapper) {
        return findOne(query).map(mapper::map);
    }


    /**
     * Find one document matching the query.
     *
     * @param query
     *      query documents and vector
     * @param mapper
     *      convert a json into expected pojo
     * @return
     *      result if exists
     * @param 
     *       class to be marshalled
     */
    public  Optional> findOne(String query, DocumentResultMapper mapper) {
        return findOne(query).map(mapper::map);
    }

    // --------------------------
    // ---    Find By Id     ----
    // --------------------------

    /**
     * Find document from its id.
     *
     * @param id
     *      document identifier
     * @return
     *      document
     */
    public Optional findById(String id) {
        return findOne(SelectQuery.findById(id));
    }

    /**
     * Find document from its id.
     *
     * @param id
     *      document identifier
     * @param clazz
     *      class for target pojo
     * @return
     *      document
     * @param 
     *       class to be marshalled
     */
    public  Optional> findById(@NonNull String id, Class clazz) {
        return findById(id).map(r -> new DocumentResult<>(r, clazz));
    }

    /**
     * Find document from its id.
     *
     * @param id
     *      document identifier
     * @param mapper
     *      convert a json into expected pojo
     * @return
     *      document
     * @param 
     *       class to be marshalled
     */
    public  Optional> findById(@NonNull String id, DocumentResultMapper mapper) {
        return findById(id).map(mapper::map);
    }

    // --------------------------
    // --- Find By Vector    ----
    // --------------------------

    /**
     * Find document from its vector.
     *
     * @param vector
     *      document vector
     * @return
     *      document
     */
    public Optional findOneByVector(float[] vector) {
        return findOne(SelectQuery.findByVector(vector));
    }

    /**
     * Find document from its vector.
     *
     * @param vector
     *      document vector
     * @param clazz
     *      class for target pojo
     * @return
     *      document
     * @param 
     *       class to be marshalled
     */
    public  Optional> findOneByVector(float[] vector, Class clazz) {
        return findOneByVector(vector).map(r -> new DocumentResult<>(r, clazz));
    }

    /**
     * Find document from its vector.
     *
     * @param vector
     *      document vector
     * @param mapper
     *      convert a json into expected pojo
     * @return
     *      document
     * @param 
     *       class to be marshalled
     */
    public  Optional> findOneByVector(float[] vector, DocumentResultMapper mapper) {
        return findOneByVector(vector).map(mapper::map);
    }

    // --------------------------
    // ---       Find        ----
    // --------------------------

    /**
     * Search records with a filter
     *
     * @param query
     *      filter
     * @return
     *      all items
     */
    public Stream find(SelectQuery query) {
        return collectionClient.find(query);
    }

    /**
     * Find documents matching the pagedQuery.
     *
     * @param pagedQuery
     *      current pagedQuery
     * @return
     *      page of results
     */
    public Page findPage(SelectQuery pagedQuery) {
        return collectionClient.findPage(pagedQuery);
    }

    /**
     * Find documents matching the pagedQuery.
     *
     * @param pagedQuery
     *      current pagedQuery
     * @return
     *      page of results
     */
    public Page findPage(String pagedQuery) {
        return collectionClient.findPage(pagedQuery);
    }

    /**
     * Search records with a filter
     *
     * @param query
     *      filter
     * @param clazz
     *      class for target pojo
     * @return
     *      all items
     * @param 
     *       class to be marshalled
     */
    public   Stream> find(SelectQuery query, Class clazz) {
        return collectionClient.find(query, clazz);
    }

    /**
     * Search records with a filter
     *
     * @param query
     *      filter
     * @param mapper
     *      convert a json into expected pojo
     * @return
     *      all items
     * @param 
     *       class to be marshalled
     */
    public   Stream> find(SelectQuery query, DocumentResultMapper mapper) {
        return collectionClient.find(query, mapper);
    }

    /**
     * Get all items in a collection.
     *
     * @return
     *      all items
     */
    public Stream findAll() {
        return collectionClient.findAll();
    }

    /**
     * Find All with Object Mapping.
     *
     * @param clazz
     *      class to be used
     * @param 
     *       class to be marshalled
     * @return
     *      stream of results
     */
    public  Stream> findAll(Class clazz) {
        return collectionClient.findAll(clazz);
    }

    /**
     * Find All with Object Mapping.
     *
     * @param mapper
     *      convert a json into expected pojo
     * @param 
     *       class to be marshalled
     * @return
     *      stream of results
     */
    public  Stream> findAll(DocumentResultMapper mapper) {
        return collectionClient.findAll(mapper);
    }

    /**
     * Find documents matching the query.
     *
     * @param query
     *      current query
     * @param clazz
     *      class for target pojo
     * @return
     *      page of results
     * @param 
     *     class to be marshalled
     */
    public  Page> findPage(SelectQuery query, Class clazz) {
        return collectionClient.findPage(query, clazz);
    }

    // --------------------------
    // ---     Delete One    ----
    // --------------------------

    /**
     * Delete single record from a request.
     *
     * @param deleteQuery
     *      delete query
     * @return
     *      number of deleted records
     */
    public int deleteOne(DeleteQuery deleteQuery) {
        return collectionClient.deleteOne(deleteQuery);
    }

    /**
     * Delete single record from its id.
     *
     * @param id
     *      id
     * @return
     *      number of deleted records
     */
    public int deleteById(String id) {
        return deleteOne(DeleteQuery.deleteById(id));
    }

    /**
     * Delete single record from its vector.
     *
     * @param vector
     *      vector
     * @return
     *      number of deleted records
     */
    public int deleteByVector(float[] vector) {
        return deleteOne(DeleteQuery.deleteByVector(vector));
    }

    // --------------------------
    // ---     Delete Many   ----
    // --------------------------

    /**
     * Delete multiple records from a request.
     *
     * @param deleteQuery
     *      delete query
     * @return
     *      number of deleted records
     */
    public int deleteMany(DeleteQuery deleteQuery) {
        return collectionClient.deleteMany(deleteQuery);
    }

    /**
     * Clear the collection.
     *
     * @return
     *      number of items deleted
     */
    public int deleteAll() {
        return deleteMany(null);
    }

    // --------------------------
    // ---  Update           ----
    // --------------------------

    /**
     * Find ana update a record based on a query,
     *
     * @param query
     *      query to find the record
     * @return
     *      result of the update
     */
    public JsonResultUpdate findOneAndUpdate(UpdateQuery query) {
        return collectionClient.findOneAndDelete(query);
    }

    /**
     * Find ana replace a record based on a query,
     *
     * @param query
     *      query to find the record
     * @return
     *      result of the update
     */
    public JsonResultUpdate findOneAndReplace(UpdateQuery query) {
        return collectionClient.findOneAndReplace(query);
    }

    /**
     * Find ana delete a record based on a query.
     *
     * @param query
     *      query to find the record
     * @return
     *      result of the update
     */
    public JsonResultUpdate findOneAndDelete(UpdateQuery query) {
        return collectionClient.findOneAndDelete(query);
    }

    // --------------------------
    // ---  UpdateOne        ----
    // --------------------------

    /**
     * Update a single record.
     *
     * @param query
     *      query to find the record
     * @return
     *      update status
     */
    public UpdateStatus updateOne(UpdateQuery query) {
        return collectionClient.updateOne(query);
    }

    // --------------------------
    // ---    UpdateMany     ----
    // --------------------------

    /**
     * Update many records.
     *
     * @param query
     *      query to find the record
     * @return
     *      update status
     */
    public UpdateStatus updateMany(UpdateQuery query) {
        return collectionClient.updateMany(query);
    }

    // ------------------------------
    // ---  Semantic Search      ----
    // ------------------------------

    /**
     * Query builder.
     *
     * @param vector
     *      vector embeddings
     * @param limit
     *      limit for output
     * @return
     *      result page
     */
    public Stream findVector(float[] vector, Integer limit) {
        return findVector(vector, null, limit);
    }

    /**
     * Query builder.
     *
     * @param vector
     *      vector embeddings
     * @param filter
     *      metadata filter
     * @param limit
     *      limit for output
     * @return
     *      result page
     */
    public Stream findVector(float[] vector, Filter filter, Integer limit) {
        return find(SelectQuery.builder()
                .withFilter(filter)
                .orderByAnn(vector)
                .withLimit(limit)
                .includeSimilarity()
                .build());
    }


    /**
     * find Page.
     *
     * @param query
     *      return query Page
     * @return
     *      page of results
     */
    public Page findVectorPage(SelectQuery query) {
        return findPage(query);
    }

    /**
     * Query builder.
     *
     * @param vector
     *      vector embeddings
     * @param filter
     *      metadata filter
     * @param limit
     *      limit
     * @param pagingState
     *      paging state
     * @return
     *      result page
     */
    public Page findVectorPage(float[] vector, Filter filter, Integer limit, String pagingState) {
        return findVectorPage(SelectQuery.builder()
                .withFilter(filter)
                .orderByAnn(vector)
                .withLimit(limit)
                .withPagingState(pagingState)
                .includeSimilarity()
                .build());
    }

    /**
     * Search similarity from the vector (page by 20)
     *
     * @param vector
     *      vector embeddings
     * @param filter
     *      metadata filter
     * @param limit
     *      limit
     * @param pagingState
     *      paging state
     * @param clazz
     *      current class.
     * @param 
     *       type of document
     * @return
     *      page of results
     */
    public  Page> findVectorPage(float[] vector, Filter filter, Integer limit, String pagingState, Class clazz) {
        return collectionClient.findVectorPage(vector, filter, limit, pagingState, clazz);
    }

    /**
     * Search similarity from the vector (page by 20)
     *
     * @param vector
     *      vector embeddings
     * @param filter
     *      metadata filter
     * @param limit
     *      limit
     * @param pagingState
     *      paging state
     * @param mapper
     *      result mapper
     * @param 
     *       type of document
     * @return
     *      page of results
     */
    public  Page> findVectorPage(float[] vector, Filter filter, Integer limit, String pagingState, DocumentResultMapper mapper) {
        return collectionClient.findVectorPage(vector, filter, limit, pagingState, mapper);
    }

    /**
     * Retrieve documents from a vector.
     *
     * @param vector
     *      vector list
     * @return
     *      the list of results
     */
    public Stream findVector(float[] vector) {
        return find(SelectQuery.findByVector(vector));
    }

    /**
     * Retrieve documents from a vector.
     *
     * @param vector
     *      vector list
     * @param recordCount
     *      record count
     * @param filter
     *      metadata filter
     * @return
     *      the list of results
     */
    public Stream findVector(float[] vector, Filter filter, int recordCount) {
        return findVector(SelectQuery.builder()
                .includeSimilarity()
                .withFilter(filter)
                .withLimit(recordCount)
                .orderByAnn(vector)
                .build());
    }

    /**
     * Retrieve documents from a vector.
     *
     * @param vector
     *      vector list
     * @param clazz
     *      expected output class
     * @param 
     *       expected type
     * @return
     *      the list of results
     */
    public  Stream> findVector(float[] vector, Class clazz) {
        return findVector(vector).map(r -> new DocumentResult<>(r, clazz));
    }

    /**
     * Retrieve documents from a vector.
     *
     * @param vector
     *      vector list
     * @param mapper
     *      mapper for results
     * @return
     *      the list of results
     * @param 
     *    expected type
     */
    public  Stream> findVector(float[] vector, DocumentResultMapper mapper) {
        return findVector(vector).map(mapper::map);
    }

    // Find Vector with a filter and conditions

    /**
     * Full-fledged search with a filter and conditions.
     *
     * @param query
     *      vector query
     * @return
     *     stream of results
     */
    public Stream findVector(SelectQuery query) {
        return find(query);
    }

    /**
     * Full-fledged search with a filter and conditions.
     *
     * @param query
     *      vector query
     * @param clazz
     *      clazz for returned type
     * @return
     *     stream of results
     * @param 
     *     expected type
     */
    public  Stream> findVector(SelectQuery query, Class clazz) {
        return findVector(query).map(r -> new DocumentResult<>(r, clazz));
    }

    /**
     * Full-fledged search with a filter and conditions.
     *
     * @param query
     *      vector query
     * @param mapper
     *      mapper for results
     * @return
     *     stream of results
     * @param 
     *     expected type
     */
    public  Stream> findVector(SelectQuery query, DocumentResultMapper mapper) {
        return findVector(query).map(mapper::map);
    }

    // Find Vector with a filter and conditions

    /**
     * Query builder.
     *
     * @param vector
     *      vector embeddings
     * @param filter
     *      metadata filter
     * @param limit
     *      how many items to be retrieved at most
     * @param includeSimilarity
     *      include similarity
     * @return
     *      result page
     */
    public Stream findVector(float[] vector, Filter filter, Integer limit, boolean includeSimilarity) {
        SelectQueryBuilder builder = SelectQuery
                .builder()
                .withFilter(filter)
                .withLimit(limit)
                .orderByAnn(vector);
        if (includeSimilarity) builder.includeSimilarity();
        return findVector(builder.build());
    }

    /**
     * Query builder.
     *
     * @param query
     *   query
     * @return
     *   result page
     */
    public Page findPageVector(SelectQuery query) {
        return findPage(query);
    }

    /**
     * Internal Client for a collection.
     *
     * @return
     *      collection client
     */
    public CollectionClient getRawCollectionClient() {
        return collectionClient;
    }

    /**
     * Set this options during insertMany: {"ordered": true}.
     */
    public void enableOrderingWhenInsert() {
        collectionClient.setInsertManyOrdered(true);
    }

    /**
     * Set this options during insertMany: {"ordered": false}.
     */
    public void disableOrderingWhenInsert() {
        collectionClient.setInsertManyOrdered(false);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy