本文目录导读:
《ES数据库使用全攻略:从连接到高效操作》
ES数据库简介
Elasticsearch(ES)是一个分布式、开源的搜索和分析引擎,适用于各种数据类型,包括文本、数字、地理空间、结构化和非结构化数据等,它以其强大的搜索功能、可扩展性和高可用性在现代数据处理领域中占据着重要地位。
ES数据库连接
(一)安装与启动ES
1、安装
图片来源于网络,如有侵权联系删除
- 首先根据操作系统类型下载相应的Elasticsearch安装包,对于Linux系统,可以从官方网站下载.tar.gz
格式的安装包,以CentOS为例,下载完成后,解压安装包到指定目录,如/usr/local/elasticsearch
。
- 在Windows系统中,下载.zip
格式的安装包,解压到合适的文件夹。
2、启动
- 在Linux系统中,进入Elasticsearch的bin
目录,执行./elasticsearch
命令来启动ES服务,需要注意的是,如果是初次启动,可能需要调整系统的一些参数,如文件描述符数量、虚拟内存等,以确保ES能够正常运行。
- 在Windows系统中,执行bin\elasticsearch.bat
命令启动服务。
(二)连接ES的客户端
1、使用Java客户端连接(以Java High - Level REST Client为例)
添加依赖:在Java项目中,需要在pom.xml
(如果是Maven项目)中添加Elasticsearch客户端的依赖。
```xml
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch - rest - high - level - client</artifactId>
<version>7.14.0</version>
</dependency>
```
连接代码示例:
```java
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RestClient;
import java.io.IOException;
public class ESConnection {
public static void main(String[] args) {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
try {
// 这里可以进行一些简单的操作,如检查ES是否可连接
System.out.println(client.ping());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
2、使用Python客户端连接(以elasticsearch - python
为例)
安装:使用pip install elasticsearch
命令安装Python的Elasticsearch客户端。
连接代码示例:
```python
from elasticsearch import Elasticsearch
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
if es.ping():
print('ES连接成功')
else:
print('ES连接失败')
```
ES数据库的基本操作
(一)索引操作
1、创建索引
- 在ES中,索引类似于传统数据库中的表,使用Java客户端创建索引的示例如下:
```java
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
public class IndexCreation {
public static void main(String[] args) {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
CreateIndexRequest request = new CreateIndexRequest("my_index");
try {
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
if (response.isAcknowledged()) {
System.out.println("索引创建成功");
} else {
System.out.println("索引创建失败");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
- 使用Python客户端创建索引:
```python
from elasticsearch import Elasticsearch
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
index_name = "my_index"
body = {
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
response = es.indices.create(index = index_name, body = body)
if 'acknowledged' in response and response['acknowledged']:
print('索引创建成功')
else:
print('索引创建失败')
```
2、查看索引
- 在Java中,可以使用IndicesClient
来查看已存在的索引:
```java
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import java.io.IOException;
import java.util.Map;
public class IndexView {
public static void main(String[] args) {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
IndicesClient indicesClient = client.indices();
try {
Map<String, IndexMetadata> indexMap = indicesClient.getMetadata("", RequestOptions.DEFAULT);
for (String index : indexMap.keySet()) {
System.out.println("索引名称: " + index);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
- 在Python中,使用es.indices.get_alias()
方法可以查看索引:
```python
from elasticsearch import Elasticsearch
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
indices = es.indices.get_alias('*')
for index in indices.keys():
print('索引名称:', index)
```
3、删除索引
- 对于Java客户端,如下是删除索引的代码:
```java
图片来源于网络,如有侵权联系删除
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
public class IndexDeletion {
public static void main(String[] args) {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
DeleteIndexRequest request = new DeleteIndexRequest("my_index");
try {
DeleteIndexResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
if (response.isAcknowledged()) {
System.out.println("索引删除成功");
} else {
System.out.println("索引删除失败");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
- 在Python中,使用es.indices.delete()
方法来删除索引:
```python
from elasticsearch import Elasticsearch
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
index_name = "my_index"
response = es.indices.delete(index = index_name)
if 'acknowledged' in response and response['acknowledged']:
print('索引删除成功')
else:
print('索引删除失败')
```
(二)文档操作
1、插入文档
- 在Java中,向索引中插入文档的示例:
```java
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class DocumentInsertion {
public static void main(String[] args) {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
Map<String, Object> data = new HashMap<>();
data.put("name", "John Doe");
data.put("age", 30);
data.put("city", "New York");
IndexRequest request = new IndexRequest("my_index").id("1").source(data, XContentType.JSON);
try {
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
if (response.getResult() == DocWriteResponse.Result.CREATED) {
System.out.println("文档插入成功");
} else {
System.out.println("文档插入失败");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
- 在Python中,向索引插入文档:
```python
from elasticsearch import Elasticsearch
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
doc = {
"name": "John Doe",
"age": 30,
"city": "New York"
}
res = es.index(index = "my_index", id = "1", body = doc)
if res['result'] == 'created':
print('文档插入成功')
else:
print('文档插入失败')
```
2、查询文档
简单查询(根据ID查询)
- 在Java中:
```java
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
public class DocumentQueryById {
public static void main(String[] args) {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
GetRequest request = new GetRequest("my_index", "1");
try {
GetResponse response = client.get(request, RequestOptions.DEFAULT);
if (response.isExists()) {
System.out.println("文档内容: " + response.getSourceAsString());
} else {
System.out.println("文档不存在");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
- 在Python中:
```python
from elasticsearch import Elasticsearch
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
res = es.get(index = "my_index", id = "1")
if 'found' in res and res['found']:
print('文档内容:', res['_source'])
else:
print('文档不存在')
```
复杂查询(使用查询语句查询)
- 在Java中,使用SearchRequest
进行复杂查询,查询年龄大于25岁的人员:
```java
图片来源于网络,如有侵权联系删除
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
public class ComplexDocumentQuery {
public static void main(String[] args) {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
SearchRequest request = new SearchRequest("my_index");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.rangeQuery("age").gt(25));
request.source(sourceBuilder);
try {
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
System.out.println("查询结果: " + hit.getSourceAsString());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
- 在Python中,同样可以构建查询语句进行复杂查询:
```python
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
s = Search(using = es, index = "my_index").query("range", age = {"gt": 25})
response = s.execute()
for hit in response:
print('查询结果:', hit.to_dict())
```
3、更新文档
- 在Java中,更新文档的示例:
```java
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
public class DocumentUpdate {
public static void main(String[] args) {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
UpdateRequest request = new UpdateRequest("my_index", "1").doc(XContentType.JSON, "age", 31);
try {
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
if (response.getResult() == DocWriteResponse.Result.UPDATED) {
System.out.println("文档更新成功");
} else {
System.out.println("文档更新失败");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
- 在Python中:
```python
from elasticsearch import Elasticsearch
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
doc = {
"doc": {
"age": 31
}
}
res = es.update(index = "my_index", id = "1", body = doc)
if res['result'] == 'updated':
print('文档更新成功')
else:
print('文档更新失败')
```
4、删除文档
- 在Java中:
```java
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
public class DocumentDeletion {
public static void main(String[] args) {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
DeleteRequest request = new DeleteRequest("my_index", "1");
try {
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
if (response.getResult() == DocWriteResponse.Result.DELETED) {
System.out.println("文档删除成功");
} else {
System.out.println("文档删除失败");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
- 在Python中:
```python
from elasticsearch import Elasticsearch
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
res = es.delete(index = "my_index", id = "1")
if res['result'] == 'deleted':
print('文档删除成功')
else:
print('文档删除失败')
```
ES数据库的高级特性
(一)数据映射(Mapping)
1、概念
- 数据映射定义了文档中字段的类型、如何被索引以及如何被存储等信息,它类似于传统数据库中的表结构定义,但更加灵活,在ES中,有动态映射和显式映射两种方式。
2、动态映射示例
- 当向ES索引插入文档时,如果索引不存在映射,ES会根据文档中的数据自动创建映射,插入一个包含name
(字符串类型)和`
评论列表