Elasticsearch的基础使用和高阶使用

2 篇文章 0 订阅

Elasticsearch的基础使用和高阶使用

Elasticsearch是一款分布式的全文搜索和分析引擎,基于Lucene构建。它具有实时搜索、稳定、扩展性强等特点,被广泛用于日志分析、全文搜索、业务数据分析等场景。本文将深入介绍Elasticsearch的基础使用和高阶使用,包括安装配置、基本操作、高级查询、集群管理、性能优化等内容。

目录
  1. Elasticsearch概述
  2. Elasticsearch安装和配置
  3. Elasticsearch基础使用
    • 创建索引
    • 文档操作
    • 基本查询
  4. Elasticsearch高级使用
    • 高级查询
    • 聚合分析
    • 脚本查询
  5. Elasticsearch集群管理
    • 集群配置
    • 节点管理
    • 索引管理
  6. Elasticsearch性能优化
    • 索引优化
    • 查询优化
    • 资源管理
  7. 实际应用案例
  8. 总结

1. 概述

Elasticsearch 是一个开源的搜索引擎,基于Lucene库构建。它支持RESTful API,具有实时搜索能力和高扩展性,能够处理PB级别的数据量。

1.1 核心概念
  • 索引(Index):类似于数据库的概念,每个索引包含了多个文档。
  • 文档(Document):最小的可查询单位,存储实际数据。
  • 类型(Type):索引中的逻辑分类,7.x版本后已弃用。
  • 节点(Node):集群中的一个实例,存储数据并参与集群的索引和查询。
  • 集群(Cluster):由一个或多个节点组成的集合,共享同一个名字。

2. 安装和配置

2.1 安装Elasticsearch

可以在官方网站下载Elasticsearch安装包,支持多种操作系统。也可以通过Docker、Homebrew等方式安装。

  • 下载和解压

    wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.10.1-linux-x86_64.tar.gz
    tar -xzf elasticsearch-7.10.1-linux-x86_64.tar.gz
    cd elasticsearch-7.10.1
    
  • 启动Elasticsearch

    ./bin/elasticsearch
    
  • 验证安装
    打开浏览器访问http://localhost:9200,如果安装成功会返回集群的基本信息。

2.2 配置Elasticsearch

Elasticsearch的配置文件位于config/elasticsearch.yml。常用配置包括:

  • 集群名称

    cluster.name: my-cluster
    
  • 节点名称

    node.name: node-1
    
  • 数据存储路径

    path.data: /path/to/data
    
  • 日志存储路径

    path.logs: /path/to/logs
    
  • 网络配置

    network.host: 0.0.0.0
    http.port: 9200
    

3. 基础使用

3.1 创建索引

使用PUT请求创建索引:

curl -X PUT "localhost:9200/my_index?pretty"
3.2 文档操作
  • 添加文档

    curl -X POST "localhost:9200/my_index/_doc/1?pretty" -H 'Content-Type: application/json' -d'
    {
      "name": "John Doe",
      "age": 30,
      "about": "I love to go rock climbing"
    }
    '
    
  • 更新文档

    curl -X POST "localhost:9200/my_index/_update/1?pretty" -H 'Content-Type: application/json' -d'
    {
      "doc": {
        "age": 31
      }
    }
    '
    
  • 删除文档

    curl -X DELETE "localhost:9200/my_index/_doc/1?pretty"
    
3.3 基本查询
  • 匹配查询

    curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
    {
      "query": {
        "match": {
          "about": "rock climbing"
        }
      }
    }
    '
    
  • 精确查询

    curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
    {
      "query": {
        "term": {
          "age": 31
        }
      }
    }
    '
    

4. 高级使用

4.1 高级查询
  • 布尔查询

    curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
    {
      "query": {
        "bool": {
          "must": [
            { "match": { "about": "climbing" } }
          ],
          "filter": [
            { "term": { "age": 31 } }
          ]
        }
      }
    }
    '
    
  • 范围查询

    curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
    {
      "query": {
        "range": {
          "age": {
            "gte": 30,
            "lte": 40
          }
        }
      }
    }
    '
    
4.2 聚合分析
  • 求平均值

    curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
    {
      "aggs": {
        "average_age": {
          "avg": {
            "field": "age"
          }
        }
      },
      "size": 0
    }
    '
    
  • 分组统计

    curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
    {
      "aggs": {
        "age_groups": {
          "terms": {
            "field": "age"
          }
        }
      },
      "size": 0
    }
    '
    
