- Notifications
You must be signed in to change notification settings - Fork131
Java client for Meilisearch
License
meilisearch/meilisearch-java
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
⚡ The Meilisearch API client written for Java ☕️
Meilisearch Java is the Meilisearch API client for Java developers.
Meilisearch is an open-source search engine.Learn more about Meilisearch.
- 📖 Documentation
- 🔧 Installation
- 🚀 Getting started
- 🛠 Customization
- 🤖 Compatibility with Meilisearch
- 💡 Learn more
- ⚙️ Contributing
This readme contains all the documentation you need to start using this Meilisearch SDK.
For general information on how to use Meilisearch—such as our API reference, tutorials, guides, and in-depth articles—refer to ourmain documentation website.
meilisearch-java is available from JCentral official repository. To be able to import this package, declare it as a dependency in your project:
Add the following code to the<dependencies> section of your project:
<dependency> <groupId>com.meilisearch.sdk</groupId> <artifactId>meilisearch-java</artifactId> <version>0.17.1</version> <type>pom</type></dependency>
Add the following line to thedependencies section of yourbuild.gradle:
implementation'com.meilisearch.sdk:meilisearch-java:0.17.1'meilisearch-java also requiresokhttp as a peer dependency.
⚡️Launch, scale, and streamline in minutes with Meilisearch Cloud—no maintenance, no commitment, cancel anytime.Try it free now.
🪨 Prefer to self-host?Download and deploy our fast, open-source search engine on your own infrastructure.
packagecom.meilisearch.sdk;importorg.json.JSONArray;importorg.json.JSONObject;importjava.util.ArrayList;classTestMeilisearch {publicstaticvoidmain(String[]args)throwsException {JSONArrayarray =newJSONArray();ArrayListitems =newArrayList() {{add(newJSONObject().put("id","1").put("title","Carol").put("genres",newJSONArray("[\"Romance\",\"Drama\"]")));add(newJSONObject().put("id","2").put("title","Wonder Woman").put("genres",newJSONArray("[\"Action\",\"Adventure\"]")));add(newJSONObject().put("id","3").put("title","Life of Pi").put("genres",newJSONArray("[\"Adventure\",\"Drama\"]")));add(newJSONObject().put("id","4").put("title","Mad Max: Fury Road").put("genres",newJSONArray("[\"Adventure\",\"Science Fiction\"]")));add(newJSONObject().put("id","5").put("title","Moana").put("genres",newJSONArray("[\"Fantasy\",\"Action\"]")));add(newJSONObject().put("id","6").put("title","Philadelphia").put("genres",newJSONArray("[\"Drama\"]"))); }};array.put(items);Stringdocuments =array.getJSONArray(0).toString();Clientclient =newClient(newConfig("http://localhost:7700","masterKey"));// An index is where the documents are stored.Indexindex =client.index("movies");// If the index 'movies' does not exist, Meilisearch creates it when you first add the documents.index.addDocuments(documents);// => { "taskUid": 0 } }}
With thetaskUid, you can check the status (enqueued,canceled,processing,succeeded orfailed) of your documents addition using thetask endpoint.
A basic search can be performed by callingindex.search() method, with a simple string query.
importcom.meilisearch.sdk.model.SearchResult;// Meilisearch is typo-tolerant:SearchResultresults =index.search("carlo");System.out.println(results);
- Output:
SearchResult(hits=[{id=1.0, title=Carol, genres:[Romance, Drama]}], offset=0, limit=20, estimatedTotalHits=1, facetDistribution=null, processingTimeMs=3, query=carlo)If you want a custom search, the easiest way is to create aSearchRequest object, and set the parameters that you need.
All the supported options are described in thesearch parameters section of the documentation.
importcom.meilisearch.sdk.SearchRequest;// ...SearchResultresults = (SearchResult)index.search(newSearchRequest("of") .setShowMatchesPosition(true) .setAttributesToHighlight(newString[]{"title"}));System.out.println(results.getHits());
- Output:
[{"id":3,"title":"Life of Pi","genres":["Adventure","Drama"],"_formatted":{"id":3,"title":"Life <em>of</em> Pi","genres":["Adventure","Drama"] },"_matchesPosition":{"title":[{"start":5.0,"length":2.0 }] }}]If you want to enable filtering, you must add your attributes to thefilterableAttributes index setting.
index.updateFilterableAttributesSettings(newString[]{"id","genres"});
You only need to perform this operation once.
Note that Meilisearch will rebuild your index whenever you updatefilterableAttributes. Depending on the size of your dataset, this might take time. You can track the process using thetask status.
Then, you can perform the search:
index.search(newSearchRequest("wonder") .setFilter(newString[] {"id > 1 AND genres = Action"}));
{"hits": [ {"id":2,"title":"Wonder Woman","genres": ["Action","Adventure"] } ],"offset":0,"limit":20,"estimatedTotalHits":1,"processingTimeMs":0,"query":"wonder"}importcom.meilisearch.sdk.SearchResultPaginated;// ...SearchResultPaginatedresults = (SearchResultPaginated)index.search(newSearchRequest("wonder") .setPage(1) .setHitsPerPage(20));
{"hits": [ {"id":2,"title":"Wonder Woman","genres": ["Action","Adventure"] } ],"query":"wonder","processingTimeMs":0,"hitsPerPage":20,"page":1,"totalPages":1,"totalHits":1}The default JSON library isGson. You can however use another library with theJsonHandler Class.
Notes: We strongly recommend using theGson library.
Initialize yourConfig and assign it a newJacksonJsonHandler object asJsonHandler.Set up yourClient with it.
importcom.meilisearch.sdk.json.JacksonJsonHandler;Configconfig =newConfig("http://localhost:7700","masterKey",newJacksonJsonHandler());Clientclient =newClient(config);
To create your own JSON handler, you must conform to theJsonHandler interface by implementing its two methods.
Stringencode(Objecto)throwsException; <T>Tdecode(Objecto,Class<?>targetClass,Class<?>...parameters)throwsException;
Then create your client by initializing yourConfig with your new handler.
Configconfig =newConfig("http://localhost:7700","masterKey",newmyJsonHandler());Clientclient =newClient(config);
This package guarantees compatibility withversion v1.x of Meilisearch, but some features may not be present. Please check theissues for more info.
This SDK is compatible with the following JDK versions:
| SDK Version | Supported Java Versions |
|---|---|
| v1.x | JDK 17 and above |
The following sections in our main documentation website may interest you:
- Manipulate documents: see theAPI references or read more aboutdocuments.
- Search: see theAPI references or follow our guide onsearch parameters.
- Manage the indexes: see theAPI references or read more aboutindexes.
- Configure the index settings: see theAPI references or follow our guide onsettings parameters.
Any new contribution is more than welcome in this project!
If you want to know more about the development workflow or want to contribute, please visit ourcontributing guidelines for detailed instructions!
Meilisearch provides and maintains manySDKs and Integration tools like this one. We want to provide everyone with anamazing search experience for any kind of project. If you want to contribute, make suggestions, or just know what's going on right now, visit us in theintegration-guides repository.
About
Java client for Meilisearch
Topics
Resources
License
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.