目录
一、ElasticSearch核心概念
二、映射数据类型
2.1字符串类型:
2.2数值类型:
三、脚本使用Es
3.1添加、查询、删除索引
#创建索引
PUT student
#查询索引
GET student
#删除索引
DELETE student
3.2添加、查询映射
注:已经创建好的映射不可删除、不可更改
#添加映射
PUT student/_mapping
{
"properties":{
"name":{
"type":"keyword"
},
"age":{
"type":"integer"
}
}
}
#查询映射
GET student/_mapping#创建索引并添加映射(一步到位)
PUT student
{
"mappings": {
"properties": {
"name":{
"type": "text"
},
"age":{
"type": "long"
}
}
}
}
3.3添加、删除、更改、查询文档
3.3.1添加文档
注:指定id进行添加时,可以使用put或者post请求;不指定id进行添加时,只能使用post请求并且生成的id是随机的
#新增文档(指定id)
PUT student/_doc/1
{
"name":"张三",
"age":11,
"address":"江苏省南京市海淀区青石街"
}POST student/_doc/2
{
"name":"王五",
"age":15,
"address":"北京市海淀区粑粑街"
}
#新增文档(不指定指定id)
POST student/_doc
{
"name":"李四",
"age":14,
"address":"安徽省合肥市庐江县粑粑区"
}
3.3.2删除文档
#删除文档
DELETE student/_doc/1
3.3.2修改文档
#修改文档
PUT student/_doc/1
{
"name":"Mr.张三",
"age":22,
"address":"江苏省南京市海淀区青石街"
}
3.3.2查询文档
#查询单个文档
GET student/_doc/1
#查询全部文档
GET student/_search
四、javaApi使用Es
4.1导入依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.4.0</version>
</dependency>
4.2添加配置类
package com.motionlesstar.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties("elasticsearch")
public class EsConfig {
private String host;
private int port;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
@Bean
public RestHighLevelClient client() {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost(
host,
port,
"http"
)
));
return client;
}
}
#yml文件配置ip&&port
elasticsearch:
host: 192.168.23.129
port: 9200
4.3api操作
package com.motionlesstar.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.motionlesstar.domain.Teacher;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@RestController
public class TestController {
@Autowired
private RestHighLevelClient client;
//添加索引及映射
@GetMapping("/createIndexAndMapping")
public void createIndexAndMapping() throws IOException {
//1.使用client获取操作索引的对象
IndicesClient indicesClient = client.indices();
//2.创建索引和创建映射
//2.1创建索引
CreateIndexRequest createIndexRequest = new CreateIndexRequest("teacher");
//2.2创建映射
String mapping = "{\n" +
" \"properties\": {\n" +
" \"teacherName\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"teacherAge\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"teacherAddress\":{\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
" }";
createIndexRequest.mapping(mapping, XContentType.JSON);
CreateIndexResponse response = indicesClient.create(createIndexRequest, RequestOptions.DEFAULT);
//3.打印结果
System.out.println(response.isAcknowledged());
}
//查询索引及映射
@GetMapping("/queryIndexAndMapping")
public void queryIndexAndMapping() throws IOException {
//1.使用client获取操作索引的对象
IndicesClient indicesClient = client.indices();
GetIndexRequest getIndexRequest = new GetIndexRequest("teacher");
GetIndexResponse response = indicesClient.get(getIndexRequest, RequestOptions.DEFAULT);
//获取结果
Map<String, MappingMetaData> mappings = response.getMappings();
for (String key:mappings.keySet()){
System.out.println(key+":"+mappings.get(key).getSourceAsMap());
}
}
//删除索引
@GetMapping("/deleteIndexAndMapping")
public void deleteIndexAndMapping() throws IOException {
//1.使用client获取操作索引的对象
IndicesClient indicesClient = client.indices();
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("teacher");
AcknowledgedResponse response = indicesClient.delete(deleteIndexRequest, RequestOptions.DEFAULT);
//3.打印结果
System.out.println(response.isAcknowledged());
}
//判断索引是否存在
@GetMapping("/existIndexAndMapping")
public void existIndexAndMapping() throws IOException {
//1.使用client获取操作索引的对象
IndicesClient indicesClient = client.indices();
GetIndexRequest getIndexRequest = new GetIndexRequest("teacher");
boolean exists = indicesClient.exists(getIndexRequest, RequestOptions.DEFAULT);
//3.打印结果
System.out.println(exists);
}
//使用map集合添加文档
@GetMapping("/addDoc")
public void addDoc() throws IOException {
Map map = new HashMap();
map.put("teacherName","张良");
map.put("teacherAge",99);
map.put("teacherAddress","江苏省南京市海淀区雨花街");
//获取文档的对象
IndexRequest request = new IndexRequest("teacher").id("1").source(map);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(response.getId());
}
//使用java对象添加文档
@GetMapping("/addDoc2")
public void addDoc2() throws IOException {
Teacher teacher = new Teacher();
teacher.setId("2");
teacher.setTeacherName("老夫子");
teacher.setTeacherAge(88);
teacher.setTeacherAddress("安徽省合肥市海淀区雨花街");
ObjectMapper objectMapper = new ObjectMapper();
String data = objectMapper.writeValueAsString(teacher);
//获取文档的对象
IndexRequest request = new IndexRequest("teacher").id(teacher.getId()).source(data,XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(response.getId());
}
/**
* 修改文档
* 当添加文档时,若id已存在则为修改操作,若不存在则为添加操作
* @throws IOException
*/
@GetMapping("/updateDoc")
public void updateDoc() throws IOException {
Teacher teacher = new Teacher();
teacher.setId("2");
teacher.setTeacherName("老夫子22222");
teacher.setTeacherAge(88);
teacher.setTeacherAddress("安徽省合肥市海淀区雨花街");
ObjectMapper objectMapper = new ObjectMapper();
String data = objectMapper.writeValueAsString(teacher);
//获取文档的对象
IndexRequest request = new IndexRequest("teacher").id(teacher.getId()).source(data,XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(response.getId());
}
//根据id查询文档
@GetMapping("/getDocById")
public void getDocById() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
GetRequest getRequest = new GetRequest("teacher","1");
//getRequest.id("1");
GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
Teacher teacher = objectMapper.readValue(response.getSourceAsString(), Teacher.class);
System.out.println(teacher);
}
//删除文档
@GetMapping("/deleteDoc")
public void deleteDoc() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("teacher","2");
DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(response.getId());
}
}