Java client library for accessing the HTTP API of Typesense search engine.
Note: This package is still under development. Some existing APIs might change or new APIs might be available in the future.
Download the JAR file from the releases section of this repository and add it to your project.
import org.typesense.api.*;
import org.typesense.models.*;
import org.typesense.resources.*;
ArrayList<Node> nodes = new ArrayList<>();
nodes.add(
new Node(
"http", // For Typesense Cloud use https
"localhost", // For Typesense Cloud use xxx.a1.typesense.net
"8108" // For Typesense Cloud use 443
)
);
Configuration configuration = new Configuration(nodes, Duration.ofSeconds(2),"<API_KEY>");
Clienr client = new Client(configuration);
ArrayList<Field> fields = new ArrayList<>();
fields.add(new Field().name("countryName").type("string"));
fields.add(new Field().name("capital").type("string"));
fields.add(new Field().name("gdp").type("int32").facet(true));
CollectionSchema collectionSchema = new CollectionSchema();
collectionSchema.name("Countries").fields(fields).defaultSortingField("gdp");
client.collections().create(collectionSchema);
HashMap<String, Object> hmap = new HashMap<>();
hmap.put("countryName","India");
hmap.put("capital","Delhi");
hmap.put("gdp", 10);
client.collections("contryName").documents().create(hmap);
HashMap<String, Object> hmap = new HashMap<>();
hmap.put("countryName","India");
hmap.put("capital","Delhi");
hmap.put("gdp", 5);
client.collections("contryName").documents().upsert(hmap);
ImportDocumentsParameters queryParameters = new ImportDocumentsParameters();
queryParameters.action("create");
String documentList = "{\"countryName\": \"India\", \"capital\": \"Washington\", \"gdp\": 5215}\n" +
"{\"countryName\": \"Iran\", \"capital\": \"London\", \"gdp\": 5215}";
// Import your document as JSONL string from a file.
client.collections("countries").documents().import_(documentList, queryParameters)
SearchParameters searchParameters = new SearchParameters()
.q("tokoyo")
.addQueryByItem("countryName").addQueryByItem("capital")
.addPrefixItem(true).addPrefixItem(false);
SearchResult searchResult = client.collections("countries").documents().search(searchParameters);
HashMap<String, Object> hmap = new HashMap<>();
hmap.put("gdp", 8);
client.collections("countries").documents("28").retrieve(hmap);
client.collections("countries").documents("28").retrieve();
client.collections("countries").documents("28").delete();
DeleteDocumentsParameters deleteDocumentsParameters = new DeleteDocumentsParameters();
deleteDocumentsParameters.filterBy("gdp:=[2,8]");
deleteDocumentsParameters.batchSize(10);
client.collections("countries").retrieve();
client.collections().retrieve();
client.collections("countries").delete();
client.collections("Countries").documents().export();
ApiKeySchema apiKeySchema = new ApiKeySchema();
List<String> actionValues = new ArrayList<>();
List<String> collectionValues = new ArrayList<>();
actionValues.add("*");
collectionValues.add("*");
apiKeySchema.description("Admin Key").actions(actionValues).collections(collectionValues);
client.keys().create(apiKeySchema);
ApiKeySchema apiKeySchema = new ApiKeySchema();
List<String> actionValues = new ArrayList<>();
List<String> collectionValues = new ArrayList<>();
actionValues.add("documents:search");
collectionValues.add("countries");
apiKeySchema.description("Search only Key").actions(actionValues).collections(collectionValues);
client.keys().create(apiKeySchema);
client.keys("6").retrieve();
client.keys().retrieve();
client.keys("6").delete();
SearchOverrideSchema searchOverrideSchema = new SearchOverrideSchema();
List<SearchOverrideInclude> searchOverrideIncludes = new ArrayList<>();
searchOverrideIncludes.add(new SearchOverrideInclude().id("422").position(1));
searchOverrideIncludes.add(new SearchOverrideInclude().id("54").position(2));
List<SearchOverrideExclude> searchOverrideExcludes = new ArrayList<>();
searchOverrideExcludes.add(new SearchOverrideExclude().id("287"));
searchOverrideSchema.rule(new SearchOverrideRule().query("new york").match("exact"))
.includes(searchOverrideIncludes)
.excludes(searchOverrideExcludes);
client.collections("countries").overrides().upsert("new-york", searchOverrideSchema)
client.collections("countries").overrides("new-york").retrieve();
client.collections("countries").overrides().retrieve();
client.collections("countries").overrides("new-york").delete();
CollectionAliasSchema collectionAliasSchema = new CollectionAliasSchema();
collectionAliasSchema.collectionName("countries");
client.aliases().upsert("countries2", collectionAliasSchema)
client.aliases("countries2").retrieve();
client.aliases().retrieve();
client.aliases("countries2").delete();
SearchSynonymSchema synonym = new SearchSynonymSchema();
synonym.addSynonymsItem("France").addSynonymsItem("Germany").addSynonymsItem("Sweden");
client.collections("countries").synonyms().upsert("country-synonyms",synonym)
SearchSynonymSchema synonym = new SearchSynonymSchema();
synonym.root("europe");
synonym.addSynonymsItem("France").addSynonymsItem("Germany").addSynonymsItem("Sweden");
client.collections("countries").synonyms().upsert("continent-synonyms",synonym)
client.collections("countries").synonyms("continent-synonyms").retrieve();
client.collections("countries").synonyms().retrieve();
client.collections("countries").synonyms("continent-synonyms").delete();
HashMap<String, String> query = new HashMap<>();
query.put("snapshot_path","/tmp/typesense-data-snapshot");
client.operations.perform("snapshot",query);
client.operations.perform("vote");
client.health.retrieve();
Please read CONTRIBUTING.md for details on the process for submitting pull requests to this repository.
typesense-java
is distributed under the Apache 2 license.
Please open a Github issue or join our Slack Community