/meilisearch-java

Java client library for Meilisearch

Primary LanguageJavaMIT LicenseMIT

MeiliSearch Java

License Slack

âš¡ Lightning Fast, Ultra Relevant, and Typo-Tolerant Search Engine MeiliSearch client written in Java

MeiliSearch Java is a client for MeiliSearch written in Java. MeiliSearch is a powerful, fast, open-source, easy to use and deploy search engine. Both searching and indexing are highly customizable. Features such as typo-tolerance, filters, and synonyms are provided out-of-the-box.

Table of Contents

🔧 Installation

dependencies {
  // add meilisearch core package
  implementation 'net.riyazali.meilisearch-java:meili:master-SNAPSHOT'
  
  // add default remote and encoder packages (or you could also provide custom implementations!)
  implementation 'net.riyazali.meilisearch-java:meili-remote-okhttp:master-SNAPSHOT'
  implementation 'net.riyazali.meilisearch-java:meili-encoder-gson:master-SNAPSHOT'
}

Run MeiliSearch

There are many easy ways to download and run a MeiliSearch instance.

For example, if you use Docker:

$ docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --master-key=masterKey

NB: you can also download MeiliSearch from Homebrew or APT.

🚀 Getting started

public final class Example {
  
  public static void main(String args[]) throws Exception {
    Meili client = new Meili(
        // uses the default okhttp remote and gson encoder
        HttpRemote.create("https://meili.example.com:7700", "API_KEY"),
        GsonEncoder.create()
    );
    
    // get or create an index
    Index<Movie> index = client.index(Movie.class);
    
    // insert new documents
    Update op = index.insert(new Movie(/*...*/), new Movie(/*...*/));
    
    // add/update/delete operations return an update object 
    // which represents an asynchromous task queued on the meili server
    // use the returned object to query for the operation's status
    // see https://docs.meilisearch.com/guides/advanced_guides/asynchronous_updates.html for more
    
    // to wait until the update is applied
    while(!op.done()) {
      Thread.sleep(1_000);
      op = op.refresh();
    }
    
    // to lookup / search an index
    SearchPage<Movie> result = index.search("harry pottre"); // meili is typo-tolerant!
    for(Movie movie : result) {
      // ... cool stuff here ...
    }
  }
  
  // just add @Document to your model class and you're good to go!
  @Document(index = "movies", primaryKey = "id")
  static class Movie {
    private String id;
    // other fields omitted for brevity...
  }
}

🤖 Compatibility with MeiliSearch

This package is compatible with the following MeiliSearch versions:

  • v0.10.X

🎬 Examples

Most of the HTTP routes of MeiliSearch are accessible via methods in this SDK.
You can check out the API documentation.

Indexes

Get / create an index

// index creation is handled automatically by the sdk
Index<Movie> index = client.index(Movie.class);  // this will automatically create an index if one doesn't exist

// auto creation (while handy) incurs additional round-trip
// if you don't want that, you can use the following signature
Index<Movie> index = client.index(Movie.class, false /* autoCreate */);

Documents

Fetch documents

// use the document's primary key to fetch an instance
Movie movie = index.get("1234");

Add documents

// add a single document
Update op = index.insert(new Movie(/* ... */));

// or many documents at once
Update op = index.insert(new Movie(/* .. */), new Movie(/* .. */));

Delete documents

// Delete one document
index.delete(movie);

// Delete several documents
index.delete(movie0, movie1, movie2);

// Delete all documents /!\
index.clear();

Search

Basic search

SearchPage<Movie> result = index.search("prince");

Custom search

All the supported options are described in this documentation section.

SearchPage<Movie> result = index.search(
    SearchConfig.builder().query("harry pottre").highlight("title").build()
);