(03)Elasticsearch多种搜索方式

1、query string search
//1.获取电子产品下的所有数据  
GET product/electronic/_search
结果
{
  "took": 7,    //耗费了几毫秒
  "timed_out": false,   //是否超时
  "_shards": {
    "total": 5,   //有5个shart 所以对于搜索请求,会打到所有的primary shard(或者是它的某个replica shard也可以)
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,   //查出来数据条数
    "max_score": 1,  //最大分值
    "hits": [
      {
        "_index": "product",
        "_type": "electronic",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "huawei",
          "price": 3000,
          "desc": "chine"
        }
      }
    ]
  }
}

//2. search参数都是以http请求的query string来附带的
搜索电子商品名称中包含huawei的商品,而且按照售价降序排序:
GET product/electronic/_search?q=name:huawei&sort=price:desc
{
  "took": 19,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_index": "product",
        "_type": "electronic",
        "_id": "2",
        "_score": null,
        "_source": {
          "name": "huawei mate 20 pro",
          "price": 4000,
          "desc": "chine  2000wan"
        },
        "sort": [
          4000
        ]
      },
      {
        "_index": "product",
        "_type": "electronic",
        "_id": "1",
        "_score": null,
        "_source": {
          "name": "huawei",
          "price": 3000,
          "desc": "chine"
        },
        "sort": [
          3000
        ]
      }
    ]
  }
}

2、query DSL
DSL:Domain Specified Language,特定领域的语言
使用http request body 请求体,可以用json的格式来构建查询语法,可以构建各种复杂的语法。
1.查询所有的商品
GET /product/electronic/_search
{
  "query":{"match_all": {}}  //查询请求,匹配全部
}
2.搜索电子商品名称中包含huawei的商品,而且按照售价降序排序:
GET product/electronic/_search
{
  "query": {
    "match": {     //按关键字匹配
      "name": "huawei"
    }
  },
  "sort":{    //排序
    "price":"desc"   //按价格倒排
  }
  
}
3.分页查询商品
GET /product/electronic/_search
{
  "query": {"match_all": {}}, //查询全部
  "from": 0,			// 从第一条开始
  "size": 1				// 查询一条
}
3、query filter

过滤查询,过滤掉不想要的数据

查询电子产品中包括huawei,而且价格大于3000的商品
GET /product/electronic/_search
{
  "query": {  //查询请求
    "bool": {  //布尔查询允许我们利用布尔逻辑将较小的查询组合成较大的查询。
      "must": { //必须要查的
        "match": {
        "name": "huawei"
       }
     } , 
      "filter": {  //过滤
        "range": {  //范围
          "price": { //过滤字段
            "gt": 3000  //过滤条件
          }
        }
      }
    }
  }
}
4、full-text search
全文检索
搜索name 是 huawei mate 20 pro的 商品
GET /product/electronic/_search
{
  "query": {
    "match": {
      "name": "huawei mate 20 pro"
    }
  }
}	
结果
{
  "took": 25,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1.1507283,
    "hits": [
      {
        "_index": "product",
        "_type": "electronic",
        "_id": "2",
        "_score": 1.1507283,
        "_source": {
          "name": "huawei mate 20 pro",
          "price": 4000,
          "desc": "chine  2000wan"
        }
      },
      {
        "_index": "product",
        "_type": "electronic",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "name": "huawei",
          "price": 3000,
          "desc": "chine"
        }
      }
    ]
  }
}

结果中包含了 huawei mate 20 pro的商品和huawei的商品,像这样的查询就是全文检索

5、phrase search
精确检索,短语检索
GET /product/electronic/_search
{
  "query": {
    "match_phrase": {
      "name": "huawei mate 20 pro"
    }
  }
}
结果
{
  "took": 34,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.1507283,
    "hits": [
      {
        "_index": "product",
        "_type": "electronic",
        "_id": "2",
        "_score": 1.1507283,
        "_source": {
          "name": "huawei mate 20 pro",
          "price": 4000,
          "desc": "chine  2000wan"
        }
      }
    ]
  }
}

像这样查询结果只返回与查询条件完全相同的检索,称为精确检索

6、highlight search
高亮查询
GET /product/electronic/_search
{
 "query" : {
        "match" : {
            "name" : "huawei"
        }
    },
    "highlight": {
        "fields" : {
            "name" : {}
        }
    }
}
结果
{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "product",
        "_type": "electronic",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "name": "huawei mate 20 pro",
          "price": 4000,
          "desc": "chine  2000wan"
        },
        "highlight": {
          "name": [
            "<em>huawei</em> mate 20 pro"  //返回的数据中关键词高亮显示
          ]
        }
      }
    ]
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值