大规模数据搜索分析引擎(Elasticsearch)

应用分类:

Elasticsearch 是一款开源的分布式搜索和分析引擎,能够近乎实时地存储、搜索和分析大规模数据,常用于日志分析、全文搜索和实时数据分析等场景。

原价为:1.00¥。当前价格为:0.00¥。

大规模数据搜索分析引擎 Elasticsearch 介绍

一、Elasticsearch 主要特点及核心功能

Elasticsearch 是一款强大且流行的开源分布式搜索和分析引擎。它构建在 Apache Lucene 之上,能够近乎实时地存储、搜索和分析大量数据。其分布式特性使其能够轻松地进行水平扩展,处理海量数据并提供高可用性。

主要特点:

  • 近实时搜索 (Near Real-Time – NRT): Elasticsearch 能够以非常低的延迟(通常是 1 秒以内)索引和搜索数据。
  • 分布式架构: Elasticsearch 本身就是分布式的,支持将数据分片 (Sharding) 存储在多个节点上,并通过副本 (Replicas) 提供高可用性和容错能力。
  • RESTful API: Elasticsearch 提供了一套全面的 RESTful API,可以使用 JSON 通过 HTTP 进行交互,方便进行数据索引、搜索、管理和监控。
  • 灵活的数据模型: 虽然是基于 Lucene 的全文搜索引擎,但 Elasticsearch 并不强制预定义模式 (schema-less),可以灵活地存储和处理各种结构化和非结构化数据。
  • 强大的搜索功能: Elasticsearch 支持全文搜索、结构化搜索、地理空间搜索、近似搜索、自动完成、搜索建议等丰富的搜索功能。
  • 分析和聚合: Elasticsearch 提供了强大的聚合框架,可以对数据进行分组、统计、计算指标等复杂的分析操作,用于生成报表、仪表盘和进行数据挖掘。
  • 可扩展性: 可以通过简单地添加更多节点来水平扩展 Elasticsearch 集群,以应对不断增长的数据量和查询负载。
  • 高可用性: 通过副本机制,即使部分节点发生故障,集群也能继续提供服务。
  • 丰富的插件生态系统: Elasticsearch 拥有庞大的插件生态系统,可以扩展其功能,例如安全认证、监控、可视化 (Kibana) 等。

核心功能:

  • 数据索引: 将各种格式的数据存储到 Elasticsearch 中。
  • 全文搜索: 对存储的数据进行高效的全文检索。
  • 结构化搜索: 基于字段值、范围等条件进行精确搜索。
  • 数据分析: 使用聚合框架对数据进行统计和分析。
  • 分布式存储和搜索: 将数据分布在多个节点上进行存储和查询。
  • 高可用性: 通过副本保证系统的可用性。

二、基础使用流程

本部分描述在 Elasticsearch 服务已经成功运行并可以通过 RESTful API 访问的前提下,用户进行基本数据操作的流程。

  1. 索引数据 (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"]
    }'
    
  2. 获取文档 (Getting a Document): 使用 HTTP GET 请求获取指定索引、类型和 ID 的文档。

    curl -X GET "localhost:9200/my_index/_doc/1"
    
  3. 搜索文档 (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"
          }
        }
      }'
      
  4. 更新文档 (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
      }
    }'
    
  5. 删除文档 (Deleting a Document): 使用 HTTP DELETE 请求删除指定索引、类型和 ID 的文档。

    curl -X DELETE "localhost:9200/my_index/_doc/1"
    
  6. 创建索引 (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" }
        }
      }
    }'
    
  7. 删除索引 (Deleting an Index): 使用 HTTP DELETE 请求删除指定的索引。

    curl -X DELETE "localhost:9200/my_new_index"
    
  8. 使用聚合 (Aggregations): 在搜索请求体中添加 aggregations 部分进行数据分析。

    curl -X POST "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
    {
      "aggs": {
        "authors": {
          "terms": {
            "field": "author"
          }
        }
      }
    }'
    

三、简单使用实例

假设我们的 Elasticsearch 服务正在本地的 9200 端口运行。

  1. 索引一本名为 “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"]
    }'
    
  2. 索引另一本名为 “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"]
    }'
    
  3. 搜索标题包含 “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"
              ]
            }
          }
        ]
      }
    }
    
  4. 使用聚合统计书籍的作者:

    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
            }
          ]
        }
      }
    }