ES常用API总结

文章目录

本文内容部分使用AI生成技术

目的

本文总结了关于ES从安装到一些常见的关于ES的API使用示例,方便可以后面快速查询使用。

一、下载安装ES

1.下载:
下载地址

2.安装

下载完成后解压,并执行以下命令启动:

bash 复制代码
# 执行解压后目录下的 /bin/elasticsearch 命令启动
> /Users/mac/Downloads/elasticsearch-9.2.3/bin/elasticsearch 

3.登录

浏览器输入:https://localhost:9200/

账号:elastic

密码:复制日志中打印的密码:

bash 复制代码
Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`):
  lvxxxxxxxxxx

4.安装 Chrome ElasticVue 扩展程序(可选)

5.解决SSL报错(可选)

关闭后不用使用https访问

bash 复制代码
vim elasticsearch.yml
修改:
# 关闭安全功能
xpack.security.enabled: false
# 关闭 HTTP 层的 SSL
xpack.security.http.ssl.enabled: false

6.安装ik分词器插件

二、ES使用

主要分为两大类别:

1.对索引(相当于数据库的DDL)的操作

2.对文档(相当于数据库的DML)的操作

1、ES索引

创建索引

bash 复制代码
PUT /product/_mapping
{
  "mappings": {
    "properties": {
      "product_name": { 
        "type": "text",  // 【规则】Text类型:用于全文搜索。存入"华为Mate60 Pro"会被分词为【华为、Mate60、Pro】。
        "analyzer": "ik_max_word",  // 使用IK中文分词
        "fields": {     // 多字段特性:一个字段以两种方式索引。
          "keyword": { 
            "type": "keyword"  // 【规则】Keyword类型:用于精确匹配、聚合、排序。存入的是完整的"华为Mate60 Pro"字符串。
          }
        }
      },
      "brand": { 
        "type": "keyword" // 品牌名通常用于精确过滤(如`品牌=小米`)或聚合统计,故用keyword。
      },
      "price": { "type": "float" },
      "title": {
          "type": "text",
          "analyzer": "ik_max_word",  // 使用IK中文分词
          "search_analyzer": "ik_smart"
      },
      "create_time": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss"
      },
      "tags": { "type": "keyword" }  // 标签也适合精确匹配和聚合。精确值,用于过滤、聚合
    }
  },
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2
  }
}

2、ES文档

添加文档

bash 复制代码
// 1. 创建单个文档 (指定ID)
PUT /product/_doc/1
{ "product_name": "华为Mate60 Pro", "brand": "华为", "price": 6999, "tags": ["手机", "旗舰", "新品"] ,
"title": "小米手机", "create_time": "2023-10-01 12:00:00"}

// 2. 批量操作(高效增删改)
POST /_bulk
{"index":{"_index":"product","_id":"2"}}
{"product_name":"iPhone 15","brand":"苹果","price":5999,"tags":["手机","iOS"],
"title": "苹果手机", "create_time": "2024-10-01 12:00:00"}
{"index":{"_index":"product","_id":"3"}}
{"product_name":"小米13 Ultra","brand":"小米","price":6499,"tags":["手机","相机", "旗舰"],
"title": "苹果手机", "create_time": "2025-10-01 12:00:00"}

查询文档(重点)

规则 对应查询类型 应用场景与示例
match: 对查询词分词,模糊匹配 match, multi_match 在商品名称、描述中进行关键词搜索。
term: 不对查询词分词,精确匹配 term, terms 对keyword字段进行精确筛选,如品牌、状态码、标签。
bool: 组合多个查询条件 bool 组合 必须满足(must)、应该满足(should)、必须不满足(must_not)和过滤(filter) 条件。
must: 必须满足,参与算分 嵌套在 bool 内 影响结果相关度排序的核心条件。
filter: 必须满足,不参与算分 嵌套在 bool 内 用于结构化数据过滤(范围、状态),不关心相关性,性能更优。
should: 应该满足(或逻辑) 嵌套在 bool 内 实现"或"逻辑,常与 minimum_should_match 配合。
must_not: 必须不满足 嵌套在 bool 内 排除不需要的数据。

查询嵌套优先级:
query -> bool -> must/filter/should -> term/terms/match
其中:
must/filter/should 决定了是否对文档进行分词以及是否计算得分
term/terms/match 则控制查询关键词是否进行分词处理

简单查询
bash 复制代码
# 基础匹配查询
GET /product/_search
{
  "query": {
    "match": {
      "title": "小米手机"
    }
  },
  "from": 0,
  "size": 10
}

# 多字段搜索
GET /product/_search
{
  "query": {
    "multi_match": {
      "query": "小米",
      "fields": ["title", "brand^2"]  // brand字段权重加倍
    }
  }
}
复杂查询
bash 复制代码
# 精确过滤与范围查询
GET /product/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "title": "手机" } }
      ],
      "filter": [
        { "term": { "brand": "小米" } },
        { "range": { "price": { "gte": 3000, "lte": 5000 } } },
        { "exists": { "field": "tags" } }
      ]
    }
  }
}

# 复杂布尔组合
GET /product/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "title": "手机" } }
      ],
      "should": [
        { "term": { "tags": "新品" } },
        { "range": { "price": { "lte": 4000 } } }
      ],
      "minimum_should_match": 1,
      "must_not": [
        { "term": { "brand": "苹果" } }
      ]
    }
  }
}

# 指标与分组聚合
GET /product/_search
{
  "size": 0,
  "aggs": {
    "price_stats": {
      "stats": { "field": "price" }  // 统计: 计数、总和、平均、最小、最大
    },
    "group_by_brand": {
      "terms": { "field": "brand" },  // 按品牌分组
      "aggs": {
        "avg_price": { "avg": { "field": "price" } }  // 每个品牌的平均价格
      }
    },
    "price_histogram": {
      "histogram": {
        "field": "price",
        "interval": 1000,
        "extended_bounds": { "min": 0, "max": 10000 }
      }
    }
  }
}

# 时间范围统计
GET /product/_search
{
  "size": 0,
  "aggs": {
    "sales_over_time": {
      "date_histogram": {
        "field": "create_time",
        "calendar_interval": "month",
        "format": "yyyy-MM"
      },
      "aggs": {
        "total_sales": { "sum": { "field": "price" } }
      }
    }
  }
}

# 全文检索(高亮显示)
GET /product/_search
{
  "query": {
    "match": { "title": "智能手机" }
  },
  "highlight": {
    "fields": {
      "title": {
        "pre_tags": ["<em>"],
        "post_tags": ["</em>"]
      }
    }
  }
}

参考文档:

https://www.elastic.co/docs/reference/elasticsearch/rest-apis#documenthttpswww.elastic.codocsapidocelasticsearchv9groupendpoint-document

相关推荐
Elasticsearch14 小时前
一次编辑,更新所有仪表板:使用 Terraform 大规模管理 Kibana 可观测性配置
elasticsearch
Elasticsearch15 小时前
足够接近就是足够快:ES|QL Fast 模式如何让 Kibana 仪表板速度提升最高 100 倍
elasticsearch
Elasticsearch16 小时前
一分钟内从提示词生成仪表板,成本降低 5 倍:Kibana 中的 AI 仪表板和自定义 Vega-Lite 图表
elasticsearch
雾屿_Mistisle16 小时前
敏感文件泄露
大数据·elasticsearch·搜索引擎
Elasticsearch16 小时前
Elastic 9.5: Columnar 、 VectorDB 索引模式与自动校准,以及由 AI 驱动的告警分类整理
elasticsearch
晚安code16 小时前
一、Elasticsearch查询的 DSL 骨架:先把 JSON 看懂
elasticsearch
XS03010617 小时前
Git远程仓库实操笔记
笔记·git·elasticsearch
love530love18 小时前
Ubuntu系统通过Homebrew安装Lightpanda完整实战教程(含端口占用排坑)
大数据·linux·运维·人工智能·elasticsearch·搜索引擎
Elasticsearch1 天前
将 Embedding 模型加载到 Elasticsearch 中
elasticsearch
Elasticsearch2 天前
使用 Elasticsearch open inference API 对 OpenAI chat completions 进行支持
elasticsearch