Elasticsearch 内联脚本过滤器不起作用
Elasticsearch inline script filter not working
我正在尝试在 ES-2.3.4 上执行以下查询。如果您在末尾删除内联脚本,查询将按预期工作。但是如果我包含脚本,查询应该是 return 结果,但事实并非如此。这是一个 groovy 脚本。其中 "bio" 是嵌套对象。如果需要任何更改,任何人都可以验证查询并建议我。
{
"bool" : {
"must" : [ {
"nested" : {
"query" : {
"term" : {
"bio.cl" : "Position"
}
},
"path" : "bio"
}
}, {
"nested" : {
"query" : {
"terms" : {
"bio.type" : [ "SV" ]
}
},
"path" : "bio"
}
}, {
"nested" : {
"query" : {
"terms" : {
"bio.node" : [ "XX" ]
}
},
"path" : "bio"
}
}, {
"terms" : {
"domain" : [ "YY" ]
}
} ],
"filter" : [ {
"nested" : {
"query" : {
"term" : {
"bio.chromo" : 1
}
},
"path" : "bio"
}
}, {
"nested" : {
"query" : {
"range" : {
"bio.start" : {
"from" : null,
"to" : 1000140.0,
"include_lower" : true,
"include_upper" : true
}
}
},
"path" : "bio"
}
}, {
"nested" : {
"query" : {
"range" : {
"bio.stop" : {
"from" : 1000861.0,
"to" : null,
"include_lower" : true,
"include_upper" : true
}
}
},
"path" : "bio"
}
}, {
"script" : {
"script" : {
"inline" : "percent <= ([stop,_source.bio.stop.value].min() - [start,_source.bio.start.value].max())/[length,_source.bio.stop.value-_source.bio.start.value+1].max()",
"params" : {
"stop" : 1001100,
"start" : 999901,
"length" : 1200,
"percent" : 0.8
}
}
}
} ]
}
}
映射:
"mappings": {
"XX": {
"properties": {
"bio": {
"type": "nested",
"properties": {
"alt": {
"type": "string",
"index": "not_analyzed"
},
"ann": {
"type": "string",
"index": "not_analyzed"
},
"chromo": {
"type": "string",
"index": "not_analyzed"
},
"cod": {
"type": "string"
},
"conseq": {
"type": "string",
"index": "not_analyzed"
},
"contri": {
"type": "string",
"index": "not_analyzed"
},
"created": {
"type": "string",
"index": "not_analyzed"
},
"createdDate": {
"type": "date",
"format": "strict_date_optional_time"
},
"domain": {
"type": "string",
"index": "not_analyzed"
}"id": {
"type": "long"
},
"name": {
"type": "string",
"index": "not_analyzed"
},
"node": {
"type": "string",
"index": "not_analyzed"
},
"position": {
"type": "string",
"index": "not_analyzed"
},
"level": {
"type": "string",
"index": "not_analyzed"
},
"start": {
"type": "long"
},
"stop": {
"type": "long"
}
}
}
}
}
}
示例文档:
_source" : {
"id" : 25,
"bio" : [ {
"creation" : "2018-03-05T20:26:46.466Z",
"updateDate" : "2018-03-05T20:26:46.466Z",
"createdBy" : "XX",
"type" : "SV",
"creationDate" : "2018-03-05T20:26:46.472Z",
"updateDate" : "2018-03-05T20:26:46.521Z",
"createdBy" : "XX",
"updatedBy" : "XX",
"domain" : "YY",
"node" : "XX",
"ann" : "1.6",
"gen" : "37",
"level" : "Position",
"chromo" : "1",
"start" : 999901,
"stop" : 1001100
}]
}
跟进我们在上面评论中的讨论...
您需要正确连接数组,即
[stop] + _source.biomarkers.collect{it.stop}
将创建一个包含 [stop, bio[0].stop, bio[1].stop, etc]
的数组,然后我们可以获取该数组的 max()
。
所以我建议像这样的东西应该可以工作(虽然未经测试)
percent <= (([stop] + _source.biomarkers.collect{it.stop}).min() - ([start] + _source.biomarkers.collect{it.start}).max()) / ([length] +_source.biomarkers.collect{it.stop - it.start + 1}).max()
我正在尝试在 ES-2.3.4 上执行以下查询。如果您在末尾删除内联脚本,查询将按预期工作。但是如果我包含脚本,查询应该是 return 结果,但事实并非如此。这是一个 groovy 脚本。其中 "bio" 是嵌套对象。如果需要任何更改,任何人都可以验证查询并建议我。
{
"bool" : {
"must" : [ {
"nested" : {
"query" : {
"term" : {
"bio.cl" : "Position"
}
},
"path" : "bio"
}
}, {
"nested" : {
"query" : {
"terms" : {
"bio.type" : [ "SV" ]
}
},
"path" : "bio"
}
}, {
"nested" : {
"query" : {
"terms" : {
"bio.node" : [ "XX" ]
}
},
"path" : "bio"
}
}, {
"terms" : {
"domain" : [ "YY" ]
}
} ],
"filter" : [ {
"nested" : {
"query" : {
"term" : {
"bio.chromo" : 1
}
},
"path" : "bio"
}
}, {
"nested" : {
"query" : {
"range" : {
"bio.start" : {
"from" : null,
"to" : 1000140.0,
"include_lower" : true,
"include_upper" : true
}
}
},
"path" : "bio"
}
}, {
"nested" : {
"query" : {
"range" : {
"bio.stop" : {
"from" : 1000861.0,
"to" : null,
"include_lower" : true,
"include_upper" : true
}
}
},
"path" : "bio"
}
}, {
"script" : {
"script" : {
"inline" : "percent <= ([stop,_source.bio.stop.value].min() - [start,_source.bio.start.value].max())/[length,_source.bio.stop.value-_source.bio.start.value+1].max()",
"params" : {
"stop" : 1001100,
"start" : 999901,
"length" : 1200,
"percent" : 0.8
}
}
}
} ]
}
}
映射:
"mappings": {
"XX": {
"properties": {
"bio": {
"type": "nested",
"properties": {
"alt": {
"type": "string",
"index": "not_analyzed"
},
"ann": {
"type": "string",
"index": "not_analyzed"
},
"chromo": {
"type": "string",
"index": "not_analyzed"
},
"cod": {
"type": "string"
},
"conseq": {
"type": "string",
"index": "not_analyzed"
},
"contri": {
"type": "string",
"index": "not_analyzed"
},
"created": {
"type": "string",
"index": "not_analyzed"
},
"createdDate": {
"type": "date",
"format": "strict_date_optional_time"
},
"domain": {
"type": "string",
"index": "not_analyzed"
}"id": {
"type": "long"
},
"name": {
"type": "string",
"index": "not_analyzed"
},
"node": {
"type": "string",
"index": "not_analyzed"
},
"position": {
"type": "string",
"index": "not_analyzed"
},
"level": {
"type": "string",
"index": "not_analyzed"
},
"start": {
"type": "long"
},
"stop": {
"type": "long"
}
}
}
}
}
}
示例文档:
_source" : {
"id" : 25,
"bio" : [ {
"creation" : "2018-03-05T20:26:46.466Z",
"updateDate" : "2018-03-05T20:26:46.466Z",
"createdBy" : "XX",
"type" : "SV",
"creationDate" : "2018-03-05T20:26:46.472Z",
"updateDate" : "2018-03-05T20:26:46.521Z",
"createdBy" : "XX",
"updatedBy" : "XX",
"domain" : "YY",
"node" : "XX",
"ann" : "1.6",
"gen" : "37",
"level" : "Position",
"chromo" : "1",
"start" : 999901,
"stop" : 1001100
}]
}
跟进我们在上面评论中的讨论...
您需要正确连接数组,即
[stop] + _source.biomarkers.collect{it.stop}
将创建一个包含 [stop, bio[0].stop, bio[1].stop, etc]
的数组,然后我们可以获取该数组的 max()
。
所以我建议像这样的东西应该可以工作(虽然未经测试)
percent <= (([stop] + _source.biomarkers.collect{it.stop}).min() - ([start] + _source.biomarkers.collect{it.start}).max()) / ([length] +_source.biomarkers.collect{it.stop - it.start + 1}).max()