查询结果解释
- took - 耗费多少毫秒
- time_out - 是否超时
- _shards - 数据分片情况
- hits.total - 查询结果的数量
- hits.max_score - document 对于一个 search 的相关度匹配分数,越相关分数越高
- hits.hits - 匹配 document 的详细数据
GET /index/type/_search
GET /ecommerce/product/_search
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": 1,
"_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
Domain Specified Language:特定领域的语言
GET /index/type/_search
{
"query": {
"match_all": {}
}
}
请求体放在
request body
里面 ,可以用 json 格式来构建各种复杂的语法,比query string search
强大
GET /ecommerce/product/_search
{
"query": {
"match_all": {}
}
}
查询商品名称中包含
yagao
的商品,而且按照售价降序排序
GET /ecommerce/product/_search
{
"query": {
"match": {
"name": "yagao"
}
},
"sort": [
{
"price": "desc"
}
]
}
当前总共3条商品数据,假设每页显示1条,现在显示第2页,所以只查出来第2个商品
GET /ecommerce/product/_search
{
"query":{
"match_all": {}
},
"from": 1,
"size": 1
}
指定只查询商品的 name、price 属性
GET /ecommerce/product/_search
{
"query": {
"match_all": {}
},
"_source": [
"name",
"price"
]
}
搜索商品名称包含 yaogao ,而且售价大于 25 元的商品
GET /ecommerce/product/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "yagao"
}
}
],
"filter": {
"range": {
"price": {
"gt": 25
}
}
}
}
}
}
- gt : 大于
- lt : 小于
- eq : 等于
- ne : 不等于
- gte : 大于等于
- lte : 小于等于
全文检索 - 会将输入的
关键字
拆解,去倒排索引里面一一匹配,只要匹配上任意一个拆解后的单词,就可以作为结果返回
PUT /ecommerce/product/4
{
"name": "special yagao",
"desc": "special meibai",
"price": 50,
"producer": "special yagao producer",
"tag": [
"meibai"
]
}
GET /ecommerce/product/_search
{
"query":{
"match": {
"producer":"yagao producer"
}
}
}
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1.2825179,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "4",
"_score": 1.2825179,
"_source": {
"name": "special yagao",
"desc": "special meibai",
"price": 50,
"producer": "special yagao producer",
"tag": [
"meibai"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "3",
"_score": 0.09037233,
"_source": {
"name": "zhonghua yagao",
"desc": "caoben zhiwu",
"price": 40,
"producer": "zhonghua producer",
"tags": [
"qingxin",
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "2",
"_score": 0.09037233,
"_source": {
"name": "jiajieshi yagao",
"desc": "youxiao fangzhu",
"price": 25,
"producer": "jiajieshi producer",
"tags": [
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": 0.09037233,
"_source": {
"name": "gaolujie yagao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"name"
]
}
}
]
}
}
当
yagao producer
作为查询条件时会被拆分成yagao
、producer
两个查询条件,但是查询结果的_score
会体现跟原始查询条件的匹配度
短语搜索 - 跟全文检索相反,搜索
关键字
必须在指定的字段文本中完全匹配
GET /ecommerce/product/_search
{
"query": {
"match_phrase": {
"producer": "yagao producer"
}
}
}
高亮搜索 - 被匹配的关键字将会在查询结果中高亮标注(默认
)
GET /ecommerce/product/_search
{
"query": {
"match": {
"producer": "producer"
}
},
"highlight": {
"fields": {
"producer": {}
}
}
}