ElasticSearch 实战:es的多种搜索方式

Elasticsearch 提供了多种搜索方式以满足不同场景下的查询需求。以下是一些常用的搜索类型及其实战演示,使用 curl 命令与 Elasticsearch 服务器交互(假设服务器运行在 http://localhost:9200)。

1. 全文搜索(Full-text search)

Match Query

使用 match 查询在一个或多个字段中搜索与指定词语或短语相匹配的文档。

curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "content": "Elasticsearch tutorial"
    }
  }
}'
Multi-match Query

multi_match 查询可以在多个字段上执行相同的标准全文查询。

curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "multi_match": {
      "query": "Elasticsearch introduction",
      "fields": ["title", "description", "tags"]
    }
  }
}'

2. 精确匹配(Exact match)

Term Query

term 查询用于查找字段中精确值匹配的文档。

curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "term": {
      "status": "published"
    }
  }
}'
Terms Query

terms 查询允许一次性指定多个精确值,查找与这些值之一匹配的文档。

curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "terms": {
      "category": ["electronics", "books", "clothing"]
    }
  }
}'

3. 范围查询(Range queries)

Range Query

range 查询用于查找字段值在指定区间内的文档。

curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "range": {
      "price": {
        "gte": 50,
        "lte": 100
      }
    }
  }
}'

4. 模糊搜索与通配符查询

Fuzzy Query

fuzzy 查询允许对某个词进行模糊匹配,容忍拼写错误或其他微小差异。

curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "fuzzy": {
      "name": {
        "value": "elastisearch",
        "fuzziness": "AUTO"
      }
    }
  }
}'
Wildcard Query

wildcard 查询支持使用通配符(*?)进行匹配。

curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "wildcard": {
      "product_name": "elec*"
    }
  }
}'

5. 组合查询(Compound queries)

Bool Query

bool 查询用于组合多个条件,支持 must(必须满足)、should(至少一个满足)、must_not(必须不满足)子句。

curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "category": "electronics" } },
        { "range": { "price": { "gte": 100 } } }
      ],
      "must_not": { "term": { "brand": "outdated_brand" } }
    }
  }
}'

6. 地理空间搜索

Geo-distance Query

查找距离某个地理位置一定范围内的文档。

curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "geo_distance": {
      "distance": "10km",
      "location": {
        "lat": 40.7128,
        "lon": -74.0060
      }
    }
  }
}'

7. 深度分页与滚动搜索

对于大量数据的分页查询,可以使用 fromsize 参数进行常规分页,或者使用 scroll API 进行滚动搜索以避免深度分页的性能问题。

Regular Pagination
curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
  "from": 50,
  "size": 20,
  "query": {
    "match_all": {}
  }
}'
Scroll Search
# 初始化滚动上下文
curl -X GET "http://localhost:9200/my_index/_search?scroll=1m" -H 'Content-Type: application/json' -d'
{
  "size": 100,
  "query": {
    "match_all": {}
  }
}'

# 使用返回的 `_scroll_id` 和 `scroll` 参数继续获取下一批数据
curl -X GET "http://localhost:9200/_search/scroll?scroll=1m&scroll_id=<scroll_id>"

以上只是 Elasticsearch 支持的多种搜索方式的一部分示例。实际应用中,可以根据具体业务需求灵活组合使用这些查询类型,并结合其他高级特性如排序、高亮、聚合等,以构建复杂的搜索和数据分析请求。同样的查询操作也可以通过 Kibana Dev Tools Console 或官方提供的各种语言客户端进行。

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值