代碼位址:https://github.com/zhaoyunxing92/spring-boot-learn-box/tree/master/spring-boot-elasticsearch/spring-boot-data-elasticsearch
這篇主要是對上篇elasticsearch入門到放棄之springboot elasticsearch x-pack的補充,下面我将按照場景結合官方編寫接口,同樣我也是開啟了
x-pack
安全認證
系列文章
- elasticsearch入門到放棄之docker搭建 es環境搭建
- elasticsearch入門到放棄之x-pack安全認證 x-pack保駕護航你的es
- elasticsearch入門到放棄之elasticsearch-head es-head可視化你的es
- elasticsearch入門到放棄之elasticsearch-in-java 這個很關鍵,看完這個,再看這個就很容易了解了
- elasticsearch入門到放棄之springboot elasticsearch x-pack spring boot跟elasticsearch整合
參考文檔
- https://docs.spring.io/spring-data/elasticsearch/docs/2.1.22.RELEASE/reference/html/ 官方的文檔也是很棒的
- https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html text字段排序問題
環境資訊、配置
- jdk 1.8
- elasticsearch 6.4.0
- x-pack 6.4.0
- spring-boot-starter-data-elasticsearch 6.4.0
- spring-boot 2.1.0 (它預設帶的elasticsearch是6.2.2的)
pom.xml檔案
<!--修改es版本這樣設定最簡單-->
<properties>
<elasticsearch.version>6.4.0</elasticsearch.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>x-pack-transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<!--這個好像要添加不然下載下傳不到 spring-boot-starter-data-elasticsearch:6.4.0 -->
<repository>
<id>spring-libs-snapshot</id>
<name>Spring Snapshot Repository</name>
<url>https://repo.spring.io/libs-snapshot</url>
</repository>
application.yml配置
spring:
data:
elasticsearch:
repositories:
enabled: true
cluster-nodes: 127.0.0.1:9300 # 叢集模式下用逗号分開
cluster-name: elasticsearch
properties:
xpack.security.user: elastic:123456
使用場景
根據名稱擷取文章
注意:如果傳回值是對象則參數必須添加
Page
Pageable
// 帶分頁資訊
Page<Article> articles = articleService.findArticleByName("docker搭建", PageRequest.of(0, 5));
// 隻關心資料本身(使用場景還沒有想到)
List<Article> articles= articleService.findArticleByName(String name);
根據名稱擷取文章,然後根據id和建立時間倒序
注意:排序的時候不要用在字段類型是上,具體原因看
text
第一個
可能遇到的問題
// 按照`org.springframework.data.domain.Sort`來
Page<Article> articles = articleService.findArticleByName("docker搭建", PageRequest.of(0, 5,Sort.by(Sort.Direction.DESC,"id","createTime")));
// 按照命名規則來
Page<Article> articles = articleService.findArticleByNameOrderByCreateTimeDesc("docker搭建", PageRequest.of(0, 5));
統計文章名稱出現次數
使用場景好像沒有,了解即可
删除文章
有很多方法,隻寫使用頻率高的
//無傳回值删除
articleService.deleteById("40");
// 根據對象删除
articleService.delete(new Article());
//有傳回值删除,隻能針對主鍵
List<Article> articles = articleService.deleteArticlesById("40");
其他未驗證的感覺沒有使用場景的
- 根據
或top
限制first
- 使用注解
的@Query
可能遇到的問題
-
java.lang.IllegalArgumentException: Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.
這個問題是因為
Sort
的字段
name
是
text
類型的,按照官方fieldata的建議,它禁止這樣操作,容易出現記憶體過大
// fielddata = true 可以解決這個問題,但是可能帶來oom問題
@Field(type = FieldType.Text,fielddata = true,store = true, analyzer = "ik_smart", searchAnalyzer = "ik_max_word")
private String name;
最後
如果你想了解更多的文章可以微信搜尋
zhaoyx92
,或者掃碼關注.别抱有太高期望,更新很慢的
