环境搭建部分
第一步:导入maven依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch‐rest‐high‐level‐client</artifactId>
<version>6.2.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.2.1</version>
</dependency>
第二步:由于项目中使用boot搭建,so开始编写配置文件application.yml
server:
port: ${port:40100}
spring:
application:
name: xc‐search‐service
xuecheng:
elasticsearch:
hostlist: ${eshostlist:127.0.0.1:9200} #多个结点中间用逗号分隔
Boot的环境搭建就省略不写了,主要是API的使用以及es的配置信息
第三步:编写es配置类(es是restful风格接口,且可以访问多个集群节点)
package com.xuecheng.search.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author weilai
* @version 1.0
* @create 2018-07-08 8:53
**/
@Configuration
public class ElasticsearchConfig {
//配置了es的ip和端口,多个地址中间以逗号分隔
@Value("${xuecheng.elasticsearch.hostlist}")
private String hostlist;
@Bean
public RestHighLevelClient restHighLevelClient(){
String[] hostlistarry = hostlist.split(",");
HttpHost[] httpHosts =new HttpHost[hostlistarry.length];
int i=0;
for(String host:hostlistarry){
String[] server = host.split(":");
HttpHost httpHost = new HttpHost(server[0],Integer.parseInt(server[1]));
httpHosts[i] = httpHost;
i++;
}
RestHighLevelClient restHighLevelClient= new RestHighLevelClient(RestClient.builder(httpHosts));
return restHighLevelClient;
}
}
编写启动引导类(博客中忽略...)
环境搭建完成....开始索引库CRUD的操作
索引库CRUD
编写springBoot测试类,开始CRUD操作
创建索引库:
原始API操作:
put请求 http://localhost:9200/索引库名称 /类型名称/_mapping
例如:创建类型为xc_course的映射,共包括三个字段:name、description、studymondel
请求:http://localhost:9200/xc_course/doc/_mapping
请求体:
{
"properties": {
"name": {
"type": "text",
"analyzer":"ik_max_word",
"search_analyzer":"ik_smart"
},
"description": {
"type": "text",
"analyzer":"ik_max_word",
"search_analyzer":"ik_smart"
},
"studymodel": {
"type": "keyword"
},
"price": {
"type": "float"
},
"timestamp": {
"type": "date",
"format": "yyyy‐MM‐dd HH:mm:ss||yyyy‐MM‐dd||epoch_millis"
}
}
}
JAVA操作的方式为
@Test
public void testCreateIndex() throws IOException {
//用于创建索引库的对象,指定索引库名称
CreateIndexRequest createIndexRequest = new CreateIndexRequest("xc_a");
//设置索引库的分片数量及副本的数量
createIndexRequest.settings(
Settings.builder().put("number_of_shards",1)//索引库的分片数量
.put("number_of_replicas",0));
//参数:String type, String source, XContentType xContentType
String mapping_source = " {\n" +
" \t\"properties\": {\n" +
" \"name\": {\n" +
" \"type\": \"text\",\n" +
" \"analyzer\":\"ik_max_word\",\n" +
" \"search_analyzer\":\"ik_smart\"\n" +
" },\n" +
" \"description\": {\n" +
" \"type\": \"text\",\n" +
" \"analyzer\":\"ik_max_word\",\n" +
" \"search_analyzer\":\"ik_smart\"\n" +
" },\n" +
" \"studymodel\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"price\": {\n" +
" \"type\": \"float\"\n" +
" },\n" +
" \"timestamp\": {\n" +
" \t\t\"type\": \"date\",\n" +
" \t\t\"format\": \"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis\"\n" +
" \t}\n" +
" }\n" +
"}";
createIndexRequest.mapping("doc",mapping_source, XContentType.JSON);
//创建IndicesClient对象向ES提交请求
IndicesClient indices = client.indices();
//创建索引库
CreateIndexResponse createIndexResponse= indices.create(createIndexRequest);
//得到响应的结果
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println(acknowledged);
}
添加文档
原始API操作:
put http://localhost:9200/xc_course/doc/3/
请求体:
{
"name":"spring cloud实战",
"description":"本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战SpringBoot 4.注册中心eureka。",
"studymodel":"201001"
"price":5.6
}