eslasticsearch多种常用搜索方式

本文介绍了Elasticsearch的两种主要搜索方式:Query String Search和Query DSL。在Query String Search中,展示了搜索所有商品和按名称及价格排序的示例。而在Query DSL部分,讲解了match_all查询、全文索引、短语搜索、term查询、查询过滤、范围查询、布尔查询以及聚合查询的用法,每个查询类型都有具体的实例演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Query String Search(‘Query String’方式的搜索)

1.搜索全部商品

GET /shop_index/productInfo/_search

返回结果:

{
   
  "took": 8,
  "timed_out": false,
  "_shards": {
   
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
   
    "total": 3,
    "max_score": 1,
    "hits": [
      {
   
        "_index": "shop_index",
        "_type": "productInfo",
        "_id": "2",
        "_score": 1,
        "_source": {
   
          "test": "test"
        }
      },
      {
   
        "_index": "shop_index",
        "_type": "productInfo",
        "_id": "zyWpRGkB8mgaHjxk0Hfo",
        "_score": 1,
        "_source": {
   
          "name": "HuaWei P20",
          "desc": "Expen but easy to use",
          "price": 5300,
          "producer": "HuaWei Producer",
          "tags": [
            "Expen",
            "Fast"
          ]
        }
      },
      {
   
        "_index": "shop_index",
        "_type": "productInfo",
        "_id": "1",
        "_score": 1,
        "_source": {
   
          "name": "HuaWei Mate8",
          "desc": "Cheap and easy to use",
          "price": 2500,
          "producer": "HuaWei Producer",
          "tags": [
            "Cheap",
            "Fast"
          ]
        }
      }
    ]
  }
}

字段解释:

  • took:耗费了几毫秒
  • timed_out:是否超时,这里是没有
  • _shards:数据被拆到了5个分片上,搜索时使用了5个分片,5个分片都成功地返回了数据,失败了0个,跳过了0个
  • hits.total:查询结果的数量,3个document
  • max_score:就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也越高
  • hits.hits:包含了匹配搜索的document的详细数据
  • _source:数据

2.搜索商品名称中包含HuaWei的商品,而且按照售价降序排序:

下面这种方法也是"Query String Search"的由来,因为search参数都是以http请求的query string来附带的.

GET /shop_index/productInfo/_search?q=name:HuaWei&sort=price:desc

返回结果:

{
   
  "took": 23,
  "timed_out": false,
  "_shards": {
   
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
   
    "total": 2,
    "max_score": null,
    "hits": [
      {
   
        "_index": "shop_index",
        "_type": "productInfo",
        "_id": "zyWpRGkB8mgaHjxk0Hfo",
        "_score": null,
        "_source": {
   
          "name": "HuaWei P20",
          "desc": "Expen but easy to use",
          "price": 5300,
          "producer": "HuaWei Producer",
          "tags": [
            "Expen",
            "Fast"
          ]
        },
        "sort": [
          
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值