文章目录
- pom.xml
- application.yml
- CubemallSearchApplication.java
- RestClientTest.java
- 使用ElasticsearchRestTemplate对象
- Blog.java
- RestTemplateTest.java
pom.xml
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency>
application.yml
spring:elasticsearch:rest:uris:- 1.1.1.1:9200- 2.2.2.2:9200- 3.3.3.3:9200
CubemallSearchApplication.java
package com.xd.cubemall.search;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class CubemallSearchApplication{public static void main(String[] args) {SpringApplication.run(CubemallSearchApplication.class);}
}
RestClientTest.java
package com.xd.cubemall.sdes;import com.xd.cubemall.search.CubemallSearchApplication;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.io.IOException;@RunWith(SpringRunner.class)
@SpringBootTest(classes = CubemallSearchApplication.class)
public class RestClientTest {@Autowiredprivate RestHighLevelClient restHighLevelClient;@Testpublic void testRestClient() throws IOException {restHighLevelClient.indices().create(new CreateIndexRequest("test"), RequestOptions.DEFAULT);}}
使用ElasticsearchRestTemplate对象
- 创建索引库
template.indexOps(IndexCoordinates.of(“mytest”)).create(); - 设置mapping信息
需要创建一个实体类,其中配置实体类和文档的映射关系,使用注解配置
可以从实体类中生成mapping信息
Blog.java
package com.xd.cubemall.search.model;import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;@Data
@Document(indexName = "blog_1", shards = 5, replicas = 1)
public class Blog {@Id@Field(type = FieldType.Long, store = true)private Long id;@Field(type = FieldType.Text, analyzer = "ik_max_word", store = true)private String title;@Field(type = FieldType.Text, analyzer = "ik_max_word", store = true)private String content;@Field(type = FieldType.Text, analyzer = "ik_max_word", store = true)private String comment;@Field(type = FieldType.Keyword, store = true)private String mobile;
}
RestTemplateTest.java
package com.xd.cubemall.sdes;import com.xd.cubemall.search.CubemallSearchApplication;
import com.xd.cubemall.search.model.Blog;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest(classes = CubemallSearchApplication.class)
public class RestTemplateTest {@Autowiredprivate ElasticsearchRestTemplate template;@Testpublic void createIndex(){template.indexOps(IndexCoordinates.of("blog_1")).create();}@Testpublic void putMapping() {Document mapping = template.indexOps(IndexCoordinates.of("blog_1")).createMapping(Blog.class);template.indexOps(IndexCoordinates.of("blog_1")).putMapping(mapping);}@Testpublic void createIndexWithMapping() {template.indexOps(Blog.class).create();Document mapping = template.indexOps(IndexCoordinates.of("blog_1")).createMapping(Blog.class);template.indexOps(Blog.class).putMapping(mapping);}@Testpublic void deleteIndex() {template.indexOps(IndexCoordinates.of("hello1")).delete();}
}