4.3 脚本查询
  • 使用脚本进行查询
    curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
    {
      "query": {
        "script_score": {
          "query": { "match_all": {} },
          "script": {
            "source": "doc[\'age\'].value * params.factor",
            "params": { "factor": 1.2 }
          }
        }
      }
    }
    '
    

5. 集群管理

5.1 集群配置
  • 集群名称

    cluster.name: my-cluster
    
  • 节点名称

    node.name: node-1
    
  • 初始主节点

    discovery.seed_hosts: ["host1", "host2"]
    cluster.initial_master_nodes: ["node-1", "node-2"]
    
5.2 节点管理
  • 添加节点
    在新节点上进行相应配置,使其加入集群。

  • 移除节点
    使用API将节点从集群中移除:

    curl -X POST "localhost:9200/_cluster/voting_config_exclusions?node_names=node-1"
    
5.3 索引管理
  • 查看索引状态

    curl -X GET "localhost:9200/_cat/indices?v"
    
  • 关闭索引

    curl -X POST "localhost:9200/my_index/_close"
    
  • 打开索引

    curl -X POST "localhost:9200/my_index/_open"
    

6. 性能优化

6.1 索引优化
  • 分片和副本配置
    根据数据量合理配置分片和副本数量。

  • 使用别名
    通过别名管理索引,方便索引切换和升级。

6.2 查询优化
  • 避免深分页

使用search_afterscroll API替代深分页。

  • 缓存查询
    配置查询缓存,提高重复查询的效率。
6.3 资源管理
  • JVM内存配置
    调整JVM内存配置,确保Elasticsearch运行在最佳状态。

  • 磁盘I/O优化
    使用SSD磁盘提高I/O性能,减少延迟。

7. 实际应用案例

在实际应用中,Elasticsearch被广泛用于日志分析、全文搜索和业务数据分析。以下是几个典型案例:

7.1 日志分析

使用Elasticsearch结合Logstash和Kibana(ELK Stack)进行日志收集、处理和可视化分析,实时监控系统运行状态。

7.2 全文搜索

电商网站使用Elasticsearch实现产品搜索,通过多条件查询和过滤,提高搜索准确性和用户体验。

7.3 业务数据分析

金融行业使用Elasticsearch存储和分析交易数据,实时监控交易异常,提升风控能力。

8. 总结

通过本文的介绍,您应该对Elasticsearch的基础使用和高阶使用有了全面的了解。无论是索引管理、查询优化还是集群管理,Elasticsearch都提供了强大的功能和工具。通过合理配置和优化,您可以充分发挥Elasticsearch的潜力,为业务提供高效可靠的数据支持。

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我理解您的问题了。其实在ES5中的数组高阶函数本质上也是利用了原型的特性。不过在ES6中引入了箭头函数和更加简洁的语法,使得使用高阶函数更加方便。下面我将通过重构ES5中的数组高阶函数来展示如何利用原型的特性。 首先,我们来看一下ES5中的数组高阶函数的写法: ``` var arr = [1, 2, 3, 4, 5]; // map var newArr = arr.map(function(item) { return item * 2; }); // filter var filteredArr = arr.filter(function(item) { return item > 3; }); // reduce var reducedValue = arr.reduce(function(acc, item) { return acc + item; }, 0); ``` 现在我们来利用原型的特性,将这些高阶函数封装到数组的原型中: ``` Array.prototype.myMap = function(callback) { var newArr = []; for (var i = 0; i < this.length; i++) { newArr.push(callback(this[i], i, this)); } return newArr; }; Array.prototype.myFilter = function(callback) { var filteredArr = []; for (var i = 0; i < this.length; i++) { if (callback(this[i], i, this)) { filteredArr.push(this[i]); } } return filteredArr; }; Array.prototype.myReduce = function(callback, initValue) { var acc = initValue || this[0]; var startIndex = initValue ? 0 : 1; for (var i = startIndex; i < this.length; i++) { acc = callback(acc, this[i], i, this); } return acc; }; ``` 现在我们就可以像使用ES5中的数组方法一样来使用我们的自定义方法了: ``` var arr = [1, 2, 3, 4, 5]; // myMap var newArr = arr.myMap(function(item) { return item * 2; }); // myFilter var filteredArr = arr.myFilter(function(item) { return item > 3; }); // myReduce var reducedValue = arr.myReduce(function(acc, item) { return acc + item; }, 0); ``` 以上就是用原型的特性重构ES5中的数组高阶函数的方法。希望可以帮助到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CopyLower

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值