结构化数据:数据库mysql
非结构化数据:比如音频,视频等(nosql,redis,mangdb)
半结构化数据:xml,html等(redis,mangdb)
Elasticsearcs是面向文档型数据库,一条数据在这里就是一个文档。
ES里的 Index 可以看做一个库,而 Types 相当于表, Documents 则相当于表的行。
这里Types 的概念已经被逐渐弱化, Elasticsearch 6.X 中,一个 index 下已经只能包含一个
type Elasticsearch 7.X 中 , Type 的概念已经被删除了。
es基础操作各种请求发送url以及格式(postman)------------------------------------------------------------------------------------------------------------------------------------------
在Postman 中,向 ES 服务器发 PUT 请求 http://127.0.0.1:9200/shopping
http://127.0.0.1:9200/_cat/indices?v
这里请求路径中的_cat 表示查看的意思, indices 表示索引,所以整体含义就是查看当前 ES
服务器中的所有索引,就好像 MySQL 中的 show tables 的感觉,服务器响应结果如下
重新访问索引时,服务器返回响应:索引不存在
向 ES 服务器发 POST 请求 http://127.0.0.1:9200/sh opping /_doc
如果想要自定义唯一性标识,需要在创建时指定http://127.0.0.1:9200/shopping/_doc/ 1001
此处需要注意:如果增加数据时明确数据主键,那么请求方式也可以为PUT
向 ES 服务器发 GET 请求 http://127.0.0.1:9200/sh opping /_doc/1
http://127.0.0.1:9200/shopping/_search
向 ES 服 务器发 POST 请求 http://127.0.0.1:9200/shopping /_doc/1
向 ES 服务器发 POST 请求 http://127.0.0.1:9200/sh opping/opping/_update/1
{
"doc":{
"address":"北京"
}
}
向 ES 服务器发 DELETE 请求 http://127.0.0.1:9200/sh opping /_doc/1
如果删除一个并不存在的文档
http://127.0.0.1:9200/shopping/_search
{"query":{
"match":{
"address":"重庆"
}
}
}
http://127.0.0.1:9200/shopping/_search
{"query":{
"match_all":{
}
}
}
http://127.0.0.1:9200/shopping/_search
{
"query":{
"match_all":{
}
},
"from":5,
"size":5,
"_source":["address","age"],
"sort":{
"age":{
"order":"asc"
}
}
}
http://127.0.0.1:9200/shopping/_search
{
"query":{
"bool":{
"must":[
{
"match":{
"address":"北京"
}
},
{
"match":{
"name":"冉述保"
}
}
]
}
}
}
http://127.0.0.1:9200/shopping/_search
{
"query":{
"bool":{
"should":[
{
"match":{
"address":"北京"
}
},
{
"match":{
"address":"重庆"
}
}
]
}
}
}
http://127.0.0.1:9200/shopping/_search
{
"query":{
"bool":{
"should":[
{
"match":{
"address":"北京"
}
},
{
"match":{
"address":"重庆"
}
}
],
"filter":{
"range":{
"age":{
"gt":26
}
}
}
}
}
}
http://127.0.0.1:9200/shopping/_search
{
"query":{
"match_phrase":{
"address":"北京"
}
},
"highlight":{
"fields":{
"address":{}
}
}
}
http://127.0.0.1:9200/shopping/_search
{
"aggs":{
"age_group":{
"terms":{
"field":"age"
}
}
},
"size":0
}
http://127.0.0.1:9200/shopping/_search
{
"aggs":{
"age_avg":{
"avg":{
"field":"age"
}
}
},
"size":0
}
http://127.0.0.1:9200/user/_mapping
{
"properties":{
"name":{
"type":"text",
"index":true
},
"sex":{
"type":"keyword",
"index":true
},
"phone":{
"type":"keyword",
"index":false
}
}
}
http://127.0.0.1:9200/user/_mapping
http://127.0.0.1:9200/user/_search
{"query":{
"match":{
"name":"张"
}
}
}
{"query":{
"match":{
"sex":"男"
}
}
}
无结果
JavaAPI操作es(基于代码)---------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>web_demo</artifactId>
<groupId>com.zgs.test</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>es-test</artifactId>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.8.0</version>
</dependency>
<!--elasticsearch的客户端-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.8.0</version>
</dependency>
<!--elasticsearch 依赖 2.x 的 log4j-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
package com.zgs.es.test;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import java.io.IOException;
/**
* @author zgs
* @date 2021年12月01日 18:01:00
*/
@Slf4j
public class EsTest_Index_Create {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//创建索引
CreateIndexRequest request = new CreateIndexRequest("zgs1");
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
log.info("-----------"+response.toString());
boolean acknowledged = response.isAcknowledged();
System.out.println("acknowledged-------"+acknowledged);
client.close();
}
}
package com.zgs.es.test;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import java.io.IOException;
/**
* @author zgs
* @date 2021年12月02日 15:32:00
*/
@Slf4j
public class EsTest_Index_Search {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder
(new HttpHost("localhost",9200,"http")));
//查询索引
GetIndexRequest request = new GetIndexRequest("zgs");
GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
log.info("#########################"+response.getAliases());
log.info("#########################"+response.getDataStreams());
log.info("#########################"+response.getMappings());
log.info("#########################"+response.getSettings());
client.close();
}
}
package com.zgs.es.test;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import java.io.IOException;
/**
* @author zgs
* @date 2021年12月02日 15:41:00
*/
@Slf4j
public class EsTest_Index_Delete {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder
(new HttpHost("localhost",9200,"http"))
);
//删除索引
DeleteIndexRequest request = new DeleteIndexRequest("zgs");
AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
log.info("#########################"+response.isAcknowledged());
client.close();
}
}
package com.zgs.es.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
/**
* @author zgs
* @date 2021年12月02日 16:24:00
*/
@Slf4j
public class EsTest_Doc_Insert {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
IndexRequest request = new IndexRequest();
request.index("zgs").id("1001");
ZgsVo zgsVo = new ZgsVo();
zgsVo.setName("张贵松");
zgsVo.setAge(24);
zgsVo.setSex("男");
//转换为json
ObjectMapper mappers = new ObjectMapper();
String zgsJson = mappers.writeValueAsString(zgsVo);
request.source(zgsJson, XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
log.info("########################"+response.getResult());
client.close();
}
}
package com.zgs.es.test;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
/**
* @author zgs
* @date 2021年12月02日 17:28:00
*/
@Slf4j
public class EsTest_Doc_Update {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
UpdateRequest request = new UpdateRequest();
request.index("zgs").id("1001");
request.doc(XContentType.JSON,"name","张帅");
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
log.info("###########################"+response.getGetResult());
client.close();
}
}
package com.zgs.es.test;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
/**
* @author zgs
* @date 2021年12月02日 17:38:00
*/
@Slf4j
public class EsTest_Doc_Get {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
GetRequest request = new GetRequest();
request.index("zgs").id("1001");
GetResponse response = client.get(request, RequestOptions.DEFAULT);
log.info("###########################"+response.getSourceAsString());
client.close();
}
}
package com.zgs.es.test;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
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.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
/**
* @author zgs
* @date 2021年12月02日 17:40:00
*/
@Slf4j
public class EsTest_Doc_Delete {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
DeleteRequest request = new DeleteRequest();
request.index("zgs").id("1001");
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
log.info("###########################"+response.toString());
client.close();
}
}
package com.zgs.es.test;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
/**
* @author zgs
* @date 2021年12月02日 17:55:00
*/
@Slf4j
public class EsTest_Doc_Insert_Batch {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new IndexRequest().index("zgs").id("1001").source(XContentType.JSON,"name","张帅1"));
bulkRequest.add(new IndexRequest().index("zgs").id("1002").source(XContentType.JSON,"name","张帅2"));
bulkRequest.add(new IndexRequest().index("zgs").id("1003").source(XContentType.JSON,"name","张帅3"));
BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
log.info("#########################"+response.getIngestTook());
log.info("#########################"+response.getItems());
client.close();
}
}
package com.zgs.es.test;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
/**
* @author zgs
* @date 2021年12月02日 18:08:00
*/
@Slf4j
public class EsTest_Doc_Delete_Batch {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new DeleteRequest().index("zgs").id("1002"));
bulkRequest.add(new DeleteRequest().index("zgs").id("1003"));
BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
log.info("#########################"+response.getIngestTook());
log.info("#########################"+response.getItems());
client.close();
}
}