這篇主要是對(duì)上篇elasticsearch入門到放棄之springboot elasticsearch x-pack的補(bǔ)充,下面我將按照?qǐng)鼍敖Y(jié)合官方編寫接口,同樣我也是開啟了x-pack安全認(rèn)證
系列文章
- elasticsearch入門到放棄之docker搭建 es環(huán)境搭建
- elasticsearch入門到放棄之x-pack安全認(rèn)證 x-pack保駕護(hù)航你的es
- elasticsearch入門到放棄之elasticsearch-head es-head可視化你的es
- elasticsearch入門到放棄之elasticsearch-in-java 這個(gè)很關(guān)鍵,看完這個(gè),再看這個(gè)就很容易理解了
- 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字段排序問題
環(huán)境信息、配置
- 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 (它默認(rèn)帶的elasticsearch是6.2.2的)
pom.xml文件
<!--修改es版本這樣設(shè)置最簡(jiǎn)單-->
<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>
<!--這個(gè)好像要添加不然下載不到 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 # 集群模式下用逗號(hào)分開
cluster-name: elasticsearch
properties:
xpack.security.user: elastic:123456
使用場(chǎng)景
根據(jù)名稱獲取文章
注意:如果返回值是
Page對(duì)象則參數(shù)必須添加Pageable
// 帶分頁(yè)信息
Page<Article> articles = articleService.findArticleByName("docker搭建", PageRequest.of(0, 5));
// 只關(guān)心數(shù)據(jù)本身(使用場(chǎng)景還沒有想到)
List<Article> articles= articleService.findArticleByName(String name);
根據(jù)名稱獲取文章,然后根據(jù)id和創(chuàng)建時(shí)間倒序
注意:排序的時(shí)候不要用在字段類型是
text上,具體原因看可能遇到的問題第一個(gè)
// 按照`org.springframework.data.domain.Sort`來
Page<Article> articles = articleService.findArticleByName("docker搭建", PageRequest.of(0, 5,Sort.by(Sort.Direction.DESC,"id","createTime")));
// 按照命名規(guī)則來
Page<Article> articles = articleService.findArticleByNameOrderByCreateTimeDesc("docker搭建", PageRequest.of(0, 5));
統(tǒng)計(jì)文章名稱出現(xiàn)次數(shù)
使用場(chǎng)景好像沒有,了解即可
Long count = articleService.countArticleByName("docker搭建");
刪除文章
有很多方法,我只寫使用頻率高的
//無返回值刪除
articleService.deleteById("40");
// 根據(jù)對(duì)象刪除
articleService.delete(new Article());
//有返回值刪除,只能針對(duì)主鍵
List<Article> articles = articleService.deleteArticlesById("40");
其他未驗(yàn)證的感覺沒有使用場(chǎng)景的
- 根據(jù)
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.
這個(gè)問題是因?yàn)?code>Sort的字段name是text類型的,按照官方fieldata的建議,它禁止這樣操作,容易出現(xiàn)內(nèi)存過大
// fielddata = true 可以解決這個(gè)問題,但是可能帶來oom問題
@Field(type = FieldType.Text,fielddata = true,store = true, analyzer = "ik_smart", searchAnalyzer = "ik_max_word")
private String name;