在Elasticsearch中将一个字段的值拆分为多个字段的值

jjjwad0x  于 5个月前  发布在  ElasticSearch
关注(0)|答案(2)|浏览(73)

我使用gdal和ogr将osm数据导入到elasticsearch的索引中。现在数据集看起来像这样:

{
            "_index": "points",
            "_id": "ttZCjIwB1FY7TO4ET-AK",
            "_score": 6.103035,
            "_source": {
                "ogc_fid": 24862,
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        8.7037536,
                        48.8916509
                    ]
                },
                "osm_id": "3330289083",
                "name": "Amt für Umweltschutz",
                "other_tags": "\"addr:city\"=>\"Pforzheim\",\"addr:country\"=>\"DE\",\"addr:housenumber\"=>\"9\",\"addr:postcode\"=>\"75175\",\"addr:street\"=>\"Östliche Karl-Friedrich-Straße\",\"government\"=>\"environment\",\"office\"=>\"government\""
            }
        }

字符串
由于OSM数据结构,地址数据已导入字段other_tags
为了创建更简单的elasticsearch查询,我更喜欢这样的索引结构:

{
            "_index": "points",
            "_id": "ttZCjIwB1FY7TO4ET-AK",
            "_score": 6.103035,
            "_source": {
                "ogc_fid": 24862,
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        8.7037536,
                        48.8916509
                    ]
                },
                "osm_id": "3330289083",
                "name": "Amt für Umweltschutz",
                "city": "Pforzheim",
                "country": "DE",
                "housenumber": "9",
                "postcode": "75175",
                "street": "Östliche Karl-Friedrich-Straße"
            }
        }


我读到过可以用logstash来改变数据,并且有很多例子可以说明一个mutate filter函数是什么样子的。我找了4个小时,但是我找不到如何使用Logstash的mutate filter。
有谁能告诉我实现目标的具体步骤是什么?我需要安装一些东西吗?我需要连接到elasticsearch服务器并使用一些命令吗?或者可以通过请求elasticsearch API来实现吗?
谢谢

x4shl7ld

x4shl7ld1#

如果您使用Logstash导入此数据,则最好使用kv filter来解析other_tags中的地址数据,然后使用mutate/rename过滤器从字段名称中删除addr:前缀,如下所示:

filter {
  ...
  kv {
     source => 'other_tags'
     field_split => ','
     value_split_pattern => '=>'
     remove_char_key => '"'
     remove_char_value => '"'
  }
  mutate {
    rename => { 
      "addr:city" => "city" 
      "addr:country" => "country" 
      "addr:housenumber" => "housenumber" 
      "addr:postcode" => "postcode" 
      "addr:street" => "street" 
    }
  }
}

字符串

n6lpvg4x

n6lpvg4x2#

我发现在重新索引索引时可以改变索引结构,在我的情况下,这也很好。

POST {{url-elasticsearch}}/_reindex
{
  "source": {
    "index": "points"
  },
  "dest": {
    "index": "points-wellformed"
  },
  "script": {
    "lang":"painless",
    "source": "if (ctx._source['other_tags'] != null) {def fieldSplit = ctx._source['other_tags'].splitOnToken(','); Map m = new HashMap(); for (item in fieldSplit) { def valueSplit = item.splitOnToken('=>'); if(valueSplit.length == 2) {m.put(valueSplit[0].replace('\"',''), valueSplit[1].replace('\"',''));}} for (entry in m.entrySet()){ctx._source[entry.getKey()] = entry.getValue(); }}"
,  
  }
}

字符串
如果有人知道更好的方法,我仍然欣赏可能更快,更优雅的解决方案。

相关问题