我想基于字段值对Elastic的结果进行排序。
Document Type Hierarchy:
1) Type1
2) Type2
3) Type3
4) Type4
我在ElasticsearchHead Chrome扩展程序中使用以下查询
http://localhost:9200/products/_search [Post]
{
"query": {
"bool": {
"must": [
{
"term": {
"ispublished": "1"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 1000,
"sort": [
{
"_script": {
"type": "number",
"script": {
"lang": "groovy",
"inline": "if(doc['type']=='Type1') return 1; elseif(doc['type']=='Type2') return 2;else return 0;"
},
"order": "asc"
}
}
],
"aggs": {}
}
但是我遇到了以下错误
{
"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[Qx2ykny3RSmMOoGCdR-MQQ][endscompliance][0]: SearchParseException[[endscompliance][0]: query[+ispublished:1],from[0],size[1000]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":[{"term":{"ispublished":"1"}}],"must_not":[],"should":[]}},"from":0,"size":1000,"sort":[{"_script":{"type":"number","script":{"lang":"groovy","inline":"if(doc['type']=='Type1') return 1; elseif(doc['type']=='Type2') return 2;else return 0;"},"order":"asc"}}],"aggs":{}}]]]; nested: SearchParseException[[endscompliance][0]: query[+ispublished:1],from[0],size[1000]: Parse Failure [_script sorting requires setting the script to sort by]]; }{[Qx2ykny3RSmMOoGCdR-MQQ][endscompliance][1]: SearchParseException[[endscompliance][1]: query[+ispublished:1],from[0],size[1000]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":[{"term":{"ispublished":"1"}}],"must_not":[],"should":[]}},"from":0,"size":1000,"sort":[{"_script":{"type":"number","script":{"lang":"groovy","inline":"if(doc['type']=='Type1') return 1; elseif(doc['type']=='Type2') return 2;else return 0;"},"order":"asc"}}],"aggs":{}}]]]; nested: SearchParseException[[endscompliance][1]: query[+ispublished:1],from[0],size[1000]: Parse Failure [_script sorting requires setting the script to sort by]]; }{[Qx2ykny3RSmMOoGCdR-MQQ][endscompliance][2]: SearchParseException[[endscompliance][2]: query[+ispublished:1],from[0],size[1000]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":[{"term":{"ispublished":"1"}}],"must_not":[],"should":[]}},"from":0,"size":1000,"sort":[{"_script":{"type":"number","script":{"lang":"groovy","inline":"if(doc['type']=='Type1') return 1; elseif(doc['type']=='Type2') return 2;else return 0;"},"order":"asc"}}],"aggs":{}}]]]; nested: SearchParseException[[endscompliance][2]: query[+ispublished:1],from[0],size[1000]: Parse Failure [_script sorting requires setting the script to sort by]]; }{[Qx2ykny3RSmMOoGCdR-MQQ][endscompliance][3]: SearchParseException[[endscompliance][3]: query[+ispublished:1],from[0],size[1000]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":[{"term":{"ispublished":"1"}}],"must_not":[],"should":[]}},"from":0,"size":1000,"sort":[{"_script":{"type":"number","script":{"lang":"groovy","inline":"if(doc['type']=='Type1') return 1; elseif(doc['type']=='Type2') return 2;else return 0;"},"order":"asc"}}],"aggs":{}}]]]; nested: SearchParseException[[endscompliance][3]: query[+ispublished:1],from[0],size[1000]: Parse Failure [_script sorting requires setting the script to sort by]]; }{[Qx2ykny3RSmMOoGCdR-MQQ][endscompliance][4]: SearchParseException[[endscompliance][4]: query[+ispublished:1],from[0],size[1000]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":[{"term":{"ispublished":"1"}}],"must_not":[],"should":[]}},"from":0,"size":1000,"sort":[{"_script":{"type":"number","script":{"lang":"groovy","inline":"if(doc['type']=='Type1') return 1; elseif(doc['type']=='Type2') return 2;else return 0;"},"order":"asc"}}],"aggs":{}}]]]; nested: SearchParseException[[endscompliance][4]: query[+ispublished:1],from[0],size[1000]: Parse Failure [_script sorting requires setting the script to sort by]]; }]",
"status": 400
}
弹性版本为1.7.5
嵌套版本为5.5