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

时间:2016-04-21 14:16:19

标签: mongodb elasticsearch elasticsearch-2.0

  • 我在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
        }
}         

ERDOR FOR 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
}

ERROR 1 for FORDATE 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
}

1 个答案:

答案 0 :(得分:3)

mongo-connector旨在将Mongo数据库与另一个目标系统(例如ES,Solr或其他Mongo DB)同步。同步意味着1:1复制,因此我不知道mongo-connector在复制期间是否丰富了文档(并且它也不是它的意图)。

但是,在ES 5中,我们很快就可以使用ingest nodes,我们可以在其中定义processing pipelines,其目标是在索引之前丰富文档。

<强>更新

通过修改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 function(第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中,您将拥有摄取节点,并且您可以在摄取时使用a来处理它。 remove processor

您可以像这样定义您的映射:

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: true添加到elasticsearch.yml并重新启动ES节点。

将会发生alt字段仍然在存储的_source中可见,但它不会被编入索引,因此不会发生错误。

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

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