小编典典

Mongo-connector是否支持在插入Elasticsearch之前添加字段?

elasticsearch

  • 我在mongoDB中有很多文档。Mongo-connector将这些数据插入elasticsearch。在插入ES之前,有什么方法可以在文档中添加额外的字段,然后再插入elasticsearch?mongo-connector中有什么方法可以完成上述操作吗?

更新

根据您的 UPDATE 3, 我创建了映射,这是正确的吗?

PUT my_index2
{
 "mappings":{
  "my_type2": {
  "transform": {
  "script": {
    "inline": "if (ctx._source.geopoint.alt) ctx._source.geopoint.remove('alt')",
    "lang": "groovy"
  }
},
"properties": {
  "geopoint": {
    "type": "geo_point"
  }
 }
}
}
}

错误

这是我尝试插入映射时不断出现的错误

{
   "error": {
  "root_cause": [
     {
        "type": "script_parse_exception",
        "reason": "Value must be of type String: [script]"
     }
  ],
  "type": "mapper_parsing_exception",
  "reason": "Failed to parse mapping [my_type2]: Value must be of type String: [script]",
  "caused_by": {
     "type": "script_parse_exception",
     "reason": "Value must be of type String: [script]"
  }
   },
   "status": 400
}

更新2

现在,将插入映射并获得确认为true。但是,当尝试在其抛出错误下方插入json数据时。

PUT my_index2/my_type2/1
{
 "geopoint": {
        "lon": 48.845877,
        "lat": 8.821861,
        "alt": 0.0
        }
}

UPDATE2错误

{
   "error": {
  "root_cause": [
     {
        "type": "mapper_parsing_exception",
        "reason": "failed to parse"
     }
  ],
  "type": "mapper_parsing_exception",
  "reason": "failed to parse",
  "caused_by": {
     "type": "illegal_argument_exception",
     "reason": "failed to execute script",
     "caused_by": {
        "type": "script_exception",
        "reason": "scripts of type [inline], operation [mapping] and lang [groovy] are disabled"
     }
  }
  },
  "status": 400
}

错误1更新2

添加script.inline:true后,尝试插入数据,但出现以下错误。

{
   "error": {
  "root_cause": [
     {
        "type": "parse_exception",
        "reason": "field must be either [lat], [lon] or [geohash]"
     }
  ],
  "type": "mapper_parsing_exception",
  "reason": "failed to parse",
  "caused_by": {
     "type": "parse_exception",
     "reason": "field must be either [lat], [lon] or [geohash]"
  }
   },
   "status": 400
}

阅读 316

收藏
2020-06-22

共1个答案

小编典典

mongo-connector旨在将Mongo数据库与另一个目标系统(例如ES,Solr或另一个Mongo
DB)进行同步。同步意味着1:1复制,所以我不知道mongo-connector在复制过程中如何丰富文档(这也不是它的意图)。

但是,在ES
5中,我们很快将能够使用摄取节点,在该节点中,我们将能够定义处理管道,其目的是在文档建立索引之前对其进行充实。

更新

可能有一种修改formatters.py文件的方法。

transform_value我要添加一个案例来处理Geopoint

    if isinstance(value, dict):
        return self.format_document(value)
    elif isinstance(value, list):
        return [self.transform_value(v) for v in value]

    # handle Geopoint class
    elif isinstance(value, Geopoint):
        return self.format.document({'lat': value['lat'], 'lon': value['lon']})

    ...

更新2

让我们通过修改transform_element函数来尝试另一种方法(在第104行):

def transform_element(self, key, value):
    try:
        # add these next two lines
        if key == 'GeoPoint':
            value = {'lat': value['lat'], 'lon': value['lon']}
        # do not modify the initial code below
        new_value = self.transform_value(value)
        yield key, new_value
    except ValueError as e:
        LOG.warn("Invalid value for key: %s as %s"
                 % (key, str(e)))

更新3

您可能要尝试的另一件事是添加一个transform。我之前没有提到它的原因是它在ES 2.0中已被弃用,但是在ES
5.0中,您将具有摄取节点,并且能够在摄取时使用处理器remove处理它

您可以这样定义映射:

PUT my_index2
{
  "mappings": {
    "my_type2": {
      "transform": {
        "script": "ctx._source.geopoint.remove('alt'); ctx._source.geopoint.remove('valid')"
      },
      "properties": {
        "geopoint": {
          "type": "geo_point"
        }
      }
    }
  }
}

注意:确保实现动态脚本,加入script.inline: trueelasticsearch.yml并重新启动ES节点。

将会发生的是,该alt字段在存储的字段中仍将是可见的,_source但不会被索引,因此不会发生任何错误。

使用ES 5,您只需使用remove处理器创建管道,如下所示:

PUT _ingest/pipeline/geo-pipeline
{
  "description" : "remove unsupported altitude field",
  "processors" : [
    {
      "remove" : {
        "field": "geopoint.alt"
      }
    }
  ]
}
2020-06-22