自定义排序 RankedQuery
RankedQuery 在原始的查询结果集上增加了二次自定义排位功能,可用于 BadCase 的快速处理或垂搜场景的结果集处理。
示例
准备一些测试数据,例如从1到10的序列
POST _bulk
{ "index" : { "_index" : "greeting","_type":"_doc", "_id" : "1" } }
{ "number" : 1 }
{ "index" : { "_index" : "greeting","_type":"_doc", "_id" : "2" } }
{ "number" : 2 }
{ "index" : { "_index" : "greeting","_type":"_doc", "_id" : "3" } }
{ "number" : 3 }
{ "index" : { "_index" : "greeting","_type":"_doc", "_id" : "4" } }
{ "number" : 4 }
{ "index" : { "_index" : "greeting","_type":"_doc", "_id" : "5" } }
{ "number" : 5 }
{ "index" : { "_index" : "greeting","_type":"_doc", "_id" : "6" } }
{ "number" : 6 }
{ "index" : { "_index" : "greeting","_type":"_doc", "_id" : "7" } }
{ "number" : 7 }
{ "index" : { "_index" : "greeting","_type":"_doc", "_id" : "8" } }
{ "number" : 8 }
{ "index" : { "_index" : "greeting","_type":"_doc", "_id" : "9" } }
{ "number" : 9 }
{ "index" : { "_index" : "greeting","_type":"_doc", "_id" : "10" } }
{ "number" : 10 }
执行简单查询
GET greeting/_search
{
"query": {
"match_all": {}
}
}
结果集默认按_id排序
"hits" : [
{
"_source" : {"number" : 1}
},
{
"_source" : {"number" : 2}
},
{
"_source" : {"number" : 3}
},
...
1. 文档置顶
将 _id 为 7 和 8 的文档置顶
GET greeting/_search
{
"query": {
"ranked": {
"query": {
"match_all": {}
},
"rank": {
"top": ["7","8"]
}
}
}
}
结果集如下
"hits" : [
{
"_source" : {"number" : 7}
},
{
"_source" : {"number" : 8}
},
{
"_source" : {"number" : 1}
},
{
"_source" : {"number" : 2}
},
...
2. 移除文档
将_id 为 1 和 2 的文档移除
GET greeting/_search
{
"query": {
"ranked": {
"query": {
"match_all": {}
},
"rank": {
"block" : [ "1", "2" ]
}
}
}
}
返回
"hits" : [
{
"_source" : {"number" : 3}
},
{
"_source" : {"number" : 4}
},
{
"_source" : {"number" : 5}
},
...
3. 指定文档位置
将 _id 为 1 的文档排在第 3 位 ,将 _id 为 9 的文档排在第 2 位
GET greeting/_search
{
"query": {
"ranked": {
"query": {
"match_all": {}
},
"rank": {
"pos": [ {"1" : 3}, {"9" : 2} ]
}
}
}
}
返回
"hits" : [
{
"_source" : {"number" : 2}
},
{
"_source" : {"number" : 9}
},
{
"_source" : {"number" : 1}
},
{
"_source" : {"number" : 3}
},
...
API
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
top | 指定_id的文档尽量置顶 | string [] | - |
block | 指定_id的文档在结果集中排除 | string [] | - |
pos | 精确指定文档排列位置,优先级高于 top | { string : number } [] | - |
限制
- block 最多处理 1024 个文档。
- top+pos 合计最多处理 1024 个文档。
影响
- 被处理的文档将丧失原始分 _score。