组合查询分类:
1、bool:布尔组合
2、boost:加权
3、constant:固定分值
4、Dis_max:单字符多字段组合
5、function:函数脚本组合
bool组合查询:
语法:
must:必须符合,包含
should:满足任一条件(相当于mysql中or)
filter:必须符合,是否命中,不计算分值,等同must
must_not:必须不符合,不包含
GET /product_codes_reindex/_search
{
{
"query": {
"bool": {
"must": [
{
"term": {
"id": {
"value": "402979"
}
}
},
{
#分词的查询
"match": {
"productId": "1075886560278216704"
}
}
]
}
}
}
}
GET /product_codes_reindex/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"id": {
"value": "402992"
}
}
}
]
}
}
}
#查询结果
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "product_codes_reindex",
"_type" : "_doc",
"_id" : "402992",
"_score" : 0.0, #不计算分值
"_source" : {
"merchantId" : 1071168956150054913,
"merchantCode" : "",
"codeStatus" : 1,
"@timestamp" : "2021-01-15T08:30:27.044Z",
"productName" : "断路器/10",
"id" : 402992,
"codeSerial" : "H190386断路器/100000006810",
"codeEncryption" : "Du5SCxeAAFc",
"companyName" : "XX网络技术",
"productId" : 1075886560278216704,
"createTime" : "2019-06-13T06:27:25.000Z",
"@version" : "1",
"companyId" : 1047621472136200192,
"merchantName" : "XXX居家专卖"
}
}
]
}
}
Boost加权组合查询
1、正向加权
2、反向加权
GET /product_codes_reindex/_search
{
"query": {
"boosting": {
#正向
"positive": {
"term": {
"merchantId": {
"value": "1071168956150054913",
"boost": 1.2
}
}
},
#反向
"negative": {
"term": {
"companyId": {
"value": "1047621472136200192"
}
}
},
"negative_boost": 0.2
}
}
}
Constant固定分值组合
GET /product_codes_reindex/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"companyId": "1047621472136200192"
}
},
"boost": 1 #决定返回结果中的分值 "max_score"
}
}
}
注:查询的时候bool优先再是filter优先,性能上慎用function