Elasticsearch学习笔记(四):ES的搜索方式

本文介绍了Elasticsearch的多种搜索方式,包括query string search、query DSL、query filter、full-text search、phrase search、highlight search以及聚合分析。query string search适用于简单搜索,而query DSL更适合构建复杂的查询语法。此外,全文检索和短语搜索分别处理不同的搜索需求,高亮搜索能突出显示匹配部分。文章还详细探讨了不同场景下的聚合分析应用。
摘要由CSDN通过智能技术生成

大纲:

  • query string search
  • query DSL
  • query filter
  • full-text search
  • phrase search
  • highlight search
  • 聚合分析

1. query string search

 搜索全部商品:get /ecommerce/product/_search

{
  "took": 20,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "jiajieshi yagao",
          "desc": "youxiao fangzhu",
          "price": 25,
          "producer": "jiajieshi producer",
          "tags": [
            "fangzhu"
          ]
        }
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "chaojiban gaolujie yaogao",
          "desc": "gaoxiao meibai",
          "price": 30,
          "producer": "gaolujie producer",
          "tags": [
            "meibai",
            "fangzhu"
          ]
        }
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "zhonghua yagao",
          "desc": "caoben zhiwu",
          "price": 40,
          "producer": "zhonghua producer",
          "tags": [
            "qingxin"
          ]
        }
      }
    ]
  }
}

took:耗费了几毫秒

time_out:是否超时,这里没有

_shards:数据拆成了5个分片,所以对于搜索请求,会打到所有的primary shard(或是它的某个 replica shard也可以)

hits.total:结果数量,这里是三个document。

hits.max_score:score的含义,就是document对于一个search相关度的匹配分数,越相关,就越匹配,分数越高

hits.hits:包含匹配搜索的document数据 


query string search的由来,是因为search 参数都是以http请求的query string来附带的。

搜索商品中包含牙膏的数据,按照售价降序排序:get /ecommerce/produce/_search?q=name:yaogao&sort=price:desc

适用于临时在命令行使用的工具,比如curl,快速的发出请求,来检索想要的信息;但是如果查询的条件非常复杂,是很难去构建的,因此,在生产环境当中,是跟少去用的。

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": null,
    "hits": [
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "3",
        "_score": null,
        "_source": {
          "name": "zhonghua yagao",
          "desc": "caoben zhiwu",
          "price": 40,
          "producer": "zhonghua producer",
          "tags": [
            "qingxin"
          ]
        },
        "sort": [
          40
        ]
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "1",
        "_score": null,
        "_source": {
          "name": "chaojiban gaolujie yagao",
          "desc": "gaoxiao meibai",
          "price": 30,
          "producer": "gaolujie producer",
          "tags": [
            "meibai",
            "fangzhu"
          ]
        },
        "sort": [
          30
        ]
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "2",
        "_score": null,
        "_source": {
          "name": "jiajieshi yagao",
          "desc": "youxiao fangzhu",
          "price": 25,
          "producer": "jiajieshi producer",
          "tags": [
            "fangzhu"
          ]
        },
        "sort": [
          25
        ]
      }
    ]
  }
}

2. query DSL

DSL: domain specified language: 特定领域的语言

http request body,请求体,可以用json来构建复杂的查询语法,比较方便,比query string search强大很多。

查询所有的商品: 

get ecommerce/product/_search
{
  "query":{
    "match_all": {}
  }
}

查询名称包含yagao的商品,并价格降序

GET /ecommerce/product/_search
{
    "query": {
        "match": {
          "name": "yagao"
        }
    },
    "sort": [
      {
        "price": {
          "order": "desc"
        }
      }
    ]
}


{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": null,
    "hits": [
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "3",
        "_score": null,
        "_source": {
          "name": "zhonghua yagao",
          "desc": 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值