以前使用的是mysql的全文索引、最开始还行。后续觉得就不好用了,但是服务器资源有限,没法上ES,只好找一个轻量级的搜索引擎、找了半天,决定使用这一个,目前效果还不错的。
参考网址
官网:https://www.meilisearch.com/docs/reference/api/overview
java第三方sdk:https://github.com/meilisearch/meilisearch-java/issues
0、docker安装 很快的
略
安装好你可以通过 host:7700打开web页面
1、客户端的创建
java
package xxx.x.x;
import cn.hutool.core.util.ObjectUtil;
import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.Config;
import com.meilisearch.sdk.json.JacksonJsonHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class MeilisearchConfig {
@Value("${meilisearch.url:http://127.0.01:7700}")
private String meilisearchUrl;
@Value("${meilisearch.masterKey:}")
private String masterKey = "masterKey";
private volatile Client client;
/**
* 获取 MeiliSearch 客户端
*
* @return
*/
public Client getClient() {
if (ObjectUtil.isNull(client)) {
synchronized (this) {
if (ObjectUtil.isNull(client)) {
client = buildClient();
}
}
}
return client;
}
/**
* 构建 MeiliSearch 客户端
*
* @return
*/
public Client buildClient() {
Config config = new Config(meilisearchUrl, masterKey, new JacksonJsonHandler());
client = new Client(config);
log.info("MeiliSearch Client 初始化完成");
return client;
}
}
2、简单工具类
java
package xxx.util;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.model.TaskInfo;
import top.shareus.bot.common.eumn.meilisearch.MeilisearchIndexEnums;
import java.util.List;
public class MeilisearchUtil {
private final static Log log = LogFactory.getCurrentLogFactory().getLog(MeilisearchUtil.class);
/**
* 添加文档
*
* @param meilisearchClient
* @param indexEnums
* @param objList
* @param <T>
*/
public static <T> void addDocuments(Client meilisearchClient, MeilisearchIndexEnums indexEnums, List<T> objList) {
if (ObjectUtil.hasNull(meilisearchClient, indexEnums) || CollUtil.isEmpty(objList)) {
return;
}
log.info("-----------------------------------------------------");
JSONArray array = new JSONArray();
List<JSONObject> objectList = objList.stream().map(JSONObject::new).toList();
log.info("待添加文档数量: {}", objectList.size());
array.put(objectList);
String documents = array.getJSONArray(0).toString();
Index index = meilisearchClient.index(indexEnums.getIndex());
String primaryKey = indexEnums.getPrimaryKey();
TaskInfo taskInfo;
if (StrUtil.isBlank(primaryKey)) {
taskInfo = index.addDocuments(documents);
} else {
taskInfo = index.addDocuments(documents, primaryKey);
}
log.info("添加文档任务ID: {}", taskInfo.getTaskUid());
log.info("添加文档任务状态: {}", taskInfo.getStatus());
log.info("添加文档任务信息:{}", JSONUtil.toJsonStr(taskInfo));
log.info("添加文档任务结果:{}", JSONUtil.toJsonStr(index.getTask(taskInfo.getTaskUid())));
log.info("-----------------------------------------------------");
}
/**
* 删除索引
*
* @param meilisearchClient
* @param indexEnums
*/
public static void delIndex(Client meilisearchClient, MeilisearchIndexEnums indexEnums) {
if (ObjectUtil.hasNull(meilisearchClient, indexEnums)) {
return;
}
meilisearchClient.deleteIndex(indexEnums.getIndex());
}
}
3、练手的单元测试、索引创建、简单查询
主要用户对索引的创建、及简单的查询
java
Client meilisearchClient = meilisearchConfig.getClient();
Index index = meilisearchClient.index(MeilisearchIndexEnums.ARCHIVED_FILE.getIndex());
meilisearchClient.deleteIndex(MeilisearchIndexEnums.ARCHIVED_FILE.getIndex());
// 这里记得 每次创建索引 需要重新把你的一些筛选字段和排序字段重新添加一下
TaskInfo taskInfo = index.updateFilterableAttributesSettings(new String[]{"name", "delFlag", "enabled", "archiveDate"});
taskInfo = index.updateSortableAttributesSettings(new String[]{"archiveDate"});
List<ArchivedFile> archivedFileList = archivedFileService.list();
MeilisearchUtil.addDocuments(meilisearchClient, MeilisearchIndexEnums.ARCHIVED_FILE, archivedFileList);
SearchRequest searchRequest = SearchRequest.builder()
.limit(10)
.page(1)
.hitsPerPage(10)
.sort(new String[]{"archiveDate:desc"})
.showRankingScore(true)
.attributesToSearchOn(new String[]{"name"})
.matchingStrategy(MatchingStrategy.FREQUENCY)
.rankingScoreThreshold(0.2)
.filterArray(new String[][]{new String[]{"delFlag = 0", "enabled = 0"}})
.q("笑对")
.build();
Searchable searchable = index.search(searchRequest);
ArrayList<HashMap<String, Object>> searchableHits = searchable.getHits();
List<ArchivedFile> copyToList = BeanUtil.copyToList(searchableHits, ArchivedFile.class);
4、设置index的区域
官方说这样会对中文更好一点,虽然不设置他也会自己检测 但是难免跑偏
PUT修改 GET查看
url: http://host:7700/indexes/**{index}** /settings/localized-attributes
body
json
[
{
"locales": [
"cmn"
],
"attributePatterns": [
"*"
]
}
]