Elasticsearch Combined Search (Vector + Text) Java Client
- k-nearest neighbor (kNN) search - A
k-nearest neighbor
(kNN) search finds the k nearest vectors to a query vector, as measured by a similarity metric. Common use cases for kNN include:- Relevance ranking based on natural language processing (NLP) algorithms
- Product recommendations and recommendation engines
- Similarity search for images or videos
- Vector Search - Vector search engines — known as vector databases, semantic, or cosine search — find the nearest neighbors to a given (vectorized) query. Where traditional search relies on mentions of keywords, lexical similarity, and the frequency of word occurrences, vector search engines use distances in the embedding space to represent similarity. Finding related data becomes searching for nearest neighbors of your query.
- Vector Search usage - vector search using Elastic Search, index and search example using python requests library
ElasticHighLevelVectorTextCombinedSearchApiHTTP
Java client that demonstrates how to use the Elasticsearch Java High-Level REST Client to perform various search operations on an Elasticsearch index. Here's a general explanation of the code:
- Import Statements:
- The code imports various classes from the org.apache.http and org.elasticsearch packages for making HTTP requests and interacting with Elasticsearch.
- Main Method:
- The code contains a main method that serves as the entry point of the program.
- It prompts the user to input the index name, vector field name, vector value, text field name, and text value.
- Creating the Elasticsearch Client:
- The code calls the
createClient()
method to create an instance of theRestHighLevelClient
using the provided Elasticsearch IP address, port, username, and password. - The IP address, port, username, and password are specified as constants or placeholders (ELASTIC-IP-ADDRESS, ELASTIC-PORT, USERNAME, PASSWORD) and should be replaced with actual values.
- The code calls the
- Vector Search:
- The code calls the
performVectorSearch()
method to perform a vector search. - It constructs a
SearchRequest and
SearchSourceBuilder` to define the search request and query. - The
MoreLikeThisQueryBuilder
is used to create a vector query based on the vector field and value provided. - The search request is executed using the
client.search()
method.
- The code calls the
- Text Search:
- The code calls the
performTextSearch()
method to perform a text search. - It constructs a
SearchRequest
andSearchSourceBuilder
to define the search request and query. - The
MatchQueryBuilder
is used to create a text query based on the text field and value provided. - The search request is executed using the
client.search()
method.
- The code calls the
- Combined Search:
- The code calls the
performCombinedSearch()
method to perform a combined search. - It constructs a
SearchRequest
andSearchSourceBuilder
to define the search request and query. - The
BoolQueryBuilder
is used to create a boolean query that combines the vector query and text query. - The search request is executed using the
client.search()
method.
- The code calls the
- Printing Search Results:
- The code calls the
printSearchResults()
method to print the search results for each type of search (vector, text, combined). - The method prints the total hits, as well as the ID, score, and source of each search hit.
- The code calls the
ElasticHighLevelVectorTextCombinedSearchApiHTTPS
Java client is almost similar to previous one with only one difference which demonstrates how to use the Elasticsearch Java High-Level REST Client to perform various search operations on an Elasticsearch index over HTTPS withSSL/TLS
.
- Creating the Elasticsearch Client with
SSL/TLS
:- The code calls the
createClient()
method to create an instance of theRestHighLevelClient
using the provided Elasticsearch IP address, port, username, password, andtruststore
information. - The IP address, port, username, and password are specified as constants or placeholders (ELASTIC-IP-ADDRESS, ELASTIC-PORT, ELASTIC-USERNAME, ELASTIC-PASSWORD) and should be replaced with actual values.
- The truststore information includes the truststore file path, password, and loading the truststore into a
KeyStore
instance. - An
SSLContext
is created and initialized with the truststore information. - The
RestClientBuilder
is configured with theSSLContext
, credentials provider, and hostname verifier to enableSSL/TLS
connections.
- The code calls the