Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Java client for Meilisearch

License

NotificationsYou must be signed in to change notification settings

Mashull/meilisearch-java

 
 

Repository files navigation

Meilisearch Java

Meilisearch Java

VersionTestsLicenseBors enabledCode Coverage

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

Table of Contents

📖 Documentation

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.

🔧 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.15.0</version>  <type>pom</type></dependency>

Gradle

Add the following line to thedependencies section of yourbuild.gradle:

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

⚠️meilisearch-java also requiresokhttp as a peer dependency.

Run Meilisearch

⚡️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.

🚀 Getting started

Add documents

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.

Basic Search

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)

Custom Search

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

Custom Search With Filters

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

Custom Search With Pagination

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}

🛠 Customization

JSON

Default JSONGsonJsonHandler

The default JSON library isGson. You can however use another library with theJsonHandler Class.

Notes: We strongly recommend using theGson library.

UsingJacksonJsonHandler

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);

Use a CustomJsonHandler

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);

🤖 Compatibility with Meilisearch

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 VersionSupported Java Versions
v1.xJDK 8 and above

💡 Learn more

The following sections in our main documentation website may interest you:

⚙️ 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 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

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java99.9%
  • Shell0.1%

[8]ページ先頭

©2009-2025 Movatter.jp