spring-boot 2.0.2
spring-data-elasticsearch 3.0.7
elasticsearch 5.6.9html
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> </dependencies> </project>
spring: data: elasticsearch: #cluster-name: #默认为elasticsearch cluster-nodes: 127.0.0.1:9300 #配置es节点信息,逗号分隔,若是没有指定,则启动ClientNode(9200端口是http查询使用的。9300集群使用。这里使用9300.) properties: path: logs: ./elasticsearch/log #elasticsearch日志存储目录 data: ./elasticsearch/data #elasticsearch数据存储目录
官方文档:https://docs.spring.io/spring...
中文翻译:https://www.jianshu.com/p/27e...
入门参考:https://www.cnblogs.com/guozp...java
@Document注解里面的几个属性,类比mysql的话是这样:node
index –> DB type –> Table Document –> row
加上@Id注解后,在Elasticsearch里对应的该列就是主键了,在查询时就能够直接用主键查询。其实和mysql很是相似,基本就是一个数据库。mysql
@Persistent @Inherited @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) public @interface Document { String indexName();//索引库的名称,我的建议以项目的名称命名 String type() default "";//类型,我的建议以实体的名称命名 short shards() default 5;//默认分区数 short replicas() default 1;//每一个分区默认的备份数 String refreshInterval() default "1s";//刷新间隔 String indexStoreType() default "fs";//索引文件存储类型 }
加上了@Document注解以后,默认状况下这个实体中全部的属性都会被创建索引、而且分词。
经过@Field注解来进行详细的指定,若是没有特殊需求,那么只须要添加@Document便可。spring
@Field注解的定义以下: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @Documented @Inherited public @interface Field { FieldType type() default FieldType.Auto;#自动检测属性的类型 FieldIndex index() default FieldIndex.analyzed;#默认状况下分词 DateFormat format() default DateFormat.none; String pattern() default ""; boolean store() default false;#默认状况下不存储原文 String searchAnalyzer() default "";#指定字段搜索时使用的分词器 String indexAnalyzer() default "";#指定字段创建索引时指定的分词器 String[] ignoreFields() default {};#若是某个字段须要被忽略 boolean includeInParent() default false; }
//不须要加@Component,直接能够@Autowared public interface ArticleSearchRepository extends ElasticsearchRepository<Article, Long> { List<Country> findByName(String name); //使用 Page<Country> countrys = articleSearchRepository.findByName("测试", PageRequest.of(0, 10)); //分页是从0开始的 Page<Country> findByName(String name, Pageable pageable); Country findProductById(String name); }
Page的方法:sql
Country.java数据库
@Document(indexName = "world", type = "country") public class Country implements Serializable { @Id private Integer id; @Field(searchAnalyzer = "ik_max_word",analyzer = "ik_smart") private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } }
CountrySearchRepository.javaapache
public interface CountrySearchRepository extends ElasticsearchRepository<Country, Long> { List<Country> findCountryByName(String name); //使用 Page<Country> countrys = countrySearchRepository.findByName("测试", PageRequest.of(0, 10)); //分页是从0开始的 Page<Country> findCountryByName(String name, Pageable pageable); Country findCountryById(String name); }
SearchService.javaapp
public class SearchService{ @Autowared CountrySearchRepository countrySearchRepository; public Page<Country> getCountryByName(String name) { Page<Country> countrys = countrySearchRepository.findCountryByName("测试", PageRequest.of(0, 10)); return countrys; } }