ElasticSearch
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>配置
application.yml
spring:
  elasticsearch:
    uris: http://116.xx.xx.29:9200
    username: elastic
    password: 123456测试连通
public class EsCollectionRepositoryTest extends CollectionBaseTest {
    @Autowired
    private ElasticsearchOperations elasticsearchOperations;
    @Test
    public void test() {
        IndexOperations indexOperations = elasticsearchOperations.indexOps(Collection.class);
        Assert.assertEquals("nfturbo_collection", indexOperations.getIndexCoordinates().getIndexName());
    }
}Easy-ES
依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.17.20</version>
</dependency>
<dependency>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es-boot-starter</artifactId>
<version>2.0.0-beta8</version>
</dependency>配置
easy-es:
  enable: true
  address : 116.xx.xx.29:9200
  username: elastic
  password: 123456Mapper定义并指定扫描
继承BaseEsMapper
public interface UserEsMapper extends BaseEsMapper
<User> {
}增加ES 的 Mapper 扫描配置
@EsMapperScan("com.xxx.xxx.es.mapper")指定索引
在 实体类 中,指定和 ES 交互的时候的索引的 key:
@Getter
@Setter
@IndexName(value = "key_name")
public class User extends BaseEntity {
    @IndexId(type = IdType.CUSTOMIZE)
    private Long id;
    @IndexField(fieldType = FieldType.KEYWORD)
    private String name;
    @IndexField(fieldType = FieldType.KEYWORD)
    private String state;
}使用easy-es
@Autowired
private UserEsMapper userEsMapper;
@Test
public void testFindByNameAndStateWithEasyEs(){
    LambdaEsQueryWrapper
<User> queryWrapper = new LambdaEsQueryWrapper<>();
    queryWrapper.match(User::getName, "测试")
            .and(wrapper -> wrapper
                    .match(User::getState, "SUCCEED")
            .orderByAsc("id");
    EsPageInfo
<User> results = userEsMapper.pageQuery(queryWrapper, 1, 1);
    System.out.println(JSON.toJSONString(results));
}@Service
public class EsProductServiceImpl implements EsProductService {
    @Autowired
    private EsProductMapper esProductMapper;
    @Override
    public PageInfo
<EsProduct> search(String keyword, Integer pageNum, Integer pageSize) {
        LambdaEsQueryWrapper
<EsProduct> wrapper = new LambdaEsQueryWrapper<>();
        if(StrUtil.isEmpty(keyword)){
            wrapper.matchAllQuery();
        }else{
            wrapper.multiMatchQuery(keyword,EsProduct::getName,EsProduct::getSubTitle,EsProduct::getKeywords);
        }
        return esProductMapper.pageQuery(wrapper, pageNum, pageSize);
    }
}