elasticsearch摄取管道:如何递归地修改hashmap中的值

mv1qrgav  于 2021-06-09  发布在  ElasticSearch
关注(0)|答案(2)|浏览(516)

使用摄取管道,我希望遍历hashmap并从所有字符串值(存在下划线的地方)中删除下划线,使键中的下划线保持不变。有些值是数组,必须进一步迭代才能进行相同的修改。
在管道中,我使用一个函数遍历和修改hashmap集合视图的值。

  1. PUT /_ingest/pipeline/samples
  2. {
  3. "description": "preprocessing of samples.json",
  4. "processors": [
  5. {
  6. "script": {
  7. "tag": "remove underscore from sample_tags values",
  8. "source": """
  9. void findReplace(Collection collection) {
  10. collection.forEach(element -> {
  11. if (element instanceof String) {
  12. element.replace('_',' ');
  13. } else {
  14. findReplace(element);
  15. }
  16. return true;
  17. })
  18. }
  19. Collection samples = ctx.samples;
  20. samples.forEach(sample -> { //sample.sample_tags is a HashMap
  21. Collection sample_tags = sample.sample_tags.values();
  22. findReplace(sample_tags);
  23. return true;
  24. })
  25. """
  26. }
  27. }
  28. ]
  29. }

当我模拟管道摄取时,我发现字符串值没有被修改。我哪里出错了?

  1. POST /_ingest/pipeline/samples/_simulate
  2. {
  3. "docs": [
  4. {
  5. "_index": "samples",
  6. "_id": "xUSU_3UB5CXFr25x7DcC",
  7. "_source": {
  8. "samples": [
  9. {
  10. "sample_tags": {
  11. "Entry_A": [
  12. "A_hyphentated-sample",
  13. "sample1"
  14. ],
  15. "Entry_B": "A_multiple_underscore_example",
  16. "Entry_C": [
  17. "sample2",
  18. "another_example_with_underscores"
  19. ],
  20. "Entry_E": "last_example"
  21. }
  22. }
  23. ]
  24. }
  25. }
  26. ]
  27. }
  28. \\Result
  29. {
  30. "docs" : [
  31. {
  32. "doc" : {
  33. "_index" : "samples",
  34. "_type" : "_doc",
  35. "_id" : "xUSU_3UB5CXFr25x7DcC",
  36. "_source" : {
  37. "samples" : [
  38. {
  39. "sample_tags" : {
  40. "Entry_E" : "last_example",
  41. "Entry_C" : [
  42. "sample2",
  43. "another_example_with_underscores"
  44. ],
  45. "Entry_B" : "A_multiple_underscore_example",
  46. "Entry_A" : [
  47. "A_hyphentated-sample",
  48. "sample1"
  49. ]
  50. }
  51. }
  52. ]
  53. },
  54. "_ingest" : {
  55. "timestamp" : "2020-12-01T17:29:52.3917165Z"
  56. }
  57. }
  58. }
  59. ]
  60. }
uujelgoq

uujelgoq1#

您的路径是正确的,但是您正在处理值的副本,并且没有将修改后的值设置回文档上下文 ctx 最终从管道返回。这意味着您将需要跟踪当前的迭代索引——因此对于数组列表,以及散列Map和介于两者之间的所有内容——这样您就可以在深度嵌套的上下文中定位字段的位置。
下面是一个处理字符串和(仅字符串)数组列表的示例。您需要扩展它来处理散列Map(和其他类型),然后可能需要将整个过程提取到一个单独的函数中。但是在java中不能返回多个数据类型,所以这可能很有挑战性。。。

  1. PUT /_ingest/pipeline/samples
  2. {
  3. "description": "preprocessing of samples.json",
  4. "processors": [
  5. {
  6. "script": {
  7. "tag": "remove underscore from sample_tags values",
  8. "source": """
  9. ArrayList samples = ctx.samples;
  10. for (int i = 0; i < samples.size(); i++) {
  11. def sample = samples.get(i).sample_tags;
  12. for (def entry : sample.entrySet()) {
  13. def key = entry.getKey();
  14. def val = entry.getValue();
  15. def replaced_val;
  16. if (val instanceof String) {
  17. replaced_val = val.replace('_',' ');
  18. } else if (val instanceof ArrayList) {
  19. replaced_val = new ArrayList();
  20. for (int j = 0; j < val.length; j++) {
  21. replaced_val.add(val[j].replace('_',' '));
  22. }
  23. }
  24. // else if (val instanceof HashMap) {
  25. // do your thing
  26. // }
  27. // crucial part
  28. ctx.samples[i][key] = replaced_val;
  29. }
  30. }
  31. """
  32. }
  33. }
  34. ]
  35. }
展开查看全部
k2arahey

k2arahey2#

以下是您的脚本的修改版本,它将处理您提供的数据:

  1. PUT /_ingest/pipeline/samples
  2. {
  3. "description": "preprocessing of samples.json",
  4. "processors": [
  5. {
  6. "script": {
  7. "tag": "remove underscore from sample_tags values",
  8. "source": """
  9. String replaceString(String value) {
  10. return value.replace('_',' ');
  11. }
  12. void findReplace(Map map) {
  13. map.keySet().forEach(key -> {
  14. if (map[key] instanceof String) {
  15. map[key] = replaceString(map[key]);
  16. } else {
  17. map[key] = map[key].stream().map(this::replaceString).collect(Collectors.toList());
  18. }
  19. });
  20. }
  21. ctx.samples.forEach(sample -> {
  22. findReplace(sample.sample_tags);
  23. return true;
  24. });
  25. """
  26. }
  27. }
  28. ]
  29. }

结果如下:

  1. {
  2. "samples" : [
  3. {
  4. "sample_tags" : {
  5. "Entry_E" : "last example",
  6. "Entry_C" : [
  7. "sample2",
  8. "another example with underscores"
  9. ],
  10. "Entry_B" : "A multiple underscore example",
  11. "Entry_A" : [
  12. "A hyphentated-sample",
  13. "sample1"
  14. ]
  15. }
  16. }
  17. ]
  18. }
展开查看全部

相关问题