Elasticsearch 8.13.4 常用搜索操作完全指南

Elasticsearch 作为分布式搜索和分析引擎,提供了丰富的搜索能力。本文将详细介绍 Elasticsearch 8.13.4 中最常用的搜索操作,帮助您快速掌握其核心搜索功能。

一、基础概念回顾

在开始搜索操作前,让我们简要回顾几个核心概念:

  • 索引(Index): 存储文档的容器,类似于数据库中的表
  • 文档(Document): 可搜索的基本信息单元,以 JSON 格式表示
  • 倒排索引: Elasticsearch 使用的数据结构,支持快速全文搜索

二、常用搜索操作

1. 匹配所有文档查询

最基础的搜索,返回索引中的所有文档:

json 复制代码
GET /your_index/_search
{
  "query": {
    "match_all": {}
  }
}

2. 全文搜索 (Match Query)

最常用的全文搜索查询,会对查询文本进行分析:

json 复制代码
GET /your_index/_search
{
  "query": {
    "match": {
      "field_name": "搜索关键词"
    }
  }
}

3. 短语匹配 (Match Phrase Query)

要求所有词条都出现且顺序相同:

json 复制代码
GET /your_index/_search
{
  "query": {
    "match_phrase": {
      "field_name": "精确短语搜索"
    }
  }
}

4. 多字段搜索 (Multi-match Query)

在多个字段中执行相同的搜索:

json 复制代码
GET /your_index/_search
{
  "query": {
    "multi_match": {
      "query": "搜索关键词",
      "fields": ["field1", "field2", "field3"]
    }
  }
}

5. 精确匹配 (Term Query)

用于精确值匹配,不会对查询文本进行分析:

json 复制代码
GET /your_index/_search
{
  "query": {
    "term": {
      "field_name": "精确值"
    }
  }
}

6. 术语集合查询 (Terms Query)

匹配指定字段中包含任意一个指定术语的文档:

json 复制代码
GET /your_index/_search
{
  "query": {
    "terms": {
      "field_name": ["值1", "值2", "值3"]
    }
  }
}

7. 范围查询 (Range Query)

查找指定范围内的值:

json 复制代码
GET /your_index/_search
{
  "query": {
    "range": {
      "field_name": {
        "gte": 10,
        "lte": 20
      }
    }
  }
}

8. 布尔查询 (Bool Query)

组合多个查询条件,支持 must(必须匹配)、should(应该匹配)、must_not(必须不匹配)、filter(必须匹配但不计算分数):

json 复制代码
GET /your_index/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "title": "Elasticsearch" } }
      ],
      "filter": [
        { "term": { "status": "published" } },
        { "range": { "publish_date": { "gte": "2023-01-01" } } }
      ],
      "should": [
        { "term": { "tags": "教程" } },
        { "term": { "tags": "指南" } }
      ],
      "minimum_should_match": 1
    }
  }
}

9. 通配符查询 (Wildcard Query)

使用 * 匹配任意字符序列,? 匹配任意单个字符:

json 复制代码
GET /your_index/_search
{
  "query": {
    "wildcard": {
      "field_name": "搜索*"
    }
  }
}

10. 前缀查询 (Prefix Query)

查找以指定前缀开头的术语:

json 复制代码
GET /your_index/_search
{
  "query": {
    "prefix": {
      "field_name": "前缀"
    }
  }
}

11. 模糊查询 (Fuzzy Query)

基于编辑距离进行模糊匹配:

json 复制代码
GET /your_index/_search
{
  "query": {
    "fuzzy": {
      "field_name": {
        "value": "搜索词",
        "fuzziness": "AUTO"
      }
    }
  }
}

三、搜索结果控制

1. 分页

json 复制代码
GET /your_index/_search
{
  "from": 0,
  "size": 10,
  "query": {
    "match_all": {}
  }
}

2. 排序

json 复制代码
GET /your_index/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    { "field1": { "order": "asc" } },
    { "field2": { "order": "desc" } }
  ]
}

3. 指定返回字段

json 复制代码
GET /your_index/_search
{
  "_source": ["field1", "field2"],
  "query": {
    "match_all": {}
  }
}

4. 高亮显示

json 复制代码
GET /your_index/_search
{
  "query": {
    "match": {
      "content": "搜索关键词"
    }
  },
  "highlight": {
    "fields": {
      "content": {}
    }
  }
}

四、聚合查询

Elasticsearch 提供了强大的聚合功能,用于数据分析:

1. 指标聚合

json 复制代码
GET /your_index/_search
{
  "size": 0,
  "aggs": {
    "avg_price": {
      "avg": { "field": "price" }
    },
    "max_price": {
      "max": { "field": "price" }
    }
  }
}

2. 分桶聚合

json 复制代码
GET /your_index/_search
{
  "size": 0,
  "aggs": {
    "group_by_category": {
      "terms": {
        "field": "category",
        "size": 10
      }
    }
  }
}

3. 组合聚合

json 复制代码
GET /your_index/_search
{
  "size": 0,
  "aggs": {
    "group_by_category": {
      "terms": {
        "field": "category"
      },
      "aggs": {
        "avg_price": {
          "avg": { "field": "price" }
        }
      }
    }
  }
}

五、性能优化建议

  1. 使用 filter 上下文:不计算分数,性能更好且可缓存
  2. 避免通配符开头的前缀查询 :如 *text,会导致性能问题
  3. 合理使用分页:深度分页(from + size > 10000)性能较差,考虑使用 search_after
  4. 选择合适的字段类型:text 用于全文搜索,keyword 用于精确匹配

六、总结

Elasticsearch 8.13.4 提供了丰富多样的搜索功能,从基础的全文搜索到复杂的聚合分析。掌握这些常用搜索操作,能够帮助您构建高效、灵活的搜索应用。

在实际应用中,建议根据具体场景选择合适的查询类型,并注意性能优化,以获得最佳的搜索体验。

希望本文能帮助您更好地理解和使用 Elasticsearch 的搜索功能!

相关推荐
ha_lydms1 小时前
MaxCompute中加密与解密函数
大数据·数据库·数据仓库·hadoop·dataworks·maxcompute·odps
文慧的科技江湖2 小时前
《从算力能源各玩各的到全套跑通:算电协同 + Token 全栈方案落地实战》
大数据·重卡充电桩系统·慧知重卡充电桩平台·慧知重卡充电桩系统
m0_466525294 小时前
数字认证参加第十四届用户体验大会,分享标准建设思路
大数据·人工智能·科技
康实训4 小时前
2026数字化AI智慧实训建设方案
大数据·人工智能·实训室·实训室建设
玫瑰互动GEO5 小时前
开发者学GEO优化:从算法基础到CoT推理链
大数据·人工智能·ai·geo·ai搜索·geo优化
liuhl09105 小时前
【无标题】
大数据·elasticsearch·搜索引擎
大嘴皮猴儿5 小时前
2026年9月行业洞察:适合亚马逊跨境卖家的免费自动图片翻译平台
大数据·人工智能·跨境电商ai跨境翻译网站哪个好·ai图片翻译网站推荐·ai图片翻译工具哪个好
天国梦6 小时前
2026年北京机器狗选型指南:从痛点、技术到实战的完整评测
大数据·人工智能
段一凡-华北理工大学6 小时前
大模型与智能体在工业的应用~系列文章07:多智能体协作:会分工的“AI 团队“
大数据·人工智能·设备巡检·智能体协作·工业智能体·高炉大模型
AI科技先锋报6 小时前
2026企业级Data Agent开发平台有哪些:主流DataAgent开发框架全景解析
大数据·人工智能