我需要从脚本访问存储在数组中的文档值。数组中项目的顺序很重要。
使用doc ['...']检索数组会混淆顺序:-(
假设像这样的简单文件
{
"ar":[5,4,3,2,1]
}
Run Code Online (Sandbox Code Playgroud)
使用此查询检索到:
{
"query":{
"match_all":{}
},
"script_fields": {
"values": {
"script": {
"inline":"return doc['ar']"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
将以反向(排序)顺序返回数组:[1,2,3,4,5]有没有办法防止这种行为?
我不能诉诸于_source,因为我需要在“ has_child”查询中使用它,该查询不支持_source。
有任何想法吗?
注意: 我最初发布这个问题的方式略有不同,不值得更新,因为阅读后我学到了更多.
搜索文档并根据文档中的嵌套元素计算自定义分数.
{
"mappings": {
"book": {
"properties": {
"title": { "type": "string", "index": "not_analyzed" },
"topics": {
"type": "nested",
"properties": {
"title": { "type": "string", "index": "not_analyzed" },
"weight": { "type": "int" }
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
{
"query": {
"function_score": {
"query": {
"term": { "title": "The Magical World of Spittle" }
},
"script_score": {
"script": {
"lang": "painless",
"inline": "int score = 0; for(int i = 0; i < doc['topics'].values.length; i++) …Run Code Online (Sandbox Code Playgroud) 我正在尝试按照官方文档学习 Elastic Search 中的无痛脚本编写。(https://www.elastic.co/guide/en/elasticsearch/painless/6.0/painless-examples.html)
我正在使用的文档示例:
{
"uid" : "CT6716617",
"old_username" : "xyz",
"new_username" : "abc"
}
Run Code Online (Sandbox Code Playgroud)
使用 params._source 访问文档值的以下脚本字段查询有效:
{
"script_fields": {
"sales_price": {
"script": {
"lang": "painless",
"source": "(params._source.old_username != params._source.new_username) ? \"change\" : \"nochange\"",
"params": {
"change": "change"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
相同的查询但使用文档映射来访问值失败:
{
"script_fields": {
"sales_price": {
"script": {
"lang": "painless",
"source": "(doc['old_username'] != doc['new_username']) ? \"change\" : \"nochange\"",
"params": {
"change": "change"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到的错误消息是:
"caused_by" : {
"type" : …Run Code Online (Sandbox Code Playgroud) 我们在项目中使用了许多ScriptQuery和ScriptField ,并以轻松的语言为它们提供了内联脚本。
我的问题是,我们如何验证这些轻松的脚本?换句话说,如何确保它们能够编译?
我们今天使用的方法是通过 Kibana 在本地测试它们。然而,这是手动的,容易出错且不可扩展。我正在寻找一种编程方式或实用程序来验证无痛脚本,以便我可以将其插入 CI/CD 管道中。Elasticsearch 底层使用的编译器是开源的吗?或者还有其他办法吗?
Elasticsearch 版本 5.4
Kibana 中的示例查询,带有用于 ScriptField 和 ScriptQuery 的 Painless 脚本
GET myIndex/_search
{
"script_fields": {
"LastName": {
"script": {
"inline": "if(doc['Author']!= null && doc['Author'].value != null){return doc['Author'].value.toUpperCase();}return null;",
"lang": "painless"
}
}
},
"query": {
"bool": {
"must_not": [
{
"bool": {
"must": [
{
"script": {
"script": {
"inline": "def lastName = '';if(doc['Author']!= null && doc['Author'].value != null){lastName = doc['Author'].value.toUpperCase();}if(doc.containsKey('LastName') && doc['LastName']!= null && doc['LastName'].value != …Run Code Online (Sandbox Code Playgroud) compilation elasticsearch kibana elasticsearch-5 elasticsearch-painless
目前,在ES 5.6上,我们使用groovy内联脚本来获取给定文档中给定术语的tf,如 -
GET document/_search
{
"size": 114,
"query": {"terms": {
"doc_id": [1840, 2160]
}},
"script_fields": {
"tf": {
"script": {
"lang": "groovy",
"inline": "_index['text'][term_value].tf()",
"params": {
"term_value": "hello"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以它回复我的回应 -
"hits": {
"total": 36,
"max_score": 1,
"hits": [
{
"_index": "document",
"_type": "sample",
"_id": "41707",
"_score": 1,
"fields": {
"tf": [
3
]
}
}]
Run Code Online (Sandbox Code Playgroud)
但是在ES 6.0 groovy支持下降之后,似乎脚本引擎是唯一的解决方案,而且由于缺乏对Elasticsearch类和内部行为的正确理解,很难弄清楚实现.
基于Scripting Engine文档,我需要实现
private static class MyExpertScriptEngine implements ScriptEngine {
@Override
public String getType() {
return …Run Code Online (Sandbox Code Playgroud) elasticsearch elasticsearch-plugin elasticsearch-painless elasticsearch-6
对此有很多疑问和答案,但仍然没有得到满意的答案。弹性搜索版本:6.5
索引映射
"_doc": {
"properties": {
"ssid": {
"type": "long"
},
"nested_field": {
"type": "nested"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
搜索查询:
{
"query": {
"bool": {
"filter": {
"script": {
"script": "params._source.nested_field.size() > 1"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
还尝试了下面的查询但没有运气
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "nested_field",
"query": {
"bool": {
"filter": {
"script": {
"script": "params._source.nested_field.size() > 1"
}
}
}
}
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误
{
"error": {
"root_cause": [
{ …Run Code Online (Sandbox Code Playgroud) 无痛脚本映射参数中存在如何检查密钥。在下面的查询中,查询检查a.toString()键存在于参数中,我已经尝试了所有方法,但没有使它起作用。请帮我
映射:
"id": {
"type": "long"
}
Run Code Online (Sandbox Code Playgroud)
查询:
{
"query":{
"bool":{
"filter":[
{
"script": {
"script": {
"lang": "painless",
"params": {
"29232":2541,
"minDistance": 0
},
"source": "def a=doc['id'].getValue();double distance=params[a.toString()]; return distance <= 1000 && distance >= params['minDistance']"
}
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何解决我的 ES 5.6 索引遇到的这两个问题。
"mappings": {
"my_test": {
"properties": {
"Employee": {
"type": "nested",
"properties": {
"Name": {
"type": "keyword",
"normalizer": "lowercase_normalizer"
},
"Surname": {
"type": "keyword",
"normalizer": "lowercase_normalizer"
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要创建两个单独的脚本过滤器:
1 - 过滤员工数组大小为 == 3 的文档
2 - 过滤数组的第一个元素具有“名称”==“约翰”的文档
我试图迈出一些第一步,但我无法遍历列表。我总是有一个空指针异常错误。
{
"bool": {
"must": {
"nested": {
"path": "Employee",
"query": {
"bool": {
"filter": [
{
"script": {
"script" : """
int array_length = 0;
for(int i = 0; i < params._source['Employee'].length; i++)
{ …Run Code Online (Sandbox Code Playgroud) es5.5中,如何判断字段是否为数字?
if (is_numeric(ctx._source.some)) {
ctx._source.some = ctx._source.some + 2
}
Run Code Online (Sandbox Code Playgroud) 在这里学习一些elasticsearch,我对在脚本化字段定义中使用 min 和 max 函数感到有点困惑。第一的,
GET my_index/_search
{
"query" : {
"match_all": {}
},
"script_fields" : {
"test1" : {
"script" : {
"lang": "painless",
"source": "min(doc[\"this field\"],5)"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的回报是
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"min(doc[\"end\"],5)",
"^---- HERE"
],
"script": "min(doc[\"end\"],5)",
"lang": "painless"
}
], ...
Run Code Online (Sandbox Code Playgroud)
我想也许我需要给它命名Long.min并返回
"reason": "runtime error",
"script_stack": [
"""Long.min(doc["end"],5)""",
" ^---- HERE"
],
Run Code Online (Sandbox Code Playgroud)
这看起来是进步,但为什么会出现问题呢doc?
它们似乎在无痛 API 参考中,我认为如果它们不可用,那就有点愚蠢了。我一直在寻找“无痛最小最大功能”的组合,但我得到的只是上面链接的内容和一堆不相关的东西。
我在这里做错了什么?