自学内容网 自学内容网

Easy es问题总结

官网教程:https://www.easy-es.cn/pages/ac41f0/#settings

一 测试项目

1 pom

    <dependencies>
        <!-- 排除springboot中内置的es依赖,以防和easy-es中的依赖冲突-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearch.client</groupId>
                    <artifactId>elasticsearch-rest-high-level-client</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.elasticsearch</groupId>
                    <artifactId>elasticsearch</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
        </dependency>

        <dependency>
            <groupId>org.dromara.easy-es</groupId>
            <artifactId>easy-es-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2 application.yml

# elasticsearch配置
easy-es:
  # 默认为true,若为false时,则认为不启用本框架
  enable: true 
  #填你的es连接地址
  address: 192.168.1.247:9200  

3 启动项:

添加扫描路径

@EsMapperScan("com.xxx.mapper_es")

4 domain_es

package com.xxx.domain_es;

import com.helka.bo.domain.gen.entity.BoSopContent;
import lombok.Data;
import org.dromara.easyes.annotation.IndexField;
import org.dromara.easyes.annotation.IndexId;
import org.dromara.easyes.annotation.IndexName;
import org.dromara.easyes.annotation.rely.Analyzer;
import org.dromara.easyes.annotation.rely.FieldType;
import org.dromara.easyes.annotation.rely.IdType;


@Data
@IndexName("bo_sop_content")
public class BoSopContentES {
    /**
     * ES的id(必须,否则会抛异常)
     */
    @IndexId(type= IdType.CUSTOMIZE)
    private String id;
    /**
     * sop的标题
     */
    @IndexField(fieldType = FieldType.TEXT, ignoreCase = true, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_SMART)
    private String sop_title;

    /**
     * sop的内容
     */
    @IndexField(fieldType = FieldType.TEXT, ignoreCase = true, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_SMART)
    private String sop_content;
}

5 Mapper

import com.xxx.domain.gen.entity.BoSopContent;
import com.xxx.domain_es.BoSopContentES;
import org.dromara.easyes.core.core.BaseEsMapper;

/**
 * @author: Tyler
 * @create: 2024-07-19
 */

public interface BoSopContentESMapper extends BaseEsMapper<BoSopContentES> {
}

6 controller

package com.xxx.controller;

import com.xxx.domain_es.BoSopContentES;
import com.xxx.mapper_es.BoSopContentESMapper;
import lombok.RequiredArgsConstructor;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.elasticsearch.index.query.Operator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: Tyler
 * @create: 2024-07-19
 */

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sopContentES")
public class BoSopContentESController {
    @Autowired
    BoSopContentESMapper sopMapper;

    /**
     * 查询索引下总数
     * @return
     */
    @GetMapping("/count")
    public Long count() {
        LambdaEsQueryWrapper<BoSopContentES> wrapper = new LambdaEsQueryWrapper<>();
        sopMapper.selectCount(wrapper);
        return sopMapper.selectCount(wrapper);
    }

    @GetMapping("/search")
    public List<BoSopContentES> search(String req) {
        LambdaEsQueryWrapper<BoSopContentES> wrapper = new LambdaEsQueryWrapper<>();
        //按照得分排序
        wrapper.multiMatchQuery(req
                , Operator.OR
                ,50
                ,BoSopContentES::getSop_title, BoSopContentES::getSop_content
            )
            .sortByScore();
        List<BoSopContentES> list1=sopMapper.selectList(wrapper);

        return list1;
    }

}

二 问题与解决

1 multiMatchQuery 查询与 kibana中不一致

这是由于minimumShouldMatch参数导致,easy-es默认为60,设置为50解决。

        //按照得分排序
        wrapper.multiMatchQuery(req
                , Operator.OR
                ,50
                ,BoSopContentES::getSop_title, BoSopContentES::getSop_content
            )
            .sortByScore();
        List<BoSopContentES> list1=sopMapper.selectList(wrapper);

2 org.dromara.easyes.common.exception.EasyEsException: no such method

es 实体创建id,es数据中无需修改

/**
     * ES的id(必须)
     */
    @IndexId(type= IdType.CUSTOMIZE)
    private String id;


原文地址:https://blog.csdn.net/hanjun0612/article/details/140603738

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!