Search API和Aggregations
搜索方式
1.query string search 2.query DSL 3.query filter 4.full-text search 5.phrase search 6.highlight search
Aggregations
7.嵌套聚合 下钻分析 聚合分析
1.query string search
GET /sport-shop-item-info/public/_search
GET /sport-shop-item-info/public/_search?q=shop_id:2896280279
2.query DSL
GET /sport-shop-item-info/public/_search
{
"query": {
"match_all": {}
}
}
查询所有标题含有“运动鞋”的商品,并按照价格进行降序排序
GET /sport-shop-item-info/public/_search
{
"size" : 10,
"query": {
"match": {
"title": "运动鞋"
}
},
"sort": [
{
"sale_price_a": {
"order": "desc"
}
}
]
}
分页
GET /sport-shop-item-info/public/_search
{ "from": 0,
"size" : 10,
"query": {
"match": {
"title": "运动鞋"
}
},
"sort": [
{
"sale_price_a": {
"order": "desc"
}
}
]
}
3.query filter
格式
GET /sport-shop-item-info/public/_search
{
"query": {
"bool": {
//多个查询条件,也可以嵌套bool
}
}
}
例子
GET /sport-shop-item-info/public/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"shop_id": {
"value": "3362038456"
}
}
},
{
"term": {
"platform_id": {
"value": "1"
}
}
},
{
"match": {
"title": "运动鞋"
}
}
]
}
}
}
4.full-text search
producer这个字段,会先被拆解,建立倒排索引
special 4
yagao 4
producer 1,2,3,4
gaolujie 1
zhognhua 3
jiajieshi 2
PUT /ecommerce/product/4
{
"name" : "special yagao",
"desc" : "texiao fangzhu",
"price" : 50,
"producer" : "yagao producer",
"tags" : ["fangzhu"]
}
GET /ecommerce/product/_search
{
"query": {
"match": {
"producer": "yagao producer"
}
}
}
5.phrase search
跟全文检索相对应,相反,全文检索会将输入的搜索串拆解开来,去倒排索引里面去一一匹配,只要能匹配上任意一个拆解后的单词,就可以作为结果返回 phrase search,要求输入的搜索串,必须在指定的字段文本中,完全包含一模一样的,才可以算匹配,才能作为结果返回
GET /ecommerce/product/_search
{
"query": {
"match_phrase": {
"producer": "yagao producer"
}
}
}
{
"took": 19,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.7854939,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "4",
"_score": 0.7854939,
"_source": {
"name": "special yagao",
"desc": "texiao fangzhu",
"price": 50,
"producer": "yagao producer",
"tags": [
"fangzhu"
]
}
}
]
}
}
6.highlight search
高亮搜索结果
GET /ecommerce/product/_search
{
"query": {
"match": {
"producer": "producer"
}
},
"highlight": {
"fields": {
"producer": {}
}
}
}
结果如下
{
"took": 42,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0.25811607,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": 0.25811607,
"_source": {
"name": "gaolujie yagao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
},
"highlight": {
"producer": [
"gaolujie <em>producer</em>"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "3",
"_score": 0.25811607,
"_source": {
"name": "zhonghua yagao",
"desc": "caoben zhiwu",
"price": 40,
"producer": "zhonghua producer",
"tags": [
"qinxin"
]
},
"highlight": {
"producer": [
"zhonghua <em>producer</em>"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "2",
"_score": 0.16358379,
"_source": {
"name": "jiajieshi yagao",
"desc": "youxiao fangzhu",
"price": 25,
"producer": "jiajieshi producer",
"tags": [
"fangzhu"
]
},
"highlight": {
"producer": [
"jiajieshi <em>producer</em>"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "4",
"_score": 0.16358379,
"_source": {
"name": "special yagao",
"desc": "texiao fangzhu",
"price": 50,
"producer": "yagao producer",
"tags": [
"fangzhu"
]
},
"highlight": {
"producer": [
"yagao <em>producer</em>"
]
}
}
]
}
}
7.嵌套聚合 下钻分析 聚合分析
size: 0 设置为对进行分组的数据不显示出来
GET /sport-shop-item-info/public/_search
{ "size": 0,
"query": {
"bool": {
"must": [
{
"term": {
"shop_id": {
"value": "3362038456"
}
}
},
{
"term": {
"platform_id": {
"value": "1"
}
}
},
{
"range": {
"crawl_time": {
"from": "2017-10-01",
"to": "2017-10-31",
"include_upper": true,
"include_lower": true
}
}
}
]
}
},
"aggs":{
"groupByItemId" : {
"terms":{
"field": "item_id"
}
}
}
}
聚合分析的需求:先分组,再算每组的平均值
GET /sport-shop-item-info/public/_search
{ "size": 0,
"query": {
"bool": {
"must": [
{
"term": {
"shop_id": {
"value": "3362038456"
}
}
},
{
"term": {
"platform_id": {
"value": "1"
}
}
},
{
"range": {
"crawl_time": {
"from": "2017-10-01",
"to": "2017-10-31",
"include_upper": true,
"include_lower": true
}
}
}
]
}
},
"aggregations":{
"groupByItemId" : {
"terms":{
"field": "item_id",
"size" : 0,
"shard_size" : 0
},
"aggregations":{
"avgPrice":{
"avg":{
"field":"sale_price"
}
}
}
}
}
}
数据分析需求:计算店铺下的商品的平均价格,并且按照平均价格降序排序
GET /sport-shop-item-info/public/_search
{
"size" : 0,
"query" : {
"bool" : {
"must" : [ {
"term" : {
"platform_id" : "1"
}
}, {
"term" : {
"shop_id" : "353571709"
}
} ],
"must_not" : [ {
"term" : {
"shelve_change" : 0
}
}, {
"term" : {
"sale" : 0
}
} ],
"should" : {
"bool" : {
"must" : [ {
"term" : {
"platform_id" : "1"
}
}, {
"term" : {
"shop_id" : "353571709"
}
} ]
}
}
}
},
"aggregations" : {
"groupByItem" : {
"terms" : {
"field" : "item_id",
"order" : {
"avgPrice" : "desc"
}
},
"aggregations" : {
"avgPrice" : {
"avg" : {
"field" : "sale_price"
}
}
}
}
}
}
参考官方文档:
https://www.elastic.co/guide/en/elasticsearch/reference/5.2/index.html