一、 查询上下文:ctx
二、相关度评分:_score
三、元数据:_source
- 禁用元数据
- 数据源过滤
includes:[]
excludes:[]
- 常用的过滤规则
"_source":"false"
"_source":"obj.*"
"_source":["obj0.*","obj1.*"]
"_source":{
"includes":["obj.*"],
"excludes":["name","gender"]
}
四、DSL(Domain Specific Language)
Query String Search
- 查询所有
- 参数查询
- 分页查询
- 精准匹配
- _all搜索
全文检索(Fulltext Query)
- match
GET index/_search
{
"query":{
"match":{
"name":"CMCC CDMA"
}
}
}
- match_all
GET index/_search
{
"query":{
"match_all":{}
}
}
- multi_match
GET index/_search
{
"query":{
"multi_match":{
"query":"CMCC",
"fields":["name","ismalfunc"]
}
}
}
- match_phrase
GET index/_search
{
"query":{
# 作为一个整体(实际上是被分词了),且顺序不能颠倒
"match_phrase":{
"name":"CMCC CDMA"
}
}
}
- 精准匹配(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"
}
}
}
}
- 过滤查询filter
GET index/_search
{
"query":{
"constant_score":{
"filter":{
"term":{
"name":"联通"
}
}
}
}
}
- 组合查询 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。