Skip to content

alallema/meilisearch-java

 
 

Repository files navigation

MeiliSearch Java

MeiliSearch Java

Version Tests License Bors enabled

⚡ 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!

Table of Contents

📖 Documentation

See our Documentation or our API References.

🔧 Installation

meilisearch-java is available from JCentral official repository. To be able to import this package, declare it as a dependency in your project:

Maven

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>

Gradle

Add the following line to the dependencies section of your build.gradle:

implementation 'com.meilisearch.sdk:meilisearch-java:0.3.1'

🚀 Getting Started

Add documents

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.

Basic Search

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)

Custom Search

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
    }]
  }
}]

🤖 Compatibility with MeiliSearch

This package only guarantees the compatibility with the version v0.20.0 of MeiliSearch.

💡 Learn More

The following sections may interest you:

⚙️ Development Workflow and Contributing

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.

About

Java client for MeiliSearch

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 99.5%
  • Shell 0.5%