弹性搜索:如何在获得所需结果后终止多搜索查询
我们有一个名为“Type”的字符串字段的弹性搜索文档。此字段可以具有从“A”到“Z”的不同值。多个文档可以具有相同的类型,即多个文档可以具有类型“A” 我们想编写一个弹性搜索查询,最多返回30个这样的文档。我们希望输出根据类型在不同的组中。例如:弹性搜索:如何在获得所需结果后终止多搜索查询,
Warning: implode(): Invalid arguments passed in /data/phpspider/zhask/webroot/tpl/detail.html on line 45
,,我们有一个名为“Type”的字符串字段的弹性搜索文档。此字段可以具有从“A”到“Z”的不同值。多个文档可以具有相同的类型,即多个文档可以具有类型“A” 我们想编写一个弹性搜索查询,最多返回30个这样的文档。我们希望输出根据类型在不同的组中。例如: POST /<index_name>/<type>/_search { "query": { "bool": { "should": [ { "bool": {
POST /<index_name>/<type>/_search
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"type": "A"
}
},
{
"match": {
"description": "Quick fox"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"type": "X"
}
},
{
"match": {
"description": "Quick bear"
}
}
]
}
}
]
}
},
"size": 30,
"sort": {
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": """
int r = 1;
if(doc['type'].value == 'X') {
r = 100;
} else if(doc['type'].value == 'C') {
r = 10;
} else if(doc['type'].value == 'A') {
r = 5;
}
r;
"""
},
"order": "desc"
}
}
}
POST\msearch/
{“索引”:“”“类型”:“”}
{“from”:0,“size”:30,“query”:{“bool”:{“must”:[{“terms”:{“type”:[“A”]}]}}}
{“索引”:“”“类型”:“”}
{“from”:0,“size”:30,“query”:{“bool”:{“must”:[{“terms”:{“type”:[“B”]}}]}}}
...
{“索引”:“”“类型”:“”}
{“from”:0,“size”:30,“query”:{“bool”:{“must”:[{“terms”:{“type”:[“Z”]}}]}}
我担心多搜索查询的执行,即对于案例1和案例2,我们从最初的几个查询中获得了足够的输出,即30个文档,那么我们为什么要执行其余的多搜索查询呢?一旦我们得到所需数量的结果,是否有任何方法停止多重搜索查询操作,即一旦我们得到30个或更多结果,就终止多重搜索
请注意:
看起来,您可以通过使用和进行单个搜索来实现所需的功能,还可以选择使用将查询合并为一个查询 我可以提前终止多搜索查询吗? 不。从文件中我们可以得出结论。它以一定程度的并发性执行多个搜索请求,并仅在所有查询完成后才发回结果 这很像是一种轻松执行并行请求的方法 我可以按自定义顺序获取匹配的文档吗? 是的,这就是为什么。要实现原始帖子中描述的行为,使用此调用就足够了:
POST /<index_name>/<index_type>/_search?sort=type:asc&size=30
或通配符表达式:
POST /multisearch*/<index_type>/_search?sort=type:asc&size=30
这也适用于多个集合(如上面的查询)
如果我有一个依赖于type
值的复杂查询,我可以这样做吗?
是,没问题,请使用查询,例如:
POST /<index_name>/<type>/_search
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"type": "A"
}
},
{
"match": {
"description": "Quick fox"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"type": "X"
}
},
{
"match": {
"description": "Quick bear"
}
}
]
}
}
]
}
},
"size": 30,
"sort": {
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": """
int r = 1;
if(doc['type'].value == 'X') {
r = 100;
} else if(doc['type'].value == 'C') {
r = 10;
} else if(doc['type'].value == 'A') {
r = 5;
}
r;
"""
},
"order": "desc"
}
}
}
POST//\u搜索
{
“查询”:{
“布尔”:{
“应该”:[
{
“布尔”:{
“必须”:[
{
“期限”:{
“类型”:“A”
}
},
{
“匹配”:{
“描述”:“快速狐狸”
}
}
]
}
},
{
“布尔”:{
“必须”:[
{
“期限”:{
“类型”:“X”
}
},
{
“匹配”:{
“说明”:“快熊”
}
}
]
}
}
]
}
},
“尺寸”:30,
“排序”:{
“_脚本”:{
“类型”:“编号”,
“脚本”:{
“郎”:“无痛”,
“来源”:”
int r=1;
如果(doc['type'].value='X'){
r=100;
}如果(doc['type'].value='C'){
r=10;
}else if(doc['type'].value='A'){
r=5;
}
R
"""
},
“订单”:“描述”
}
}
}
希望有帮助
POST /<index_name>/<type>/_search
{
"size": 30,
"sort": {
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": """
int r = 1;
if(doc['type'].value == 'X') {
r = 100;
} else if(doc['type'].value == 'C') {
r = 10;
} else if(doc['type'].value == 'A') {
r = 5;
}
r;
"""
},
"order": "desc"
}
}
}
POST /<index_name>/<type>/_search
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"type": "A"
}
},
{
"match": {
"description": "Quick fox"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"type": "X"
}
},
{
"match": {
"description": "Quick bear"
}
}
]
}
}
]
}
},
"size": 30,
"sort": {
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": """
int r = 1;
if(doc['type'].value == 'X') {
r = 100;
} else if(doc['type'].value == 'C') {
r = 10;
} else if(doc['type'].value == 'A') {
r = 5;
}
r;
"""
},
"order": "desc"
}
}
}