一、Query String Search(‘Query String’方式的搜索)
1.搜索全部商品
GET /shop_index/productInfo/_search
返回结果:
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "shop_index",
"_type": "productInfo",
"_id": "2",
"_score": 1,
"_source": {
"test": "test"
}
},
{
"_index": "shop_index",
"_type": "productInfo",
"_id": "zyWpRGkB8mgaHjxk0Hfo",
"_score": 1,
"_source": {
"name": "HuaWei P20",
"desc": "Expen but easy to use",
"price": 5300,
"producer": "HuaWei Producer",
"tags": [
"Expen",
"Fast"
]
}
},
{
"_index": "shop_index",
"_type": "productInfo",
"_id": "1",
"_score": 1,
"_source": {
"name": "HuaWei Mate8",
"desc": "Cheap and easy to use",
"price": 2500,
"producer": "HuaWei Producer",
"tags": [
"Cheap",
"Fast"
]
}
}
]
}
}
字段解释:
- took:耗费了几毫秒
- timed_out:是否超时,这里是没有
- _shards:数据被拆到了5个分片上,搜索时使用了5个分片,5个分片都成功地返回了数据,失败了0个,跳过了0个
- hits.total:查询结果的数量,3个document
- max_score:就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也越高
- hits.hits:包含了匹配搜索的document的详细数据
- _source:数据
2.搜索商品名称中包含HuaWei的商品,而且按照售价降序排序:
下面这种方法也是"Query String Search"的由来,因为search参数都是以http请求的query string来附带的.
GET /shop_index/productInfo/_search?q=name:HuaWei&sort=price:desc
返回结果:
{
"took": 23,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": null,
"hits": [
{
"_index": "shop_index",
"_type": "productInfo",
"_id": "zyWpRGkB8mgaHjxk0Hfo",
"_score": null,
"_source": {
"name": "HuaWei P20",
"desc": "Expen but easy to use",
"price": 5300,
"producer": "HuaWei Producer",
"tags": [
"Expen",
"Fast"
]
},
"sort": [