请求
curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
- VERB HTTP方法:GET, POST, PUT, HEAD, DELETE
- PROTOCOL:http或者https协议(只有在Elasticsearch前面有https代理的时候可用)
- HOST:Elasticsearch集群中的任何一个节点的主机名,如果是在本地的节点,那么就叫localhost
- PORT:Elasticsearch HTTP服务所在的端口,默认为9200 PATH API路径(例如_count将返回集群中文档的数量),
- PATH:可以包含多个组件,例如_cluster/stats或者_nodes/stats/jvm
- QUERY_STRING:一些可选的查询请求参数,例如?pretty参数将使请求返回更加美观易读的JSON数据
- BODY:一个JSON格式的请求主体(如果请求需要的话)
可选的查询请求参数
?pretty
使请求返回更加美观易读的JSON数据
复杂查询DSL语句
查询
过滤
分页
排序
高亮
聚合
版本区别
语法
查询
模糊查询语法;查询title为荣耀的数据
GET /goods/info/_search
{
"query":{
"match":{
"title":"荣耀"
}
}
}
过滤查询语法;按照tmId(品牌id)为2的查询
GET /goods/info/_search
{
“query”:{
“bool”:{
“filter”:{
“term”:{
“tmId”:”2”
}
}
}
}
}
分页+过滤语法
过滤:按照tmId(品牌id)为2的查询
分页:从索引为0开始显示(关键字:from),显示一条数据(关键字:size)
GET /goods/info/_search
{
“query”:{
“bool”:{
“filter”:{
“term”:{
“tmId”:”2”
}
}
}
},
“from”:0,
“size”:1
}
过滤+排序
过滤:查询tmId(品牌id)为2的
排序:关键字sort;按照id排序,排列顺序order:倒序desc
GET /goods/info/_search
{
“query”:{
“bool”:{
“filter”:{
“term”:{
“tmId”:”2”
}
}
}
},
“sort”:[
{
“id”:{
“order”:”desc”
}
}
]
}
模糊查询+高亮
查询title为荣耀的数据
高亮显示 关键字highlight,会给title的数据添加em标签进行高亮显示
GET /goods/info/_search
{
“query”:{
“match”:{
“title”:”荣耀”
}
},
“highlight”:{
“fields”:{
“title”:{}
}
}
}
分组查询,按照品牌统计
关键字aggs,分组field,按照tmId分组;查询个数size
GET /goods/info/_search
{
“query”:{
“match”:{
“title”:”荣耀”
}
},
“aggs”:{
“tmAg:{
“terms”:{
“field”:”tmId”,
“size”:10
}
}
}
}
## 热度排名:
1. 根据索引库中的 hotScore 进行排名
2. hotScore应该根据什么进行变化呢?
思路:
查询商品详情, 查询一次商品详情,把字段更新 +1
每次有一个人查看都更新一次索引库,频率太高,
我们把这个字段存到redis一份, 如果 %100==0 我再让更新索引库,
## 搜索功能
搜索条件
过滤条件
分页
排序
### 搜索条件
GET /goods/info/_search
{
“query”:{
“match”:{
“title”:{
#query 查询 小米手机的数据
“query”:”小米手机”,
# 默认”operator”的值为”or”,查询小米|手机的数据
# 可以设置”operator”的值为”and”,查询包含’小米手机’的数据
“operator”:”or”
}
}
}
}
### 过滤条件
单个过滤条件
GET /goods/info/_search
{
“query”:{
“bool”:{
# 过滤出category3Id为61的数据
“filter”:{
“term”:{
“category3Id”:”61”
}
}
}
}
}
多个过滤条件
GET /goods/info/_search
{
“query”:{
“bool”:{
# 过滤出category3Id为61并且tmId为2的数据
“filter”:[
{“term”:{“category3Id”:”61”}},
{“term”:{“tmId”:”2”}}
]
}
}
}
### nested类型数据
GET /goods/info/_search
{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“title”: {
“query”: “荣耀手机”,
“operator”: “and”
}
}
},
{
“nested”: {
“path”: “attrs”,
“query”: {
“bool”: {
“must”: [
{
“term”: {
“attrs.attrId”: {
“value”: “3”
}
}
},
{
“term”: {
“attrs.attrValue”: {
“value”: “8GB”
}
}
}
]
}
}
}
}
]
}
}
}
### 合并
#分页查询
#过滤条件
#排序
#高亮显示
GET /goods/info/_search
{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“title”: {
“query”: “小米手机”,
“operator”: “or”
}
}
},
{
“nested”: {
“path”: “attrs”,
“query”: {
“bool”: {
“must”: [
{
“term”: {
“attrs.attrId”: {
“value”: “3”
}
}
},
{
“term”: {
“attrs.attrValue”: {
“value”: “8GB”
}
}
}
]
}
}
}
}
]
}
},
“from”: 0,
“size”: 2,
“sort”: [
{
“hotScore”: {
“order”: “desc”
}
}
],
“highlight”: {
“pre_tags”: [““],
“fields”: {
“title”: {}
},
“post_tags”: [““]
}
}