我没有Java经验,我有弹性搜索无痛脚本语言的问题.(这个名字是无痛的,不是很好选择).
对于以下代码,我收到错误:
{"lang": "painless",
"inline": "float price = doc['newPrice'] > 0.0 ? doc['price'] / doc['newPrice'] : 0; _score * params.constant * price",
"params": {"constant": 1.2}}}}
Run Code Online (Sandbox Code Playgroud)
无法对类型[org.elasticsearch.index.fielddata.ScriptDocValues.Doubles]和[java.lang.Double]应用[>]操作.
我绑定使用(浮动)doc ['newPrice']> 0将其转换为浮动,但具有相同的错误.
然后我改为 "Double price = ((Double)doc['discountPrice'] > 0.0) ? doc['price'] / doc['discountPrice'] : 0; _score * params.constant * price",
并收到:
'无法从[Double]转换为[double].
有人可以帮助我,尝试了很多类似错误的变化.该死的无痛语言......
我正在使用Elastic 5.5轻松过滤文档
使用“无痛”,查找带有strings字段的文档。
仅strings返回带字段的文档
所有文件均已退回。
只要有带strings字段的文档,所有文档都会返回。这可能是某种缓存问题。
PUT /test_idx
POST /test_idx/t/1
{
"strings": ["hello", "world"]
}
POST /test_idx/t/2
{
"numbers": [1, 2, 3]
}
Run Code Online (Sandbox Code Playgroud)
GET /test_idx/_search
{
"query": {
"bool": {
"filter": [
{
"script": {
"script": {
"lang": "painless",
"inline": "return doc.containsKey(params.keypath)",
"params": {"keypath": "strings"}
}
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": …Run Code Online (Sandbox Code Playgroud) 我希望能够列出存储在给定 Elasticsearch 集群上的所有脚本。
elasticsearch 文档有关于如何创建、检索、使用和删除具有特定名称的脚本的清晰示例。不幸的是,没有提到列表端点。
下面做不工作(至少在Elasticsearch 5.4.1):
GET _cat/scriptsGET _scriptsGET _scripts/我正在运行 Elasticsearch 的本地实例,并尝试在 scripted_fields 下使用“无痛”。我可以很好地编写一行脚本代码,但是当我使用三引号(根据文档支持)创建多行脚本时,它给了我这个奇怪的解析错误。
运行单行脚本效果很好:
{
"script_fields": {
"scripted": {
"script": {
"lang": "painless",
"source": "0"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果中返回的每个实体中的结果(预期)如下:
"fields" : {
"scripted" : [
0
]
}
Run Code Online (Sandbox Code Playgroud)
但使用多行格式:
{
"script_fields": {
"scripted": {
"script": {
"lang": "painless",
"source":
"""
0
"""
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
给我这个错误:
Unexpected character ('\"' (code 34)): was expecting comma to separate Object entries\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@56e69b76; line: 7, column: 12]
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
考虑以下Elasticsearch(v5.4)对象("奖励"doc类型):
{
"name": "Gold 1000",
"date": "2017-06-01T16:43:00.000+00:00",
"recipient": {
"name": "James Conroy",
"date_of_birth": "1991-05-30"
}
}
Run Code Online (Sandbox Code Playgroud)
映射类型都award.date和award.recipient.date_of_birth是"日期".
我想进行范围汇总,以获得该奖项的获奖者年龄范围列表("18岁以下","18-24岁","24-30岁","30岁以上").奖励.我尝试了以下聚合查询:
{
"size": 0,
"query": {"match_all": {}},
"aggs": {
"recipients": {
"nested": {
"path": "recipient"
},
"aggs": {
"age_ranges": {
"range": {
"script": {
"inline": "doc['date'].date - doc['recipient.date_of_birth'].date"
},
"keyed": true,
"ranges": [{
"key": "Under 18",
"from": 0,
"to": 18
}, {
"key": "18-24",
"from": 18,
"to": 24
}, {
"key": "24-30",
"from": 24,
"to": …Run Code Online (Sandbox Code Playgroud) datetime date elasticsearch elasticsearch-5 elasticsearch-painless
我试图在elasticsearch的脚本语言中操纵日期painless.具体来说,我试图增加4小时,即14,400秒.
{
"script_fields": {
"new_date_field": {
"script": {
"inline": "doc['date_field'] + 14400"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这引发了 Cannot apply [+] operation to types [org.elasticsearch.index.fielddata.ScriptDocValues.Longs] and [java.lang.Integer].
谢谢
https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
考虑:
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"user": {
"type": "nested"
}
}
}
}
}
PUT my_index/_doc/1
{
"group" : "fans",
"user" : [
{
"first" : "John",
"last" : "Smith"
},
{
"first" : "Alice",
"last" : "White"
}
]
}
Run Code Online (Sandbox Code Playgroud)
当我运行无痛脚本时:
ctx._source.user.add({
"first" : "Foo",
"last" : "Bar"
})
Run Code Online (Sandbox Code Playgroud)
它不工作。我试过了,但找不到任何相关文件或其他人发起的讨论。
我正在将 Elasticsearch 脚本从 Groovy 转换为 Painless。该脚本接受一个参数,该参数可以是整数或可转换为整数的字符串(即可以是123或"123")。
在 Groovy 中,执行my_val.toLong()可以很好地转换两者,但该方法在 Painless 中不可用。
是否有任何替代语法可以在 Painless 中执行相同的操作?
我尝试使用(long) my_var进行显式转换,但我得到java.lang.String无法转换为 java.lang.Number
简而言之,我想在 Painless 中执行以下操作并最终实现:
GET _search
{
"script_fields": {
"test": {
"script": {
"lang": "groovy",
"params": {
"my_val1": "123",
"my_val2": 123
},
"source": """
my_val1.toLong() == my_val2.toLong()
"""
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 使用 Logstash,我的目标是如果该文档的时间戳之前尚未被索引,则对该文档建立索引,否则,如果该文档确实存在并且时间戳不在数组中,则附加时间戳数组。我的问题是一个数组附加到一个数组。
即我的输入日志行始终相同,除了我想要附加到 Elastic 中同一文档的时间戳。
这是我的输入数据。
“hash”字段将成为文档 ID(仅作为示例)
{"timestamp":"1534023333", "hash":"1"}
{"timestamp":"1534022222", "hash":"1"}
{"timestamp":"1534011111", "hash":"1"}
Run Code Online (Sandbox Code Playgroud)这是我的 Logstash 配置:
使用 params.event.get 是因为它阻止动态脚本编译
input {
file {
path => "timestamp.json"
start_position => "beginning"
codec => "json"
}
}
filter {
mutate {
split => { "timestamp" => "," }
}
}
output {
elasticsearch {
hosts => ["http://127.0.0.1:9200"]
index => "test1"
document_id => "%{[hash]}"
doc_as_upsert => true
script => 'if(ctx._source.timestamp.contains(params.event.get("timestamp"))) return true; else (ctx._source.timestamp.add(params.event.get("timestamp")))'
action => "update"
retry_on_conflict=>3
} …Run Code Online (Sandbox Code Playgroud)我需要从脚本访问存储在数组中的文档值。数组中项目的顺序很重要。
使用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。
有任何想法吗?