我想在我的文档中以一种不会重复的方式存储标记。
我的文档有一个Tags字段,定义为:
...
"Tags": { "type": "string" }
...我从Python中将标记添加到它的Tags字段:
es.update(index=ES_INDEX, doc_type=ES_DOC_TYPE, id=user_id, body=doc)我的更新文件:
doc = {
"script": {
"lang": "groovy",
"inline": "ctx._source.Tags.addAll(tags)",
"params": {
"tags": [
"c#",
"winforms",
"type-conversion",
"decimal",
"opacity"
]
}
}
}这是可行的,但标记可能是重复的。
--我想在存储标签之前先去复制它们。我基本上希望字段充当一个集合.
下面是我尝试过的(基于这个答案:https://stackoverflow.com/a/17465831/318557)
...
"inline": "ctx._source.Tags.addAll(tags); ctx._source.Tags.unique();",
...但它没有效果。
是否有Groovy解决方案来做到这一点?或者可能是来自Elasticsearch对存储集的支持?
发布于 2017-07-28 18:40:39
问题是在初始化的数组上使用.addAll()。使用REST客户端给了我以下消息:
{
"error": {
"root_cause": [
{
"type": "remote_transport_exception",
"reason": "[DevHunt][192.168.1.98:9300][indices:data/write/update[s]]"
}
],
"type": "illegal_argument_exception",
"reason": "failed to execute script",
"caused_by": {
"type": "script_exception",
"reason": "error evaluating ctx._source.Tags.addAll(tags);",
"script_stack": [],
"script": "",
"lang": "groovy",
"caused_by": {
"type": "null_pointer_exception",
"reason": "Cannot invoke method addAll() on null object"
}
}
},
"status": 400
}解决办法是使用:
...
"inline": "if(ctx._source.Tags == null) {ctx._source.Tags = [];}; ctx._source.Tags.addAll(tags); ctx._source.Tags.unique();",
...发布于 2017-07-28 14:07:54
我不认为这是一个很好的问题。你在检查正确的对象吗?
我有一份被编入索引的文件:
{
"_index": "script",
"_type": "test",
"_id": "AV2Jd1zXM1eNU8lqyoZS",
"_score": 1,
"_source": {
"Tags": [
"tag1",
"decimal"
]
}
}然后打电话:
curl -X POST \
http://127.0.0.1:9200/script/test/AV2Jd1zXM1eNU8lqyoZS/_update \
-d '{
"script": {
"lang": "groovy",
"inline": "ctx._source.Tags.addAll(tags); ctx._source.Tags.unique();",
"params": {
"tags": [
"c#",
"winforms",
"type-conversion",
"decimal",
"opacity",
"aaa",
"aaa"
]
}
}
}'结果:
{
"_index": "script",
"_type": "test",
"_id": "AV2Jd1zXM1eNU8lqyoZS",
"_score": 1,
"_source": {
"Tags": [
"tag1",
"decimal",
"c#",
"winforms",
"type-conversion",
"opacity",
"aaa"
]
}所以groovy在这里工作得很好,你可以用REST客户端来检查它。您可以尝试重新分配集合:
"inline": "ctx._source.Tags = ctx._source.Tags.unique(); ctx._source.Tags += tags.unique();",也许更多的是Python代码的问题?
https://stackoverflow.com/questions/45370511
复制相似问题