注意:适用于springboot或者springcloud框架
1.首先下载相关文件
2.然后需要去启动相关的启动文件
3、导入相关jar包(如果有相关的依赖包不需要导入)以及配置配置文件,并且写一个dao接口继承一个类,在启动类上标注地址
org.projectlombok
lombok
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-elasticsearch
## ElasticSearch - start
#开启 Elasticsearch 仓库(默认值:true)
spring.data.elasticsearch.repositories.enabled=true
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.data.elasticsearch.cluster-name=myes
Shop:是下面创建的实体类名称(不能写错),String(传参时的类型,我这里id也给的String,因为integer报错)
import com.jk.user.model.Shop;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface EsDao extends ElasticsearchRepository {
}
启动类上加上注解,后面跟的是dao的包名
@EnableElasticsearchRepositories(basePackages = "com.jk.web.dao")
4.实体类
indexName相当于数据库名, type 相当于表名 ,必须加上id,type 类型,analyzer 分词器名称(ik分词)
@Document(indexName = "zth",type = "t_shangpin")
public class Shop implements Serializable {
private static final long serialVersionUID = 2006762641515872124L;
private String id;
@Field(type = FieldType.Text, analyzer = "ik_max_word")