当前位置: 首页 > 面试题库 >

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

姬捷
2023-03-14
问题内容
  • 我在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
}

问题答案:

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"
      }
    }
  ]
}


 类似资料:
  • 我试图通过将每个数字添加到整数列表来将输入的数字拆分为其数字。我必须输入一个非整数,以阻止扫描仪寻找更多的整数。但是,当我输入一个非整数时,我总是得到这个错误: 我知道这是因为我期望一个int,但得到另一种类型的输入,但我不知道为什么会这样。不应该阻止这种情况发生吗? 编辑:抱歉,我忘记指定注释掉的行是导致问题的行。

  • WebSocket 使用一种被称作“Upgrade handshake(升级握手)”的机制将标准的 HTTP 或HTTPS 协议转为 WebSocket。因此,使用 WebSocket 的应用程序将始终以 HTTP/S 开始,然后进行升级。这种升级发生在什么时候取决于具体的应用;可以在应用启动的时候,或者当一个特定的 URL 被请求的时候。 在我们的应用中,仅当 URL 请求以“/ws”结束时,我

  • Groovy支持字符串插值操作。这是一个例子 Java 8是否像Groovy那样支持字符串插值? 任何答案,不胜感激。谢谢

  • 我尝试使用r2dbc执行批处理插入。 我已经看到,使用spring boot中的DatabaseClient,这还不可能实现。我尝试使用R2DBC SPI语句和and方法来实现这一点,如下所示: 我在日志上看到完成了两个插入请求。 添加是执行批更新还是只运行两个请求? 谢谢

  • 我在查阅Spring Data Elasticsearch store的文档时,遇到了以下问题: 有关反应性支持的更多详细信息,请参阅特定于存储的留档。 虽然我已经在elasticsearch商店留档中,但我没有找到任何关于反应性支持的进一步信息。我在哪里可以找到关于这方面的进一步信息?

  • 问题内容: 以下代码以相同的插入顺序为我提供了输出。我阅读了Javadoc,他们甚至没有谈论插入顺序。有人可以帮助我获取正确的信息。 问题答案: 不,不是的。要保留插入顺序,请改用(javadoc)。 而且,现在优先于,因为它具有不必要的并发开销。(请参见HashMap和Hashtable之间的区别?。)