springboot整合MeiliSearch轻量级搜索引擎

这篇具有很好参考价值的文章主要介绍了springboot整合MeiliSearch轻量级搜索引擎。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、Meilisearch与Easy Search点击进入官网了解,本文主要从小微型公司业务出发,选择meilisearch来作为项目的全文搜索引擎,还可以当成来mongodb来使用。

二、starter封装

1、项目结构展示

springboot整合MeiliSearch轻量级搜索引擎,spring boot,搜索引擎,elasticsearch

2、引入依赖包(我是有包统一管理的fastjson用的1.2.83,gson用的2.8.6)

<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>
        <!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <!-- 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参数读取代码参考

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;

}

4、自动配置类代码参考

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、数据处理类参考

import java.util.List;

/**
 * MeiliSearch json解析类
 * 2023年9月21日
 */
public class JsonHandler {

    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类改写基本没啥用(meilisearch通过官方sdk到springboot的雪花id会丢失精度,我是加了冗余字段把雪花id存进去当主键用的。还有就是LocalDateTime不用大于小于范围查询,我是把要范围查询的时间字段冗余成long类型),有对LocalDateTime和雪花id Long存储有更好的解决方案的欢迎留言。

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;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

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

    public MyGsonJsonHandler() {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json, typeOfT, context) ->
                LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ISO_DATE_TIME));
//        gsonBuilder.registerTypeAdapter(Long.class, (JsonDeserializer<Long>) (json, typeOfT, context) -> {
//            Long parse = Long.parseLong(json.getAsJsonPrimitive().getAsString());
//            雪花id科学计数后转支付串会丢失进度小1啥的
//            return parse;
//        });
        this.gson = gsonBuilder.create();
    }

    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) {
                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;
                }
            }
            try {
                return JSON.toJSONString(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 json = this.gson.toJson(o);
                    json = json.replace("\\", "");
                    //去除json首位字符串
                    json = json.substring(1, json.length() - 1);
                    String string = o.toString();
                    return this.gson.fromJson(string, type);
//                    return  JSON.parseObject(string, type);
                } else {
                    return (T) JSON.parseObject((String) o, targetClass);
                }
            } catch (JsonSyntaxException var5) {
                throw new JsonDecodingException(var5);
            }
        }
    }
}

7、自定义注解代码参考

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、返回结果解析类参考

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;


    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、基础操作接口封装

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);

    String select(SearchRequest sr);

    Settings getSettings();

    TaskInfo updateSettings(Settings settings);

    TaskInfo resetSettings();

    Task getUpdate(int updateId);

//    UpdateStatus updateSettings(Settings settings);
//
//    UpdateStatus resetSettings();
//
//    UpdateStatus getUpdate(int updateId);
//
//    UpdateStatus[] getUpdates();
}

10、基本操作实现

