springboot integrates MeiliSearch lightweight search engine

1. Meilisearch and Easy Search Click to enter the official website to learn more. This article mainly starts from the business of small and micro companies. Meilisearch is chosen as the full-text search engine of the project, and it can also be used as mongodb .

2. starter packaging

1. Project structure display

2. Introduce dependency packages (I use 1.2.83 for fastjson, which has unified package management, and 2.8.6 for gson)

<dependencies>
        <dependency>
            <groupId>cn.iocoder.boot</groupId>
            <artifactId>yudao-common</artifactId>
        </dependency>
        <!-- meilisearch 轻量级搜索 -->
        <!-- https://mvnrepository.com/artifact/com.meilisearch.sdk/meilisearch-java -->
        <dependency>
            <groupId>com.meilisearch.sdk</groupId>
            <artifactId>meilisearch-java</artifactId>
            <version>0.11.2</version>
        </dependency>
        <!-- meilisearch 有bug:查询时不能用gosn把string转换成LocalDateTime,只有一处使用,其他的全用gson,尝试全用fastjson 各章报错 -->
        <!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <!-- meilisearch 内部json的转换依赖,我们用fastjson会报各种报错 -->
        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
        <!-- Web 相关 -->
        <dependency>
            <groupId>cn.iocoder.boot</groupId>
            <artifactId>yudao-spring-boot-starter-web</artifactId>
            <scope>provided</scope> <!-- 设置为 provided,只有 OncePerRequestFilter 使用到 -->
        </dependency>
    </dependencies>

3. Yml parameter reading code reference

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;

/**
 * MeiliSearch 自动装配参数类
 * 2023年9月21日
 */
@ConfigurationProperties("yudao.meilisearch")
@Data
@Validated
public class MeiliSearchProperties {

    /**
     * 主机地址
     */
    private String hostUrl = "";
    /**
     * 接口访问标识
     */
    private String apiKey = "123456";

}

4. Automatic configuration code reference

import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.Config;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;


/**
 * MeiliSearch 自动装配类
 * 2023年9月21日
 */
@AutoConfiguration
@EnableConfigurationProperties({MeiliSearchProperties.class})
@EnableCaching
public class MeiliSearchAutoConfiguration {
    @Resource
    MeiliSearchProperties properties;

    @Bean
    @ConditionalOnMissingBean(Client.class)
    Client client() {
        return new Client(config());
    }

    @Bean
    @ConditionalOnMissingBean(Config.class)
    Config config() {
        return new Config(properties.getHostUrl(), properties.getApiKey());
    }

}

5. Data processing reference

import java.util.List;

/**
 * MeiliSearch json解析类
 * 2023年9月21日
 */
public class JsonHandler {
    // GsonJsonHandler有点小bug需要把代码拷贝过来改写成自己的MyGsonJsonHandler,不建议用JacksonJsonHandler会各种报错
    private com.meilisearch.sdk.json.JsonHandler jsonHandler = new MyGsonJsonHandler();

