elasticsearch 如何将对象重新索引为文本?

vcirk6k6  于 5个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(64)

如何重新索引对象类型字段,如:

{
  "Foo": {
    "properties": {
      "ID": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "Name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

字符串
到其文本表示:

{
  "Foo": {
    "type": "text",
    "index": "false"
  }
}


使用raw reindex,我得到一个错误:Can't get text on a START_OBJECT at 1:141

lo8azlld

lo8azlld1#

我有一个类似的问题,只发现了这个问题,所以这是我的解决方案后,一些挖掘。
源中的数据不是字符串,因此Elasticsearch将无法找到内容。您需要将内容扁平化并将'Foo'字段替换为扁平化的值。

POST _reindex
{
  "source": {
    "index": "source_index"
  },
  "dest": {
    "index": "target_index"
  },
  "script": {
    "lang": "painless",
    "source": """
      if (ctx._source.Foo != null {
        String flatText = '[';
        for (def item : ctx._source.Foo) {
          List keys = new ArrayList(item.keySet());
          Collections.sort(keys);
          flatText += '{';
          for (def key : keys) {
            flatText += key + ':' + item[key] + ',';
          }
          flatText = flatText.substring(0, flatText.length() - 1); // Remove last comma
          flatText += '},';
        }
        flatText = flatText.substring(0, flatText.length() - 1); // Remove last comma
        flatText += ']';
        ctx._source.Foo = flatText;
      }
    """
  }
}

字符串
请注意,您必须检查ctx._source.Foo != null,否则脚本将失败。
如果'Foo'在另一个字段中,则需要检查整个层次结构,如
(ctx._source.Higher != null && ctx._source.Higher.Foo != null)

相关问题