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