Elasticsearch 是一款开源的分布式搜索和分析引擎,能够近乎实时地存储、搜索和分析大规模数据,常用于日志分析、全文搜索和实时数据分析等场景。
1.00¥ 原价为:1.00¥。0.00¥当前价格为:0.00¥。
Elasticsearch 是一款强大且流行的开源分布式搜索和分析引擎。它构建在 Apache Lucene 之上,能够近乎实时地存储、搜索和分析大量数据。其分布式特性使其能够轻松地进行水平扩展,处理海量数据并提供高可用性。
主要特点:
核心功能:
本部分描述在 Elasticsearch 服务已经成功运行并可以通过 RESTful API 访问的前提下,用户进行基本数据操作的流程。
索引数据 (Indexing): 使用 HTTP PUT 请求向指定的索引和类型 (在 7.x 版本后类型已被弱化,通常直接指定文档 ID) 发送 JSON 文档来索引数据。
curl -X PUT "localhost:9200/my_index/_doc/1" -H 'Content-Type: application/json' -d'
{
"title": "Elasticsearch: The Definitive Guide",
"author": "Clinton Gormley, Zachary Tong",
"publish_date": "2015-02-07",
"tags": ["elasticsearch", "search", "distributed"]
}'
获取文档 (Getting a Document): 使用 HTTP GET 请求获取指定索引、类型和 ID 的文档。
curl -X GET "localhost:9200/my_index/_doc/1"
搜索文档 (Searching): 使用 HTTP POST 请求向指定的索引和 _search
端点发送查询请求体 (JSON)。
简单查询 (Match All):
curl -X POST "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}'
全文搜索 (Match Query):
curl -X POST "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"title": "guide elasticsearch"
}
}
}'
结构化搜索 (Term Query):
curl -X POST "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"term": {
"author": "Clinton Gormley"
}
}
}'
更新文档 (Updating a Document): 使用 HTTP POST 请求向指定的索引、类型和 ID 的 _update
端点发送更新请求体。
curl -X POST "localhost:9200/my_index/_doc/1/_update" -H 'Content-Type: application/json' -d'
{
"doc": {
"price": 49.99
}
}'
删除文档 (Deleting a Document): 使用 HTTP DELETE 请求删除指定索引、类型和 ID 的文档。
curl -X DELETE "localhost:9200/my_index/_doc/1"
创建索引 (Creating an Index): 使用 HTTP PUT 请求创建新的索引,可以指定索引的设置和映射 (mapping,定义字段类型和属性)。
curl -X PUT "localhost:9200/my_new_index" -H 'Content-Type: application/json' -d'
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"title": { "type": "text" },
"author": { "type": "keyword" },
"publish_date": { "type": "date" }
}
}
}'
删除索引 (Deleting an Index): 使用 HTTP DELETE 请求删除指定的索引。
curl -X DELETE "localhost:9200/my_new_index"
使用聚合 (Aggregations): 在搜索请求体中添加 aggregations
部分进行数据分析。
curl -X POST "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
"aggs": {
"authors": {
"terms": {
"field": "author"
}
}
}
}'
假设我们的 Elasticsearch 服务正在本地的 9200 端口运行。
索引一本名为 “Learning Elasticsearch” 的书籍信息:
curl -X PUT "localhost:9200/books/_doc/1" -H 'Content-Type: application/json' -d'
{
"title": "Learning Elasticsearch",
"author": "Rafał Kuć",
"publish_date": "2015-10-06",
"tags": ["elasticsearch", "learning", "search"]
}'
索引另一本名为 “Elasticsearch in Action” 的书籍信息:
curl -X PUT "localhost:9200/books/_doc/2" -H 'Content-Type: application/json' -d'
{
"title": "Elasticsearch in Action",
"author": "Radu Gheorghe, Matthew Lee Hinman, Roy Russo",
"publish_date": "2015-12-23",
"tags": ["elasticsearch", "search", "action"]
}'
搜索标题包含 “elasticsearch” 的书籍:
curl -X POST "localhost:9200/books/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"title": "elasticsearch"
}
}
}'
预期输出 (包含匹配到的文档):
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.69314718,
"hits" : [
{
"_index" : "books",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.69314718,
"_source" : {
"title" : "Learning Elasticsearch",
"author" : "Rafał Kuć",
"publish_date" : "2015-10-06",
"tags" : [
"elasticsearch",
"learning",
"search"
]
}
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.69314718,
"_source" : {
"title" : "Elasticsearch in Action",
"author" : "Radu Gheorghe, Matthew Lee Hinman, Roy Russo",
"publish_date" : "2015-12-23",
"tags" : [
"elasticsearch",
"search",
"action"
]
}
}
]
}
}
使用聚合统计书籍的作者:
curl -X POST "localhost:9200/books/_search" -H 'Content-Type: application/json' -d'
{
"aggs": {
"authors": {
"terms": {
"field": "author"
}
}
}
}'
预期输出 (包含作者统计结果):
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [ ... ]
},
"aggregations" : {
"authors" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "radu gheorghe, matthew lee hinman, roy russo",
"doc_count" : 1
},
{
"key" : "rafał kuć",
"doc_count" : 1
}
]
}
}
}