How to set vector value while doing search in Azure AI search Java SDK?

Yash Deokate 0 Reputation points
2024-11-20T13:06:44.4933333+00:00

I want to do a vector search in Azure AI search Java SDK. In some old tutorials I see there was a setValue function in SearchQueryVector class. But now I am not sure if that class exists. In VectorSearchOptions class I can add List<VectorQuery>. VectorQuery has methods to set k value, fields and other properties but I am confused how to set the actual vectors.

Azure AI Search
Azure AI Search
An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.
1,083 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 12,816 Reputation points
    2024-11-20T18:05:45.7166667+00:00

    Hello Yash Deokate,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you are in need of how to set vector value while doing search in Azure AI search Java SDK.

    From my understanding of this issue, the SearchQueryVector class and its setValue function might have been deprecated or replaced in the newer versions of the Azure AI Search Java SDK and instead, you can use the VectorSearchOptions class to set up your vector search.

    This is a basic method on how you can set the actual vectors using the VectorSearchOptions class:

    First, create a VectorQuery object, setting the vector, the number of nearest neighbors (k), and the fields to search. Then, add this query to the VectorSearchOptions. Finally, execute the search with the SearchClient.

    List<Double> vector = Arrays.asList(-0.009154141, 0.018708462, -0.02178128, -0.00086512347); // Example vector
    VectorQuery vectorQuery = new VectorQuery()
        .setVector(vector)
        .setK(5)
        .setFields("contentVector");
    VectorSearchOptions options = new VectorSearchOptions()
        .setVectorQueries(Collections.singletonList(vectorQuery));
    SearchClient searchClient = new SearchClientBuilder()
        .endpoint("https://<your-search-service>.search.windows.net")
        .credential(new AzureKeyCredential("<your-api-key>"))
        .indexName("<your-index-name>")
        .buildClient();
    SearchPagedIterable searchResults = searchClient.search("", options);
    

    This setup allows you to perform vector searches efficiently with the updated SDK.

    For more detailed information, you can refer to the Azure AI Search documentation - https://learn.microsoft.com/en-us/azure/search/vector-search-how-to-query

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.