大纲:
- 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":