MeiliSearch 技术文档 简介 MeiliSearch 是一个开源的全文搜索引擎,旨在提供快速、可靠的搜索体验。它是用 Rust 编写的,具有高性能和可扩展性。
支持中文搜索:MeiliSearch 对中文有良好的支持,不需要额外的配置。
高度可定制:搜索和索引都可以高度定制,提供了诸如错字容忍、过滤器和同义词等功能。
快速搜索:MeiliSearch 的搜索速度通常在 50 毫秒以内,能够提供快速的搜索体验。
易于使用和部署:它提供了简单的安装和部署方式,并且有友好的 Web 界面用于开发调试。
和其它方案比较结果官方给与了信息,具体信息可以查看下方链接
docker安装步骤:
拉取:
docker pull getmeili/meilisearch:v1.9 |
启动:
docker run -it --rm \ -p 7700:7700 \ -e MEILI_ENV='development' \ -v $(pwd)/meili_data:/meili_data \ getmeili/meilisearch:v1.9 |
此时已经安装成功。
可以在页面中访问页面 http://localhost:7700/
java代码:
根据官方文档中样例需要引入pom依赖
<dependency> <groupId>com.meilisearch.sdk</groupId> <artifactId>meilisearch-java</artifactId> <version>0.11.7</version> </dependency> |
添加数据
import cn.hutool.json.JSONArray; import cn.hutool.json.JSONException; import cn.hutool.json.JSONObject; import com.meilisearch.sdk.Client; import com.meilisearch.sdk.Config; import com.meilisearch.sdk.Index; import java.util.ArrayList; /** * @author lizhongde * @Description * @createTime 2024年07月11日 11:07:00 */ public class MeilisearchTest { public static void main(String[] args) throws JSONException { JSONArray array = new JSONArray(); ArrayList items = new ArrayList() {{ add(new JSONObject().put("id", "1").put("title", "Carol").put("genres", new JSONArray("[\"Romance\",\"Drama\"]"))); add(new JSONObject().put("id", "2").put("title", "Wonder Woman").put("genres", new JSONArray("[\"Action\",\"Adventure\"]"))); add(new JSONObject().put("id", "3").put("title", "Life of Pi").put("genres", new JSONArray("[\"Adventure\",\"Drama\"]"))); add(new JSONObject().put("id", "4").put("title", "Mad Max: Fury Road").put("genres", new JSONArray("[\"Adventure\",\"Science Fiction\"]"))); add(new JSONObject().put("id", "5").put("title", "Moana").put("genres", new JSONArray("[\"Fantasy\",\"Action\"]"))); add(new JSONObject().put("id", "6").put("title", "Philadelphia").put("genres", new JSONArray("[\"Drama\"]"))); }}; array.put(items); String documents = array.getJSONArray(0).toString(); Client client = new Client(new Config("http://localhost:7700", "masterKey")); // An index is where the documents are stored. Index index = client.index("movies"); // If the index 'movies' does not exist, Meilisearch creates it when you first add the documents. index.addDocuments(documents); // => { "taskUid": 0 } } } |
根据官方文档中用例运行时会发送错误,错误信息:
重新引用了okhttp依赖
<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.10.0</version> </dependency> |
再次运行代码可以成功,同时页面中也将展示数据
根据填入的数据可以在页面进行查询
填入数据完成。
查询数据:
Client client = new Client(new Config("http://localhost:7700", "masterKey")); // An index is where the documents are stored. Index index = client.index("movies"); //获取数据 SearchResult results = index.search("ro"); System.out.println(results); |
打印结果为:
SearchResult(hits=[{genres=[Romance, Drama], id=1, title=Carol}, {genres=[Adventure, Science Fiction], id=4, title=Mad Max: Fury Road}], facetDistribution=null, processingTimeMs=2, query=ro, offset=0, limit=20, estimatedTotalHits=2) |
查询结果与页面查询结果一致;查询条件和不同的参数可以观看官方文档