Elasticsearch RESTful API入门:全文搜索实战

发布于:2025-07-10 ⋅ 阅读:(20) ⋅ 点赞:(0)

Elasticsearch RESTful API入门:全文搜索实战

全文搜索是Elasticsearch的核心能力,本文通过10+个实战示例,手把手教你掌握match、multi_match等全文搜索技术,并实现高亮显示等高级功能。

一、全文搜索核心概念

1. 什么是全文搜索?

  • 在文本内容中搜索语义相关的结果(非精确匹配)
  • 示例:搜索"苹果手机"也能匹配"iPhone 13 Pro"
  • Elasticsearch通过分词+倒排索引实现高性能搜索

2. 全文搜索流程

原始文本
分词器处理
生成倒排索引
搜索关键词
分词处理
索引匹配
相关性排序

二、环境准备

1. 创建书籍索引

PUT /books
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "ik_max_word"  # 使用中文分词器
      },
      "author": {"type": "keyword"},
      "content": {
        "type": "text",
        "analyzer": "ik_smart"
      },
      "publish_date": {"type": "date"},
      "rating": {"type": "float"}
    }
  }
}

2. 添加测试数据

POST /books/_bulk
{"index":{}}
{"title":"深入理解Elasticsearch","author":"张明","content":"本书全面讲解ES核心原理与高级功能","publish_date":"2023-01-15","rating":4.5}
{"index":{}}
{"title":"大数据搜索技术实战","author":"李华","content":"基于ElasticSearch的搜索算法与性能优化","publish_date":"2022-11-20","rating":4.2}
{"index":{}}
{"title":"分布式系统设计","author":"王伟","content":"Elasticsearch作为分布式存储的典型应用","publish_date":"2023-03-08","rating":4.7}

三、基础全文搜索实战

1. match查询 - 基础全文搜索

GET /books/_search
{
  "query": {
    "match": {
      "content": "搜索技术"
    }
  }
}
  • 分词效果: “搜索技术” → [“搜索”, “技术”]

  • 匹配包含任意分词的文档(OR逻辑)

2. 设置匹配逻辑

{
  "query": {
    "match": {
      "content": {
        "query": "分布式存储",
        "operator": "and"  // 必须包含所有分词
      }
    }
  }
}

3. 多字段搜索 - multi_match

{
  "query": {
    "multi_match": {
      "query": "性能优化",
      "fields": ["title", "content"]  // 同时搜索两个字段
    }
  }
}

四、高级全文搜索技巧

1. 短语搜索 - match_phrase

{
  "query": {
    "match_phrase": {
      "content": {
        "query": "典型应用",
        "slop": 2  // 允许中间间隔2个词
      }
    }
  }
}

2. 模糊搜索 - fuzziness

{
  "query": {
    "match": {
      "title": {
        "query": "Elasticsarch",  // 拼写错误
        "fuzziness": "AUTO"       // 自动容错
      }
    }
  }
}

3. 同义词扩展

PUT /books/_settings
{
  "analysis": {
    "filter": {
      "my_synonyms": {
        "type": "synonym",
        "synonyms": [
          "es, elasticsearch",
          "大数据 => big data"
        ]
      }
    },
    "analyzer": {
      "my_analyzer": {
        "tokenizer": "ik_max_word",
        "filter": ["my_synonyms"]
      }
    }
  }
}

搜索"es"可匹配"elasticsearch"

五、搜索结果优化

1. 高亮显示

{
  "query": {
    "match": {"content": "分布式"}
  },
  "highlight": {
    "fields": {
      "content": {
        "pre_tags": ["<strong>"],
        "post_tags": ["</strong>"]
      }
    }
  }
}

返回结果片段:

"highlight": {
  "content": [
    "Elasticsearch作为<strong>分布式</strong>存储的典型应用"
  ]
}

2. 结果排序

{
  "query": {...},
  "sort": [
    {
      "_score": {"order": "desc"},  // 按相关性降序
      "rating": {"order": "desc"}   // 再按评分降序
    }
  ]
}

3. 字段权重设置

{
  "query": {
    "multi_match": {
      "query": "搜索算法",
      "fields": [
        "title^3",  // title权重为3
        "content"   // content权重默认为1
      ] 
    }
  }
}

六、复杂场景实战

1. 组合搜索 - bool + fulltext

{
  "query": {
    "bool": {
      "must": [
        {"match": {"content": "技术"}}
      ],
      "should": [
        {"match_phrase": {"title": "实战"}}
      ],
      "filter": [
        {"range": {"rating": {"gte": 4.0}}}
      ]
    }
  }
}

2. 跨索引搜索

GET /books,articles/_search
{
  "query": {
    "multi_match": {
      "query": "分布式系统",
      "fields": ["title", "content"]
    }
  }
}

3. 搜索建议 - suggest

{
  "suggest": {
    "title_suggest": {
      "text": "elastc",
      "term": {
        "field": "title",
        "suggest_mode": "always"
      }
    }
  }
}

七、性能优化指南

1. 避免全字段搜索

// 不推荐
{"match": {"_all": "搜索技术"}}

// 推荐:明确指定字段
{"match": {"content": "搜索技术"}}

2. 合理使用分页

// 深度分页性能差
{"from": 10000, "size": 10}

// 替代方案:search_after
{
  "size": 10,
  "sort": [{"_score": "desc"}, {"title.keyword": "asc"}],
  "search_after": [0.8, "深入理解Elasticsearch"]
}

3. 索引优化建议

PUT /books/_settings
{
  "index": {
    "refresh_interval": "30s",  // 降低刷新频率
    "number_of_replicas": 1
  }
}

八、调试技巧

1. 分析查询语句

GET /books/_validate/query?explain
{
  "query": {
    "match": {"title": "搜素引型"}
  }
}

2. 分词测试

GET /books/_analyze
{
  "field": "content",
  "text": "Elasticsearch搜索性能优化"
}

九、常见问题排查

问题现象 可能原因 解决方案
搜索无结果 分词器不匹配 检查mapping和analyzer设置
搜索结果不相关 未设置合理权重 使用^提升关键字段权重
高亮显示失效 字段未存储原始内容 确保字段store=true
搜索响应慢 分页过深或索引过大 使用search_after替代from/size

下集预告《Elasticsearch RESTful API入门:全文搜索实战(Java版)》


网站公告

今日签到

点亮在社区的每一天
去签到