    public <T> SearchResult<T> resultDecode(String o, Class<T> clazz) {
        Object result = null;
        try {
            result = jsonHandler.decode(o, SearchResult.class, clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result == null ? null : (SearchResult<T>) result;
    }

    public <T> List<T> listDecode(Object o, Class<T> clazz) {
        Object list = null;
        try {
            list = jsonHandler.decode(o, List.class, clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list == null ? null : (List<T>) list;
    }

    public String encode(Object o) {
        try {
            return jsonHandler.encode(o);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public <T> T decode(Object o, Class<T> clazz) {
        T t = null;
        try {
            t = jsonHandler.decode(o, clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }
}

6. MyGsonJsonHandler class rewriting reference (yyyy-MM-dd'T'HH:mm:ss'Z' time format can be modified by yourself)

import com.alibaba.fastjson.JSON;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import com.meilisearch.sdk.exceptions.JsonDecodingException;
import com.meilisearch.sdk.exceptions.JsonEncodingException;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.model.Key;

import java.lang.reflect.Type;

public class MyGsonJsonHandler implements com.meilisearch.sdk.json.JsonHandler {
    private Gson gson;

    public MyGsonJsonHandler() {
        this.gson = new Gson();
    }

    public MyGsonJsonHandler(Gson gson) {
        this.gson = gson;
    }

    public String encode(Object o) throws MeilisearchException {
        if (o != null && o.getClass() == String.class) {
            return (String) o;
        } else {
            if (o != null && o.getClass() == Key.class) {
                GsonBuilder builder = new GsonBuilder();
                this.gson = builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").create();
                Key key = (Key) o;
                if (key.getExpiresAt() == null) {
                    JsonElement jsonElement = this.gson.toJsonTree(o);
                    JsonObject jsonObject = jsonElement.getAsJsonObject();
                    jsonObject.add("expiresAt", JsonNull.INSTANCE);
                    o = jsonObject;
                    this.gson = builder.serializeNulls().setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").create();
                }
            }

            try {
                return this.gson.toJson(o);
            } catch (Exception var6) {
                throw new JsonEncodingException(var6);
            }
        }
    }

    public <T> T decode(Object o, Class<?> targetClass, Class<?>... parameters) throws MeilisearchException {
        if (o == null) {
            throw new JsonDecodingException("Response to deserialize is null");
        } else if (targetClass == String.class) {
            return (T) o;
        } else {
            try {
                if (parameters != null && parameters.length != 0) {
                    TypeToken<?> parameterized = TypeToken.getParameterized(targetClass, parameters);

                    Type type = parameterized.getType();
                    String string = o.toString();
                    return JSON.parseObject(string, type);
                } else {
                    return (T) this.gson.fromJson((String) o, targetClass);
                }
            } catch (JsonSyntaxException var5) {
                throw new JsonDecodingException(var5);
            }
        }
    }
}

7. Custom annotation code reference

import java.lang.annotation.*;

/**
 * MeiliSearch
 * 2023年9月21日
 */
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MSFiled {

    /**
     * 是否开启过滤
     */
    boolean openFilter() default false;

    /**
     * 是否不展示
     */
    boolean noDisplayed() default false;

    /**
     * 是否开启排序
     */
    boolean openSort() default false;


    /**
     *  处理的字段名
     */
    String key() ;
}





import java.lang.annotation.*;

/**
 * MeiliSearch
 * 2023年9月21日
 */
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MSIndex {

    /**
     * 索引
     */
    String uid() default "";

    /**
     * 主键
     */
    String primaryKey() default "";

    /**
     * 分类最大数量
     */
    int maxValuesPerFacet() default 100;

    /**
     *  单次查询最大数量
     */
    int maxTotalHits() default 1000;
}

8. Return result analysis class reference

import com.alibaba.fastjson.annotation.JSONField;

import java.util.List;

/**
 * MeiliSearch
 * 2023年9月21日
 */
public class SearchResult<T> {

    private String query;
    private long offset;
    private long limit;
    private long processingTimeMs;
    private long nbHits;

    // 因为我是要当mongodb来使用所以加入了精分页的参数
    private long hitsPerPage;
    private long page;
    private long totalPages;
    private long totalHits;

    private boolean exhaustiveNbHits;

    private List<T> hits;

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public long getOffset() {
        return offset;
    }

    public void setOffset(long offset) {
        this.offset = offset;
    }

    public long getLimit() {
        return limit;
    }

    public void setLimit(long limit) {
        this.limit = limit;
    }

    public long getProcessingTimeMs() {
        return processingTimeMs;
    }

    public void setProcessingTimeMs(long processingTimeMs) {
        this.processingTimeMs = processingTimeMs;
    }

    public long getNbHits() {
        return nbHits;
    }

    public void setNbHits(long nbHits) {
        this.nbHits = nbHits;
    }

    public boolean isExhaustiveNbHits() {
        return exhaustiveNbHits;
    }

    public void setExhaustiveNbHits(boolean exhaustiveNbHits) {
        this.exhaustiveNbHits = exhaustiveNbHits;
    }

    public List<T> getHits() {
        return hits;
    }

    public void setHits(List<T> hits) {
        this.hits = hits;
    }

    @Override
    public String toString() {
        return "SearchResult{" +
                "query='" + query + '\'' +
                ", offset=" + offset +
                ", limit=" + limit +
                ", processingTimeMs=" + processingTimeMs +
                ", nbHits=" + nbHits +
                ", exhaustiveNbHits=" + exhaustiveNbHits +
                ", hits=" + hits +
                '}';
    }

    public long getHitsPerPage() {
        return hitsPerPage;
    }

    public void setHitsPerPage(long hitsPerPage) {
        this.hitsPerPage = hitsPerPage;
    }

    public long getPage() {
        return page;
    }

    public void setPage(long page) {
        this.page = page;
    }

    public long getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(long totalPages) {
        this.totalPages = totalPages;
    }

    public long getTotalHits() {
        return totalHits;
    }

    public void setTotalHits(long totalHits) {
        this.totalHits = totalHits;
    }
}

9. Basic operation interface encapsulation

import cn.iocoder.yudao.framework.meilisearch.json.SearchResult;
import com.meilisearch.sdk.SearchRequest;
import com.meilisearch.sdk.model.Settings;
import com.meilisearch.sdk.model.Task;
import com.meilisearch.sdk.model.TaskInfo;

import java.util.List;

/**
 * MeiliSearch 基础接口
 * 2023年9月21日
 */
interface DocumentOperations<T> {

    T get(String identifier);

    List<T> list();

    List<T> list(int limit);

    List<T> list(int offset, int limit);

    long add(T document);

    long update(T document);

    long add(List<T> documents);

    long update(List<T> documents);

    long delete(String identifier);

    long deleteBatch(String... documentsIdentifiers);

    long deleteAll();

    SearchResult<T> search(String q);

    SearchResult<T> search(String q, int offset, int limit);

    SearchResult<T> search(SearchRequest sr);

    Settings getSettings();

    TaskInfo updateSettings(Settings settings);

    TaskInfo resetSettings();

    Task getUpdate(int updateId);
}

10. Basic operation implementation

import cn.iocoder.yudao.framework.meilisearch.json.JsonHandler;
import cn.iocoder.yudao.framework.meilisearch.json.MSFiled;
import cn.iocoder.yudao.framework.meilisearch.json.MSIndex;
import cn.iocoder.yudao.framework.meilisearch.json.SearchResult;
import com.google.gson.Gson;
import com.meilisearch.sdk.*;
import com.meilisearch.sdk.model.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.*;

/**
 * MeiliSearch 基本操作实现
 * 2023年9月21日
 */
public class MeilisearchRepository<T> implements InitializingBean, DocumentOperations<T> {

    private Index index;
    private Class<T> tClass;
    private JsonHandler jsonHandler = new JsonHandler();

    @Resource
    private Client client;


    @Override
    public T get(String identifier) {
        T document;
        try {
            document = index.getDocument(identifier, tClass);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return document;
    }

    @Override
    public List<T> list() {
        List<T> documents;
        try {
            documents = Optional.ofNullable(index.getDocuments(tClass))
                    .map(indexDocument -> indexDocument.getResults())
                    .map(result -> Arrays.asList(result))
                    .orElse(new ArrayList<>());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return documents;
    }

    @Override
    public List<T> list(int limit) {
        List<T> documents;
        try {
            DocumentsQuery query = new DocumentsQuery();
            query.setLimit(limit);
            documents = Optional.ofNullable(index.getDocuments(query, tClass))
                    .map(indexDocument -> indexDocument.getResults())
                    .map(result -> Arrays.asList(result))
                    .orElse(new ArrayList<>());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return documents;
    }

    @Override
    public List<T> list(int offset, int limit) {
        List<T> documents;
        try {
            DocumentsQuery query = new DocumentsQuery();
            query.setLimit(limit);
            query.setOffset(offset);
            documents = Optional.ofNullable(index.getDocuments(query, tClass))
                    .map(indexDocument -> indexDocument.getResults())
                    .map(result -> Arrays.asList(result))
                    .orElse(new ArrayList<>());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return documents;
    }

    @Override
    public long add(T document) {
        List<T> list = Collections.singletonList(document);
        return add(list);
    }

    @Override
    public long update(T document) {
        List<T> list = Collections.singletonList(document);
        return update(list);
    }

    @Override
    public long add(List documents) {
        int taskId;
        try {
            taskId = index.addDocuments(new Gson().toJson(documents)).getTaskUid();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return taskId;
    }

    @Override
    public long update(List documents) {
        int updates;
        try {
            updates = index.updateDocuments(new Gson().toJson(documents)).getTaskUid();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return updates;
    }


    @Override
    public long delete(String identifier) {
        int taskId;
        try {
            taskId = index.deleteDocument(identifier).getTaskUid();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return taskId;
    }

    @Override
    public long deleteBatch(String... documentsIdentifiers) {
        int taskId;
        try {
            taskId = index.deleteDocuments(Arrays.asList(documentsIdentifiers)).getTaskUid();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return taskId;
    }

    @Override
    public long deleteAll() {
        int taskId;
        try {
            taskId = index.deleteAllDocuments().getTaskUid();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return taskId;
    }


    @Override
    public cn.iocoder.yudao.framework.meilisearch.json.SearchResult<T> search(String q) {
        String result;
        try {
            result = new Gson().toJson(index.search(q));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return jsonHandler.resultDecode(result, tClass);
    }

    @Override
    public cn.iocoder.yudao.framework.meilisearch.json.SearchResult<T> search(String q, int offset, int limit) {
        SearchRequest searchRequest = SearchRequest.builder()
                .q(q)
                .offset(offset)
                .limit(limit)
                .build();
        return search(searchRequest);
    }

//    @Override
    public cn.iocoder.yudao.framework.meilisearch.json.SearchResult<T> searchPage(String q) {
        SearchRequest searchRequest = SearchRequest.builder()
                .q(q)
                .build();
        return search(searchRequest);
    }

    @Override
    public SearchResult<T> search(SearchRequest sr) {
        String result;
        try {
            result = new Gson().toJson(index.search(sr));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return jsonHandler.resultDecode(result, tClass);
    }

    @Override
    public Settings getSettings() {
        try {
            return index.getSettings();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public TaskInfo updateSettings(Settings settings) {
        try {
            return index.updateSettings(settings);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public TaskInfo resetSettings() {
        try {
            return index.resetSettings();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Task getUpdate(int updateId) {
        try {
            return index.getTask(updateId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        initIndex();
    }

    public Index getIndex() {
        return index;
    }

    /**
     * 初始化索引信息
     *
     * @throws Exception
     */
    private void initIndex() throws Exception {
        Class<? extends MeilisearchRepository> clazz = getClass();
        tClass = (Class<T>) ((ParameterizedType) clazz.getGenericSuperclass()).getActualTypeArguments()[0];
        MSIndex annoIndex = tClass.getAnnotation(MSIndex.class);
        String uid = annoIndex.uid();
        String primaryKey = annoIndex.primaryKey();
        if (StringUtils.isEmpty(uid)) {
            uid = tClass.getSimpleName().toLowerCase();
        }
        if (StringUtils.isEmpty(primaryKey)) {
            primaryKey = "id";
        }
        int maxTotalHit=1000;
        int maxValuesPerFacet=100;
        if (Objects.nonNull(annoIndex.maxTotalHits())){
            maxTotalHit=annoIndex.maxTotalHits();
        }
        if (Objects.nonNull(annoIndex.maxValuesPerFacet())){
            maxValuesPerFacet=100;
        }

        List<String> filterKey = new ArrayList<>();
        List<String> sortKey = new ArrayList<>();
        List<String> noDisPlay = new ArrayList<>();
        //获取类所有属性
        for (Field field : tClass.getDeclaredFields()) {
            //判断是否存在这个注解
            if (field.isAnnotationPresent(MSFiled.class)) {
                MSFiled annotation = field.getAnnotation(MSFiled.class);
                if (annotation.openFilter()) {
                    filterKey.add(annotation.key());
                }

                if (annotation.openSort()) {
                    sortKey.add(annotation.key());
                }
                if (annotation.noDisplayed()) {
                    noDisPlay.add(annotation.key());
                }
            }
        }
        Results<Index> indexes = client.getIndexes();
        Index[] results = indexes.getResults();
        Boolean isHaveIndex=false;
        for (Index result : results) {
            if (uid.equals(result.getUid())){
                isHaveIndex=true;
                break;
            }
        }

        if (isHaveIndex){
            client.updateIndex(uid,primaryKey);

            this.index = client.getIndex(uid);
            Settings settings = new Settings();
            settings.setDisplayedAttributes(noDisPlay.size()>0?noDisPlay.toArray(new String[noDisPlay.size()]):new String[]{"*"});
            settings.setFilterableAttributes(filterKey.toArray(new String[filterKey.size()]));
            settings.setSortableAttributes(sortKey.toArray(new String[sortKey.size()]));
            index.updateSettings(settings);
        }else {
            client.createIndex(uid, primaryKey);
        }
    }
}

11. Specify the location of the automatic configuration class

12. The project has version management under unified version management settings.

2. Project references

1. Introduce starter dependencies (if there is no unified version management, version must be added)

2. Basic use

2.1. Create index (wide table)

import cn.iocoder.yudao.framework.meilisearch.json.MSFiled;
import cn.iocoder.yudao.framework.meilisearch.json.MSIndex;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@MSIndex(uid = "com_baidu_main", primaryKey = "id")
public class MainDO {
    private Long id;
    private String seedsName;
    @MSFiled(openFilter = true, key = "isDelete")
    private Integer isDelete;
    @MSFiled(openFilter = true, key = "status")
    private Integer status;

    @MSFiled(openFilter = true, key = "classFiledId")
    private Integer classFiledId;
    private String classFiledName;
    @MSFiled(openFilter = true, key = "tags")
    private List<TageInfo> tags;
}

2.2. Integrate the mapper in the starter to perform basic operations on milisearch

import cn.iocoder.yudao.framework.meilisearch.core.MeilisearchRepository;
import org.springframework.stereotype.Repository;

@Repository
public class MeiliSearchMapper extends MeilisearchRepository<MainDO> {
}

2.3. When mongodb implements precise paging query

        // 条件组装,实体类根据业务需要提前加好业务字段索引注解@MSFiled,显示隐藏 索引等
        StringBuffer sb = new StringBuffer();
        if (ObjectUtil.isNotEmpty(pageParam.getStartTime())) {
            sb.append("createTime>=").append(pageParam.getStartTime()).append(" AND ");
        }
        if (ObjectUtil.isNotEmpty(pageParam.getEndTime())) {
            sb.append("createTime<=").append(pageParam.getEndTime()).append(" AND ");
        }
        sb.append("userId=" + SecurityFrameworkUtils.getLoginUserId());

        // 分页查询及排序
        SearchRequest searchRequest4 = SearchRequest.builder()
                .sort(new String[]{"createTime:desc"})
                .page(pageParam.getPageNo())
                .hitsPerPage(pageParam.getPageSize())
                .filter(new String[]{sb.toString()}).build();
        SearchResult<SeedsDO> search = meiliSearchMapper.search(searchRequest4);
        return SeedCultivateConvert.INSTANCE.convert(search);






        // SeedCultivateConvert.INSTANCE.convert是个类转化器可手动转换成分页的统一数据格式
        pageResult.setList(search.getHits());
        pageResult.setTotal(search.getTotalPages());
        .......


2.4. Other basic uses

@Resource
private MeiliSearchMapper meiliSearchMapper;

//根据标签分页查询
SearchRequest searchRequest4 = SearchRequest.builder()
                .limit(pageParam.getPageSize().intValue())
                .sort(new String[]{"createTime:desc"})
                .offset(pageParam.getPageNo().intValue() == 0 ? pageParam.getPageNo().intValue() : (pageParam.getPageNo().intValue() - 1) * pageParam.getPageSize().intValue())
                .filter(new String[]{"tags.id=" + "10010" + " AND status=1 AND isDelete=0"}).build();
SearchResult<MainDO> search4 = meiliSearchMapper.search(searchRequest4);

//保存Or编辑
List<SeedsDO> articleCardDTOS = new ArrayList<>();
Boolean aBoolean = meiliSearchMapper.add(articleCardDTOS) > 0 ? Boolean.TRUE : Boolean.FALSE;
//按id删除
meiliSearchMapper.delete(String.valueOf(10085));

//根据类目分页查询
SearchRequest searchRequest3 = SearchRequest.builder()
                .limit(pageParam.getPageSize().intValue())
                .offset(pageParam.getPageNo().intValue() == 0 ? pageParam.getPageNo().intValue() : (pageParam.getPageNo().intValue() - 1) * pageParam.getPageSize().intValue())
                .build();
StringBuffer sb1 = new StringBuffer();
sb.append("status =1 AND isDelete=0").append(" AND ").append("categoryId =").append(10086L);
searchRequest.setFilter(new String[]{sb.toString()});
searchRequest.setSort(new String[]{"createTime:desc"});
SearchResult<SeedsDO> search3 = meiliSearchMapper.search(searchRequest3);

Guess you like

Origin blog.csdn.net/qq_29653373/article/details/133179166