import cn.hutool.core.util.ObjectUtil;
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.alibaba.fastjson.JSON;
import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.SearchRequest;
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 = getIndex().getDocument(identifier, tClass);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return document;
    }

    @Override
    public List<T> list() {
        List<T> documents;
        try {
            documents = Optional.ofNullable(getIndex().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(getIndex().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) {
        try {
            if (ObjectUtil.isNotNull(documents)) {
                String jsonString = JSON.toJSONString(documents);
                if (ObjectUtil.isNotNull(jsonString)) {
                    TaskInfo taskInfo = getIndex().addDocuments(jsonString);
                    if (ObjectUtil.isNotNull(taskInfo)) {
                        return taskInfo.getTaskUid();
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(documents.toString(), e);
        }
        return 0;
    }


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


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

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

    @Override
    public long deleteAll() {
        int taskId;
        try {
            taskId = getIndex().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 = JSON.toJSONString(getIndex().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 = "";
            if (ObjectUtil.isNotNull(sr)) {
                if (ObjectUtil.isNull(getIndex())) {
                    initIndex();
                }
                Searchable search = getIndex().search(sr);
                String jsonString = JSON.toJSONString(search);
                result = jsonString;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return jsonHandler.resultDecode(result, tClass);
    }


    @Override
    public String select(SearchRequest sr) {
        try {
            if (ObjectUtil.isNotNull(sr)) {
                if (ObjectUtil.isNull(getIndex())) {
                    initIndex();
                }
                Searchable search = getIndex().search(sr);
                String jsonString = JSON.toJSONString(search);
                return jsonString;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }

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

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

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

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

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

    public Index getIndex() {
        if (ObjectUtil.isNull(index)) {
            try {
                initIndex();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        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);
        } else {
            client.createIndex(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);
    }

}


11、指定自动配置类所在

springboot整合MeiliSearch轻量级搜索引擎,spring boot,搜索引擎,elasticsearch

12、项目有统一版本管理的设置下版本管理

springboot整合MeiliSearch轻量级搜索引擎,spring boot,搜索引擎,elasticsearch

二、项目引用

1、引入starter依赖(没有版本统一管理的要把version加上)

springboot整合MeiliSearch轻量级搜索引擎,spring boot,搜索引擎,elasticsearch

2、基本使用

2.1、建立索引(宽表)

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 = "idToString")
public class MainDO {
    private Long id;
    @MSFiled(openFilter = true, key = "idToString", openSort = true)
    private idToString;
    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;

    @MSFiled(openFilter = true,key = "createTime",openSort = true)
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'")
    private LocalDateTime createTime;
    @MSFiled(openFilter = true,key = "createTimeToLong",openSort = true)
    private LocalDateTime createTimeToLong;
}

2.2、集成starter里边的mapper对milisearch进行基本操作

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

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

2.3、当mongodb实现精准分页查询

        // 条件组装,实体类根据业务需要提前加好业务字段索引注解@MSFiled,显示隐藏 索引等
        StringBuffer sb = new StringBuffer();
        if (ObjectUtil.isNotEmpty(pageParam.getStartTime())) {
            sb.append("createTimeToLong>=").append(pageParam.getStartTime()).append(" AND ");
        }
        if (ObjectUtil.isNotEmpty(pageParam.getEndTime())) {
            sb.append("createTimeToLong<=").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、其他基本使用文章来源地址https://www.toymoban.com/news/detail-714466.html

@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);

到了这里,关于springboot整合MeiliSearch轻量级搜索引擎的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring Boot整合Postgres实现轻量级全文搜索

    有这样一个带有搜索功能的用户界面需求: 搜索流程如下所示: 这个需求涉及两个实体: “评分(Rating)、用户名(Username)”数据与 User 实体相关 “创建日期(create date)、观看次数(number of views)、标题(title)、正文(body)”与 Story 实体相关 需要支持的功能对 User

    2024年02月19日
    浏览(29)
  • Springboot集成轻量级内存数据库H2

    最近做一个小项目,需要存储的数据不多,用mysql太重了,用其他的Redis之类的也不太方便,然后就想到了H2,他就是一个jar包,可以和项目一起打包发布,非常适合数据量不多的微小系统,下面大概介绍下H2的基本知识和Springboot的集成 H2是一个用Java开发的嵌入式数据库,它本

    2024年02月07日
    浏览(37)
  • SimSearch:一个轻量级的springboot项目索引构建工具,实现快速模糊搜索

    大部分项目都会涉及模糊搜索功能,而实现模糊搜索一般分为两个派系: like简约派系 搜索引擎派系 对于较为大型的项目来说,使用Solr、ES或者Milvus之类的引擎是比较流行的选择了(效果只能说优秀),而对于中小型项目,如果考虑这些较为重型的引擎,就意味着开发成本和

    2024年02月02日
    浏览(77)
  • 告别if else!试试这款轻量级流程引擎吧,跟SpringBoot绝配!

    之前同事用了一款轻量级的规则引擎脚本 AviatorScript ,我也跟着用了起来,真的挺香,能少写很多代码。这期就给大家介绍一下这款规则引擎。 AviatorScript 是一门高性能、轻量级寄宿于 JVM (包括 Android 平台)之上的脚本语言。 它起源于2010年,作者对当时已有的一些产品不是

    2024年02月13日
    浏览(34)
  • git轻量级服务器gogs、gitea,非轻量级gitbucket

    本文来源:git轻量级服务器gogs、gitea,非轻量级gitbucket, 或 gitcode/gogs,gitea.md 结论: gogs、gitea很相似 确实轻, gitbucket基于java 不轻, 这三者都不支持组织树(嵌套组织 nested group) 只能一层组织。 个人用,基于gogs、gitea,两层结构树 简易办法: 把用户当成第一层节点、该用户的

    2024年02月07日
    浏览(52)
  • 轻量灵动: 革新轻量级服务开发

    从 JDK 8 升级到 JDK 17 可以让你的应用程序受益于新的功能、性能改进和安全增强。下面是一些 JDK 8 升级到 JDK 17 的最佳实战: 1.1、确定升级的必要性:首先,你需要评估你的应用程序是否需要升级到 JDK 17。查看 JDK 17 的新特性、改进和修复的 bug,以确定它们对你的应用程序

    2024年02月07日
    浏览(35)
  • 轻量级 HTTP 请求组件

    Apache HttpClient 是著名的 HTTP 客户端请求工具——现在我们模拟它打造一套简单小巧的请求工具库, 封装 Java 类库里面的 HttpURLConnection 对象来完成日常的 HTTP 请求,诸如 GET、HEAD、POST 等等,并尝试应用 Java 8 函数式风格来制定 API。 组件源码在:https://gitee.com/sp42_admin/ajaxjs/tr

    2024年02月01日
    浏览(48)
  • 一种轻量级定时任务实现

    现在市面上有各式各样的分布式定时任务,每个都有其独特的特点,我们这边的项目因为一开始使用的是分布式开源调度框架TBSchedule,但是这个框架依赖ZK, 由于ZK的不稳定性和项目老旧无人维护 ,导致我们的定时任务会偶发出现异常,比如:任务停止、任务项丢失、任务不

    2024年02月14日
    浏览(27)
  • Tomcat轻量级服务器

    目录 1.常见系统架构  C-S架构 B-S架构 2.B-S架构系统的通信步骤 3.常见WEB服服务器软件 4.Tomcat服务器的配置 下载安装 环境变量配置 测试环境变量是否配置成功 测试Tomcat服务器是否配置成功  Tomcat窗口一闪而过的解决步骤 Tomcat解决乱码 介绍: C-S架构即Client/Server(客户端/服务

    2023年04月14日
    浏览(115)
  • Kotlin 轻量级Android开发

    Kotlin 是一门运行在 JVM 之上的语言。 它由 Jetbrains 创建,而 Jetbrains 则是诸多强大的工具(如知名的 Java IDE IntelliJ IDEA )背后的公司。 Kotlin 是一门非常简单的语言,其主要目标之一就是提供强大语言的同时又保持简单且精简的语法。 其主要特性如下所示: 轻量级:这一点对

    2024年02月07日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包