搜索DSL
分析
因为搜索服务有多种查询条件,所以要使用bool
查询来构造查询条件, 全文检索使用must
条件,因为要记录查询分数, 不进行全文检索的条件 使用filter
查询, 因为filter
不进行分数统计,这样查询比较快些.
关键字查询
这个参数为全文检索条件, 根据tiitle检索商品
{
"query": {
{
"match": {
"skuTitle": "小米"
}
}
分类查询
根据分类id进行检索 这个条件不需要全文检索,所以直接写入filter即可 字符串匹配使用match 非字符串使用term
{
"query": {
"term": {
"catalogId": {
"value": "225"
}
}
}
}
品牌查询
品牌查询可以选中多个品牌 可以使用terms来进行查询
{
"query": {
"terms": {
"brandId": [
"10",
"11"
]
}
}
}
商品属性查询
因为商品属性是Nested类型,所以不能直接查询 要根据Nested特殊查询方式
nested介绍: https://www.cnblogs.com/lyraHeartstrings/p/15871391.html
path属性填写拥有nested参数的属性 之后进和普通查询一样拼接条件 如下面那个条件
查询attrId为15且值为晓龙665或海思的值查询出来
{
"query": {
"nested": {
"path": "attrs",
"query": {
"bool": {
"must": [
{
"term": {
"attrs.attrId": {
"value": "15"
}
}
},
{
"terms": {
"attrs.attrValue": [
"骁龙665",
"海思(Hisilicon)"
]
}
}
]
}
}
}
}
库存查询
将库存为true的值检索出来 使用trem查询
GET /product/_search
{
"query": {
"term": {
"hasStock": {
"value": "true"
}
}
}
}
价格范围查询
使用range查询 gte为最小 lte为最大
{
"query": {
"range": {
"skuPrice": {
"gte": 1500,
"lte": 5000
}
}
}
}
然后条件查询
sort根据价格进行排序
GET /product/_search
{
"sort": [
{
"skuPrice": {
"order": "desc"
}
}
]
}
高亮查询
fields表示高粱字段 pre_tags表示前缀 表示后缀 post_tags,必须之前有条件才能查询出来 如下例,之前使用SkuTitle进行查询过,高亮查询时也是根据skutitle进行检索,之后搜索结果会有一个高亮数组
GET /product/_search
{
"query": {
"term": {
"skuTitle": {
"value": "小米"
}
}
},
"highlight": {
"fields": {"skuTitle": {}},
"pre_tags": "<b>"
, "post_tags": "</b>"
}
分页查询
和MySQL中的分页查询相同from表示第几页 size表示每页条数
GET /product/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 5
}
条件聚合
将之前条件聚合起来 全文检索使用must, 其他条件使用filter
GET /product/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"skuTitle": "小米"
}
}
],
"filter": [
{
"term": {
"catalogId": {
"value": "225"
}
}
},
{
"terms": {
"brandId": [
"10",
"11"
]
}
},
{
"nested": {
"path": "attrs",
"query": {
"bool": {
"must": [
{
"term": {
"attrs.attrId": {
"value": "15"
}
}
},
{
"terms": {
"attrs.attrValue": [
"骁龙665",
"海思(Hisilicon)"
]
}
}
]
}
}
}
},
{
"term": {
"hasStock": {
"value": "true"
}
}
},
{
"range": {
"skuPrice": {
"gte": 1500,
"lte": 5000
}
}
}
]
}
},
"sort": [
{
"skuPrice": {
"order": "desc"
}
}
],
"from": 0,
"size": 5,
"highlight": {
"fields": {"skuTitle": {}},
"pre_tags": "<b>"
, "post_tags": "</b>"
}
}
虽然道路是曲折的,但前途是光明的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律