Elastic Stack之搜索和查询

一、 查询上下文:ctx

二、相关度评分:_score

三、元数据:_source

  1. 禁用元数据
  2. 数据源过滤

includes:[]
excludes:[]

  1. 常用的过滤规则
"_source":"false"
"_source":"obj.*"
"_source":["obj0.*","obj1.*"]
"_source":{
	"includes":["obj.*"],
	"excludes":["name","gender"]
}

四、DSL(Domain Specific Language)

Query String Search
  1. 查询所有
  2. 参数查询
  3. 分页查询
  4. 精准匹配
  5. _all搜索
全文检索(Fulltext Query)
  1. match
GET index/_search
{
	"query":{
		"match":{
			"name":"CMCC CDMA"
		}
	}
}
  1. match_all
GET index/_search
{
	"query":{
		"match_all":{}
	}
}

  1. multi_match
GET index/_search
{
	"query":{
		"multi_match":{
			"query":"CMCC",
			"fields":["name","ismalfunc"]
		}
	}
}
  1. match_phrase
GET index/_search
{
	"query":{
		# 作为一个整体(实际上是被分词了),且顺序不能颠倒
		"match_phrase":{
			"name":"CMCC CDMA"
		}
	}
}
  1. 精准匹配(exact match/value)
    5.1. term(不会被分词)
    term && match_phrase
    term && keyword
    5.2. terms
# select * from table where funcs in ("4G","5G","StarLinks")
GET index/_search
{
	"query":{
		"terms":{
			"funcs":["4G","5G","StarLinks"]
		}
	}
}

5.3. range

GET index/_search
{
	"query":{
		"range":{
			"date":{
				"time_zone":"+08:00",
				"gte":"2022-05-22T08:00:00"
			}
		}
	}
}

  1. 过滤查询filter
GET index/_search
{
	"query":{
		"constant_score":{
			"filter":{
            	"term":{
            		"name":"联通"
            	}
            }
		}
	}
}
  1. 组合查询 bool query
    关键词:bool
  • filter 过滤器不计算相关度评分,并用于缓存,查询效率较高
  • must 计算相关度评分
  • should
  • must not
GET index/_search
{
	"query":{
		"bool":{
        	"filter":[
        		{
        			"match":{
        				"name": "移动 联通 电信"
        			}
        		},
        		{
        			"match_phrase":{
        				"name":"中国 邮政"
        			}
        		}
        	],
        	"should":[
        		{
        			"range":{
        				"price":{
        					"lte":120
        				}
        			}
        		},
        		{
        			"bool":{
        				"must_not":[ 
        					{
        						"match":{
        							"name":"CDMA"
        						}
        					}
						]
        			}
        		}
        	],
        	# should条件至少满足0个1个或多个
        	"minimum_should_match":1
        }
	}
}

must 和 filter组合,提高查询性能,filter不会提供相关度评分,节省性能,must可以提供相关度评分

minimum_should_match:如果bool查询包含至少一个should子句,而没有must或filter子句,则默认值为1,否则默认值为0。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值