meilisearch安装和使用样例

MeiliSearch 技术文档 简介 MeiliSearch 是一个开源的全文搜索引擎,旨在提供快速、可靠的搜索体验。它是用 Rust 编写的,具有高性能和可扩展性。

支持中文搜索:MeiliSearch 对中文有良好的支持,不需要额外的配置。

高度可定制:搜索和索引都可以高度定制,提供了诸如错字容忍、过滤器和同义词等功能。

快速搜索:MeiliSearch 的搜索速度通常在 50 毫秒以内,能够提供快速的搜索体验。

易于使用和部署:它提供了简单的安装和部署方式,并且有友好的 Web 界面用于开发调试。     

和其它方案比较结果官方给与了信息,具体信息可以查看下方链接

Comparison to alternatives — Meilisearch documentationDeciding on a search engine for your project is an important but difficult task. This article describes the differences between Meilisearch and other search engines.icon-default.png?t=N7T8https://www.meilisearch.com/docs/learn/what_is_meilisearch/comparison_to_alternatives

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问题,解决方式:org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.meilisearch.sdk.Client]: Factory method 'client' threw exception with message: okhttp3/MediaType · Issue #580 · meilisearch/meilisearch-java · GitHub

重新引用了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)

查询结果与页面查询结果一致;查询条件和不同的参数可以观看官方文档

Search — Meilisearch API referenceThe /search route allows you to search your indexed documents. This route includes a large number of parameters you can use to customize returned search results.icon-default.png?t=N7T8https://www.meilisearch.com/docs/reference/api/search#show-matches-position

  • 29
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值