es学习三之 es中查询方式

一 es中查询的方式

1.1 query string search

1.1.1 比如搜索全部商品:

GET /ecommerce/product/_search

结果如下:

{
"took": 4,
"timed_out": false,
"_shards": {
 "total": 5,
 "successful": 5,
 "failed": 0
},
"hits": {
 "total": 4,
 "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": "4",
     "_score": 1,
     "_source": {
       "name": "special yagao",
       "desc": "special meibai",
       "price": 50,
       "producer": "special yagao producer",
       "tags": "meibai"
     }
   },
   {
     "_index": "ecommerce",
     "_type": "product",
     "_id": "1",
     "_score": 1,
     "_source": {
       "name": "gaolujie yagao",
       "desc": " meibai,fangzhu",
       "price": 25,
       "producer": "gaolujie producer",
       "tags": [
         "meibai",
         "fangzhu"
       ]
     }
   },
   {
     "_index": "ecommerce",
     "_type": "product",
     "_id": "3",
     "_score": 1,
     "_source": {
       "name": "zhonghua yagao",
       "desc": " caoben jinghua",
       "price": 40,
       "producer": "zhonghua producer",
       "tags": [
         "qingxin"
       ]
     }
   }
 ]
}
}

其中一些关键字段意思:

took:耗费了几毫秒
timed_out:是否超时,这里是没有
_shards:数据拆成了5个分片,所以对于搜索请求,会打到所有的primary shard(或者是它的某个replica shard也可以)
hits.total:查询结果的数量,3个document
hits.max_score:score的含义,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高
hits.hits:包含了匹配搜索的document的详细数据

1.2 query DSL(特定领域语言)

2.2.1 举例查询所有产品:

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

举例查询名称包含yagao的商品,同时按照价格降序排序

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

1.2.2举例 查询第二个产品

GET /ecommerce/product/_search
{
"query": { "match_all": {} },
"from": 1,
"size": 1
}

1.2.3 举例 指定要查询出来的名称和价格就可以

GET /ecommerce/product/_search
{
"query": { "match_all": {} },
"_source": ["name", "price"]
}

1.3 query filter (查询过滤)

2.3.1举例搜索商品名称包含yagao,并且售价大于25元的商品

GET /ecommerce/product/_search
{
 "query" : {
     "bool" : {
         "must" : {
             "match" : {
                 "name" : "yagao" 
             }
         },
         "filter" : {
             "range" : {
                 "price" : { "gt" : 25 } 
             }
         }
     }
 }
}

1.4 full text search(全文本检索)

2.4.1 举例查询所有 producer字段中包含 yagao producer

GET /ecommerce/product/_search
{
 "query" : {
     "match" : {
         "producer" : "yagao producer"
     }
 }
}

结果如下

{
"took": 32,
"timed_out": false,
"_shards": {
 "total": 5,
 "successful": 5,
 "failed": 0
},
"hits": {
 "total": 4,
 "max_score": 0.70293105,
 "hits": [
   {
     "_index": "ecommerce",
     "_type": "product",
     "_id": "4",
     "_score": 0.70293105,
     "_source": {
       "name": "special yagao",
       "desc": "special meibai",
       "price": 50,
       "producer": "special yagao producer",
       "tags": "meibai"
     }
   },
   {
     "_index": "ecommerce",
     "_type": "product",
     "_id": "1",
     "_score": 0.25811607,
     "_source": {
       "name": "gaolujie yagao",
       "desc": " meibai,fangzhu",
       "price": 25,
       "producer": "gaolujie producer",
       "tags": [
         "meibai",
         "fangzhu"
       ]
     }
   },
   {
     "_index": "ecommerce",
     "_type": "product",
     "_id": "3",
     "_score": 0.25811607,
     "_source": {
       "name": "zhonghua yagao",
       "desc": " caoben jinghua",
       "price": 40,
       "producer": "zhonghua producer",
       "tags": [
         "qingxin"
       ]
     }
   },
   {
     "_index": "ecommerce",
     "_type": "product",
     "_id": "2",
     "_score": 0.1805489,
     "_source": {
       "name": "jiajieshi yagao",
       "desc": " youxiao fangzhu",
       "price": 25,
       "producer": "jiajieshi producer",
       "tags": [
         "fangzhu"
       ]
     }
   }
 ]
}
}

为什么有4条记录而不是没有,首先 producer这个字段为被拆解,建立倒排索引,id为4 的拆解为 (special ,yagao,producer),id为3的被拆解为( zhonghua ,yagao),id为2的拆解为(jiajieshi ,yagao),id为1的被拆解为(gaolujie ,yagao),然后 关键字(yagao producer)被拆解成yagao和producer,其中id为4的匹配 yagao和 producer,id为3的匹配 yagao,id为2的匹配yagao,id为1的yagao. 关于相关度大小计算不讨论。

1.5 phrase search(短语搜索)

跟全文检索相对应,相反,全文检索会将输入的搜索串拆解开来,去倒排索引里面去一一匹配,只要能匹配上任意一个拆解后的单词,就可以作为结果返回
phrase search,要求输入的搜索串,必须在指定的字段文本中,完全包含一模一样的,才可以算匹配,才能作为结果返回

举例:

GET /ecommerce/product/_search
{
"query" : {
  "match_phrase" : {
      "producer" : "yagao producer"
  }
}
}

结果如下:

{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.70293105,
"hits": [
{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "4",
  "_score": 0.70293105,
  "_source": {
    "name": "special yagao",
    "desc": "special meibai",
    "price": 50,
    "producer": "special yagao producer",
    "tags": "meibai"
  }
}
]
}
}


1.6 highlight search(高亮搜索结果)

举例:

GET /ecommerce/product/_search
{
    "query" : {
        "match" : {
            "producer" : "producer"
        }
    },
    "highlight": {
        "fields" : {
            "producer" : {}
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值