首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Elasticsearch:存储没有重复的标记

Elasticsearch:存储没有重复的标记
EN

Stack Overflow用户
提问于 2017-07-28 10:01:15
回答 2查看 1.4K关注 0票数 0

我想在我的文档中以一种不会重复的方式存储标记。

我的文档有一个Tags字段,定义为:

代码语言:javascript
复制
...
"Tags": { "type": "string" }
...

我从Python中将标记添加到它的Tags字段:

代码语言:javascript
复制
es.update(index=ES_INDEX, doc_type=ES_DOC_TYPE, id=user_id, body=doc)

我的更新文件:

代码语言:javascript
复制
doc = {
  "script": {
    "lang": "groovy",
    "inline": "ctx._source.Tags.addAll(tags)",
    "params": {
      "tags": [
        "c#",
        "winforms",
        "type-conversion",
        "decimal",
        "opacity"
      ]
    }
  }
}

这是可行的,但标记可能是重复的。

--我想在存储标签之前先去复制它们。我基本上希望字段充当一个集合.

下面是我尝试过的(基于这个答案:https://stackoverflow.com/a/17465831/318557)

代码语言:javascript
复制
...
"inline": "ctx._source.Tags.addAll(tags); ctx._source.Tags.unique();",
...

但它没有效果。

是否有Groovy解决方案来做到这一点?或者可能是来自Elasticsearch对存储集的支持?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-28 18:40:39

问题是在初始化的数组上使用.addAll()。使用REST客户端给了我以下消息:

代码语言:javascript
复制
{
  "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
}

解决办法是使用:

代码语言:javascript
复制
...
"inline": "if(ctx._source.Tags == null) {ctx._source.Tags = [];}; ctx._source.Tags.addAll(tags); ctx._source.Tags.unique();",
...
票数 0
EN

Stack Overflow用户

发布于 2017-07-28 14:07:54

我不认为这是一个很好的问题。你在检查正确的对象吗?

我有一份被编入索引的文件:

代码语言:javascript
复制
{
    "_index": "script",
    "_type": "test",
    "_id": "AV2Jd1zXM1eNU8lqyoZS",
    "_score": 1,
    "_source": {
        "Tags": [
            "tag1",
            "decimal"
        ]
    }
}

然后打电话:

代码语言:javascript
复制
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"
      ]
    }
  }
}'

结果:

代码语言:javascript
复制
{
    "_index": "script",
    "_type": "test",
    "_id": "AV2Jd1zXM1eNU8lqyoZS",
    "_score": 1,
    "_source": {
        "Tags": [
            "tag1",
            "decimal",
            "c#",
            "winforms",
            "type-conversion",
            "opacity",
            "aaa"
        ]
}

所以groovy在这里工作得很好,你可以用REST客户端来检查它。您可以尝试重新分配集合:

代码语言:javascript
复制
"inline": "ctx._source.Tags = ctx._source.Tags.unique(); ctx._source.Tags += tags.unique();",

也许更多的是Python代码的问题?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45370511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档