meilisearch,一个es的小型需求替代

/ 后端 / 没有评论 / 455浏览

下载程序执行

下载地址https://github.com/meilisearch/meilisearch/releases 执行会出现一个key,将在程序中连接使用

编写依赖,并编写测试代码

 <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.11.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.meilisearch.sdk/meilisearch-java -->
        <dependency>
            <groupId>com.meilisearch.sdk</groupId>
            <artifactId>meilisearch-java</artifactId>
            <version>0.11.1</version>
        </dependency>
import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.Config;
import com.meilisearch.sdk.SearchRequest;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.model.Searchable;

public class MeilisearchTest {

    public static void main(String[] args) throws MeilisearchException {
        Client client = new Client(new Config("http://127.0.0.1:7700", "FaJdu7Cx-YAUTWI_zKIFe8Rp_pBcIn7ys7S2-84bU3g"));

        String json = "[\n" +
                "        {\n" +
                "            \"id\":1651,\n" +
                "            \"chaptername\":\"新学期开学第一课【公开课】\",\n" +
                "            \"chapterprice\":\"0\",\n" +
                "            \"chaptertype\":1,\n" +
                "            \"chaptersubtitle\":\"中文素养一(上)\",\n" +
                "            \"chaptersalenumber\":0,\n" +
                "            \"chapterstatus\":1,\n" +
                "            \"playTotal\":0,\n" +
                "            \"belongFlag\":1,\n" +
                "            \"playPlan\":0\n" +
                "        }\n" +
                "    ]";
         client.index("test").addDocuments(json);
        
         //搜索全部字段匹配,自带中文分词器
         Searchable search = client.index("test").search(new SearchRequest("四个声母王子"));
        search.getHits().forEach(r->{
            System.out.println(r.get("chaptername"));
        });
    }
}