es的六种搜索方式
- query string search
GET /ecommerce/product/_search //查询所有数据
{
"took": 4,//耗费几毫秒
"timed_out": false,//是否超时
"_shards": {//数据拆分成5个分片,对所有请求都会打到所有primary shared(或者是它的某个replica shared也可以)
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,//条数
"max_score": 1,//查询匹配度
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "2",
"_score": 1,
"_source": {
"name": "jiajieshi yagao",
"desc": "jiajieshi meibai",
"price": 30,
"producer": "jiajieshi producer",
"tags": [
"meibai",
"fangzhu"
]
}
}
]
}
}
GET /ecommerce/product/_search?q=name:yagao&sort=price:desc
query DSL
- DSL:Domain Specified Language:特定领域的语言
- http request body:请求体,用json格式构建查询语法
GET /ecommerce/product/_search
{
"query": {
"match": {
"name": "yagao" //查询包含单词
}
},
"_source": ["name","price"],//不写查询所有字段
"sort": [
{
"price": {
"order": "desc"//倒序排序
}
}
],
"from": 0,//分页
"size": 1
}
query filter
GET /ecommerce/product/_search
{
"query": {
"bool": {//多个条件
"must": [
{
"match": {
"name": "yagao"
}
}
],
"filter": {//过滤条件
"range": {
"price": {
"gte": 10,
"lte": 40
}
}
}
}
}
}
- full-text search
GET /ecommerce/product/_search
{
"query": {
"match": {
"producer": "lishi producer"//会查包含这两个单词的所有数据
}
}
}
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0.51623213,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "3",
"_score": 0.51623213,//匹配度最高
"_source": {
"name": "lishi yagao",
"desc": "lishi meibai",
"price": 50,
"producer": "lishi producer",
"tags": [
"meibai",
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": 0.25811607,
"_source": {
"name": "jiaqiangban gaolujie yagao2",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "2",
"_score": 0.1805489,
"_source": {
"name": "jiajieshi yagao",
"desc": "jiajieshi meibai",
"price": 40,
"producer": "jiajieshi producer",
"tags": [
"meibai",
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "4",
"_score": 0.14638957,
"_source": {
"name": "special yaogao",
"desc": "special meibai",
"price": 50,
"producer": "special yagao producer",
"tags": [
"meibai"
]
}
}
]
}
}
- phrase search
必须包含一模一样的串,才会返回(包含短语的意思)
GET /ecommerce/product/_search
{
"query": {
"match_phrase": {
"producer": "yagao producer"
}
}
}
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.70293105,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "4",
"_score": 0.70293105,
"_source": {
"name": "special yaogao",
"desc": "special meibai",
"price": 50,
"producer": "special yagao producer",
"tags": [
"meibai"
]
}
}
]
}
}
- highlight search
- 查询到的结果高亮
GET /ecommerce/product/_search
{
"query": {
"match_phrase": {
"producer": "gaolujie producer"
}
},
"highlight": {
"fields": {
"producer": {}
}
}
}
{
"took": 18,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.51623213,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": 0.51623213,
"_source": {
"name": "jiaqiangban gaolujie yagao2",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
},
"highlight": {
"producer": [
"<em>gaolujie</em> <em>producer</em>"
]
}
}
]
}
}
计算每个tag下的商品数量
- 将文本field的fielddata属性设置为true
PUT /ecommerce/_mapping/product
{
"properties": {
"tags":{
"type": "text",
"fielddata": true
}
}
}
- 再运行下面才能查出来
GET /ecommerce/product/_search
{
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
}
}
}
}
"aggregations": {
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "fangzhu",
"doc_count": 3
},
{
"key": "meibai",
"doc_count": 3
},
{
"key": "suibian",
"doc_count": 1
}
]
}
}
对名称中包含yagao的商品,计算每个tag下的商品数量
GET /ecommerce/product/_search
{
"query": {
"match": {
"name": "yagao"
}
},
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
}
}
}
}
先分组,再计算每组的平均值,计算每个tag下的商品的平均价格
GET /ecommerce/product/_search
{
"size": 0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
对上述结果按平均价格排序
GET /ecommerce/product/_search
{
"size": 0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags",
"order": {
"avg_price": "desc"
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
按照指定的价格范围区间进行分组,然后在每组内再按照tag进行分组,最后再计算每组的平均价格
GET /ecommerce/product/_search
{
"size": 0,
"aggs": {
"group_by_price": {
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 20
},
{
"from": 20,
"to": 40
},
{
"from": 40,
"to": 60
}
]
},
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}