MeiliSearch | Documentation | Slack | Roadmap | Website | FAQ
⚡ The MeiliSearch API client written for Java ☕️
MeiliSearch Java is the MeiliSearch API client for Java developers.
MeiliSearch is an open-source search engine. Discover what MeiliSearch is!
- 📖 Documentation
- 🔧 Installation
- 🚀 Getting Started
- 🤖 Compatibility with MeiliSearch
- 💡 Learn More
- ⚙️ Development Workflow and Contributing
See our Documentation or our API References.
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.3.1</version>
<type>pom</type>
</dependency>
Add the following line to the dependencies
section of your build.gradle
:
implementation 'com.meilisearch.sdk:meilisearch-java:0.3.1'
import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.Config;
import com.meilisearch.sdk.Index;
class TestMeiliSearch {
public static void main(String[] args) throws Exception {
final String documents = "["
+ "{\"book_id\": 123, \"title\": \"Pride and Prejudice\"},"
+ "{\"book_id\": 456, \"title\": \"Le Petit Prince\"},"
+ "{\"book_id\": 1, \"title\": \"Alice In Wonderland\"},"
+ "{\"book_id\": 1344, \"title\": \"The Hobbit\"},"
+ "{\"book_id\": 4, \"title\": \"Harry Potter and the Half-Blood Prince\"},"
+ "{\"book_id\": 2, \"title\": \"The Hitchhiker\'s Guide to the Galaxy\"}"
+ "]";
Client client = new Client(new Config("http://localhost:7700", "masterKey"));
// An index is where the documents are stored.
Index index = client.index("books");
// If the index 'books' does not exist, MeiliSearch creates it when you first add the documents.
index.addDocuments(documents); // => { "updateId": 0 }
}
}
With the updateId
, you can check the status (enqueued
, processed
or failed
) of your documents addition using the update endpoint.
A basic search can be performed by calling index.search()
method, with a simple string query.
import com.meilisearch.sdk.model.SearchResult;
// MeiliSearch is typo-tolerant:
SearchResult results = index.search("harry pottre");
System.out.println(results);
- Output:
SearchResult(hits=[{book_id=4.0, title=Harry Potter and the Half-Blood Prince}], offset=0, limit=20, nbHits=1, exhaustiveNbHits=false, facetsDistribution=null, exhaustiveFacetsCount=false, processingTimeMs=3, query=harry pottre)
If you want a custom search, the easiest way is to create a SearchRequest
object, and set the parameters that you need.
All the supported options are described in the search parameters section of the documentation.
import com.meilisearch.sdk.SearchRequest;
// ...
String results = index.search(
new SearchRequest("in")
.setMatches(true)
.setAttributesToHighlight(new String[]{"title"})
);
System.out.println(results.getHits());
- Output:
[{
"book_id":1,
"title":"Alice In Wonderland",
"_formatted":{
"book_id":1,
"title":"Alice <em>In</em> Wonderland"
},
"_matchesInfo":{
"title":[{
"start":6,
"length":2
}]
}
}]
This package only guarantees the compatibility with the version v0.20.0 of MeiliSearch.
The following sections may interest you:
- Manipulate documents: see the API references or read more about documents.
- Search: see the API references or follow our guide on search parameters.
- Manage the indexes: see the API references or read more about indexes.
- Configure the index settings: see the API references or follow our guide on settings 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 our contributing guidelines for detailed instructions!
MeiliSearch provides and maintains many SDKs and Integration tools like this one. We want to provide everyone with an amazing 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 the integration-guides repository.