声明:本文仅仅只是记个笔记
搜索方式
took :耗费了几毫秒
timed_out:是否超时,这里没有
_shareds: 数据拆成1哥分片,所以对于搜索请求,会打造所有的primary shard (或者是它的某个replica shard也可以)
hits.total :查询结果的数量,这里有 3 个 document
max_score : score的含义就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也越高
hist.hits:包含了匹配搜索的document的详细数据
结果:
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "jiajieshi yagao",
"desc" : "youxiao fangzhu",
"price" : 25,
"producer" : "jiajieshi producer",
"tags" : [
"fangzhu"
]
}
},
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "zhonghua yagao",
"desc" : "caoben zhiwu",
"price" : 40,
"producer" : "zhonghua producer",
"tags" : [
"qingxin"
]
}
},
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"producer" : "gaolujie producer",
"tags" : [
"meibai",
"fangzhu"
]
}
}
]
}
}
搜索商品名中包含yagao的商品,而且按照售价降序排序:GET /ecommerce/product/_search?q=name:yagao&sort=price:desc
适用于临时的在命令行使用的一些工具,比如curl ,快速的发出请求,来检索想要的信息;但是如果查询请求很复杂,是很难去构建在生产环境中,几乎很好使用 query string search
DSL : domain Specific Language 特定领域的语言
1) 查询所有商品
GET /ecommerce/product/_search
{
"query": {
"match_all": {}
}
}
2) 按条件查找
GET /ecommerce/product/_search
{
"query": {
"match": {
"name":"yagao"
}
},
"sort":[
{"price":"desc"}
]
}
3)分页查询
先查询所有,再看显示哪条
from :从哪条开始
size : 显示几条
GET /ecommerce/product/_search
{
"query": {
"match_all": {}
},
"from": 1,
"size": 1
}
4)指定要查询出来的商品名称和价格
GET /ecommerce/product/_search
{
"query": {
"match_all": {}
},
"_source": ["name","price"]
}
搜索商品名称包含yagao,而且售价大于25的商品
GET /ecommerce/product/_search
{
"query": {
"bool": {
"must": {
"match":{
"name":"yagao"
}
},
"filter": {
"range": {
"price": {
"gt": 25
}
}
}
}
}
}
producer 字段会 先被拆除,后建立倒排索引
GET /ecommerce/product/_search
{
"query": {
"match": {
"producer": "yagao producer"
}
}
}
结果:
{
"took" : 11,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.1522135,
"hits" : [
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "4",
"_score" : 1.1522135,
"_source" : {
"name" : "special yagao",
"desc" : "special meibai",
"price" : 50,
"producer" : "special yagao producer",
"tags" : [
"meibai"
]
}
},
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "2",
"_score" : 0.110377684,
"_source" : {
"name" : "jiajieshi yagao",
"desc" : "youxiao fangzhu",
"price" : 25,
"producer" : "jiajieshi producer",
"tags" : [
"fangzhu"
]
}
},
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "3",
"_score" : 0.110377684,
"_source" : {
"name" : "zhonghua yagao",
"desc" : "caoben zhiwu",
"price" : 40,
"producer" : "zhonghua producer",
"tags" : [
"qingxin"
]
}
},
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "1",
"_score" : 0.110377684,
"_source" : {
"name" : "gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"producer" : "gaolujie producer",
"tags" : [
"meibai",
"fangzhu"
]
}
}
]
}
}
跟全文搜索相对应,相反,全文检索会将输入的搜索串拆解开来,去倒排索引里面去一一匹配,只要能匹配上任意一个拆解后的单词,就可以作为结果返回,phrase search 要求输入的搜索串,必须在指定的字段文本中,完全包含一模一样的,才可以算匹配,才能作为结果返回。
GET /ecommerce/product/_search
{
"query": {
"match_phrase": {
"producer": "yagao producer"
}
}
}
高亮凡是匹配的词在结果中都标红
GET /ecommerce/product/_search
{
"query": {
"match": {
"producer": "producer"
}
},
"highlight": {
"fields": {
"producer":{}
}
}
}
结果:
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 153,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 0.110377684,
"hits" : [
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "2",
"_score" : 0.110377684,
"_source" : {
"name" : "jiajieshi yagao",
"desc" : "youxiao fangzhu",
"price" : 25,
"producer" : "jiajieshi producer",
"tags" : [
"fangzhu"
]
},
"highlight" : {
"producer" : [
"jiajieshi producer"
]
}
},
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "3",
"_score" : 0.110377684,
"_source" : {
"name" : "zhonghua yagao",
"desc" : "caoben zhiwu",
"price" : 40,
"producer" : "zhonghua producer",
"tags" : [
"qingxin"
]
},
"highlight" : {
"producer" : [
"zhonghua producer"
]
}
},
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "1",
"_score" : 0.110377684,
"_source" : {
"name" : "gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"producer" : "gaolujie producer",
"tags" : [
"meibai",
"fangzhu"
]
},
"highlight" : {
"producer" : [
"gaolujie producer"
]
}
},
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "4",
"_score" : 0.09271725,
"_source" : {
"name" : "special yagao",
"desc" : "special meibai",
"price" : 50,
"producer" : "special yagao producer",
"tags" : [
"meibai"
]
},
"highlight" : {
"producer" : [
"special yagao producer"
]
}
}
]
}
}