我想使用以下方法查询名称长度为value(text)大于20的文件,但不起作用:
GET /groups/_search
{
"query": {
"bool" : {
"must" : {
"script" : {
"script" : "_source.name.values.length() > 20"
}
}
}
}
}
错误消息是:
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"_source.name.values.lengt ...",
"^---- HERE"
],
"script": "_source.name.values.length() > 5",
"lang": "painless"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "groups",
"node": "exBbDVGeToSDRzLLmOh8-g",
"reason": {
"type": "query_shard_exception",
"reason": "failed to create query: {\n \"bool\" : {\n \"must\" : [\n {\n \"script\" : {\n \"script\" : {\n \"inline\" : \"_source.name.values.length() > 5\",\n \"lang\" : \"painless\"\n },\n \"boost\" : 1.0\n }\n }\n ],\n \"disable_coord\" : false,\n \"adjust_pure_negative\" : true,\n \"boost\" : 1.0\n }\n}",
"index_uuid": "_VH1OfpdRhmd_UPV7uTNMg",
"index": "groups",
"caused_by": {
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"_source.name.values.lengt ...",
"^---- HERE"
],
"script": "_source.name.values.length() > ",
"lang": "painless",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Variable [_source] is not defined."
}
}
}
}
]
},
"status": 400
}
不知道我该怎么解决...
仅供引用:es的版本是5.4.0
我不知道以下相关问题:
无痛的script_fields无权访问_source变量#20068
https://github.com/elastic/elasticsearch/issues/20068
最佳答案
解决此问题的最佳方法是,也用name
字段的长度来索引另一个字段,我们将其称为nameLength
。这样,您就可以转移在索引时计算名称字段长度的负担,而不必(在重复时)在查询时进行计算。
因此,在建立索引时,如果您有一个像name
这样的{"name": "A big brown fox"}
字段,那么您将创建一个具有名称字段长度的新字段,例如{"name": "A big brown fox", "nameLength": 15}
。
在查询时,您将可以在range
字段上使用简单快速的nameLength
查询:
GET /groups/_search
{
"query": {
"bool" : {
"must" : {
"range" : {
"nameLength": {
"gt": 20
}
}
}
}
}
}
关于elasticsearch - 长度大于20的elasticsearch查询文本字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45811337/