Java Client Examples
This page provides detailed examples of using the hlquery Java client for various tasks.
Setup
First, initialize the client:
import hlquery.Client;
import java.util.HashMap;
import java.util.Map;
Client client = new Client("http://localhost:9200");
Running the Examples
Using Make
The easiest way to run the modular examples is using the provided Makefile:
cd etc/api/java
# Run Collections examples
make examples-cols
# Run Documents examples
make examples-docs
# Run Search examples
make examples-search
# Run the general quickstart example
make example
Manual Execution (Without Make)
If you prefer to run them manually:
-
Compile everything:
mkdir -p binjavac -cp lib/json.jar -d bin $(find src/main/java -name "*.java") -
Run a specific example:
# Collectionsjava -cp lib/json.jar:bin hlquery.examples.Collections# Documentsjava -cp lib/json.jar:bin hlquery.examples.Documents# Searchjava -cp lib/json.jar:bin hlquery.examples.Search
Collection Management
Creating a Collection with Schema
import org.json.JSONArray;
import org.json.JSONObject;
JSONObject schema = new JSONObject();
JSONArray fields = new JSONArray();
fields.put(new JSONObject()
.put("name", "title")
.put("type", "string"));
fields.put(new JSONObject()
.put("name", "description")
.put("type", "string"));
fields.put(new JSONObject()
.put("name", "price")
.put("type", "float")
.put("facet", true));
schema.put("fields", fields);
Response res = client.collections().create("products", schema);
Listing Collections with Pagination
Response res = client.listCollections(0, 50);
if (res.isSuccess()) {
JSONObject body = res.getBodyAsObject();
JSONArray collections = body.getJSONArray("collections");
// Iterate through collections...
}
Document Operations
Adding and Updating Documents
JSONObject doc = new JSONObject()
.put("id", "p123")
.put("title", "Wireless Mouse")
.put("price", 29.99);
// Add document
client.documents().add("products", doc);
// Update document (partial update supported)
JSONObject update = new JSONObject().put("price", 24.99);
client.documents().update("products", "p123", update);
Bulk Import
JSONArray docs = new JSONArray();
docs.put(new JSONObject().put("id", "1").put("title", "A"));
docs.put(new JSONObject().put("id", "2").put("title", "B"));
client.documents().importDocuments("products", docs);
Search
Basic Search
Map<String, Object> params = new HashMap<>();
params.put("q", "mouse");
params.put("query_by", "title");
params.put("sort_by", "price:asc");
Response res = client.search("products", params);
Vector Search
Map<String, Object> params = new HashMap<>();
float[] vector = {0.1f, 0.5f, -0.2f};
params.put("vector_query", vector);
params.put("limit", 5);
Response res = client.vectorSearch("products", params);
Multi-Search
import org.json.JSONArray;
JSONArray searches = new JSONArray();
searches.put(new JSONObject()
.put("collection", "products")
.put("q", "mouse"));
searches.put(new JSONObject()
.put("collection", "accessories")
.put("q", "mouse"));
Response res = client.searchApi().multiSearch(searches);
Maintenance
Flushing Data
client.flush();