Java client library for accessing the HTTP API of Typesense search engine.
The client is available on Maven central:
dependencies {
implementation 'org.typesense:typesense-java:1.1.0'
}
<dependency>
<groupId>org.typesense</groupId>
<artifactId>typesense-java</artifactId>
<version>1.1.0</version>
</dependency>
import org.typesense.api.*;
import org.typesense.model.*;
import org.typesense.resources.*;
List<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>");
Client client = new Client(configuration);
List<Field> fields = new ArrayList<>();
fields.add(new Field().name("countryName").type(FieldTypes.STRING));
fields.add(new Field().name("capital").type(FieldTypes.STRING));
fields.add(new Field().name("gdp").type(FieldTypes.INT32).facet(true).sort(true));
CollectionSchema collectionSchema = new CollectionSchema();
collectionSchema.name("Countries").fields(fields).defaultSortingField("gdp");
client.collections().create(collectionSchema);
String schemaJson = new String(
Files.readAllBytes(Paths.get("schema.json")),
StandardCharsets.UTF_8
);
client.collections().create(schemaJson);
Map<String, Object> hmap = new HashMap<>();
hmap.put("countryName","India");
hmap.put("capital","Delhi");
hmap.put("gdp", 10);
client.collections("Countries").documents().create(hmap);
Map<String, Object> hmap = new HashMap<>();
hmap.put("countryName","India");
hmap.put("capital","Delhi");
hmap.put("gdp", 5);
client.collections("Countries").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")
.queryBy("countryName,capital")
.prefix("true,false");
SearchResult searchResult = client.collections("Countries").documents().search(searchParameters);
Map<String, Object> hmap = new HashMap<>();
hmap.put("gdp", 8);
client.collections("Countries").documents("28").update(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();
AnalyticsRuleSchema analyticsRule = new AnalyticsRuleSchema();
analyticsRule.setName("popular-queries");
analyticsRule.setType(AnalyticsRuleSchema.TypeEnum.POPULAR_QUERIES);
analyticsRule.setParams(new AnalyticsRuleParameters()
.source(new AnalyticsRuleParametersSource()
.collections(Arrays.asList("Countries")))
.destination(new AnalyticsRuleParametersDestination()
.collection("top_searches")));
client.analytics().rules().create(analyticsRule);
AnalyticsRuleUpsertSchema analyticsRule = new AnalyticsRuleUpsertSchema()
.type(AnalyticsRuleUpsertSchema.TypeEnum.NOHITS_QUERIES)
.params(new AnalyticsRuleParameters()
.source(new AnalyticsRuleParametersSource()
.collections(Arrays.asList("Countries")))
.destination(new AnalyticsRuleParametersDestination()
.collection("failed_searches")));
client.analytics().rules().upsert("failed-searches", analyticsRule);
AnalyticsRulesRetrieveSchema rules = client.analytics().rules().retrieve();
AnalyticsRuleSchema rule = client.analytics().rules("failed-searches").retrieve();
client.analytics().rules("failed-searches").delete();
AnalyticsEventCreateSchema analyticsEvent = new AnalyticsEventCreateSchema()
.type("conversion")
.name("purchase_made")
.data(Map.of(
"product_id", "123",
"user_id", "user_456",
"amount", "99.99"
));
client.analytics().events().create(analyticsEvent);
List<String> stopwords = new ArrayList<>();
stopwords.add("the");
stopwords.add("of");
stopwords.add("and");
StopwordsSetUpsertSchema stopwordsSet = new StopwordsSetUpsertSchema();
stopwordsSet.stopwords(stopwords);
client.stopwords().upsert("common-words", stopwordsSet);
StopwordsSetRetrieveSchema set = client.stopwords("common-words").retrieve();
StopwordsSetsRetrieveAllSchema sets = client.stopwords().retrieve();
client.stopwords("common-words").delete();
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();
Map<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