如何在Elasticsearch中使用IngestPlugin根据IngestDocument填充indexableField

huangapple 未分类评论58阅读模式
英文:

How to populate indexableField based on IngestDocument in Elasticsearch using IngestPlugin

问题

{
  "mappings": {
    "properties": {
      "field1": {
        "type": "text",
        "fields": {
          "timestamp": {
            "type": "date",
            "format": "yyyy-MM-dd"
          }
        }
      },
      "field2": {
        "type": "text",
        "fields": {
          "timestamp": {
            "type": "date",
            "format": "yyyy-MM-dd"
          }
        }
      },
      "field3": {
        "type": "text"
      }
    }
  },
  "settings": {
    "default_pipeline": "my_pipeline"
  }
}
@Override
public IngestDocument execute(IngestDocument ingestDocument) throws Exception {
    ingestDocument.setFieldValue("field1.timestamp", "2020-07-27");
    return ingestDocument;
}
{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "cannot set [timestamp] with parent object of type [java.lang.String] as part of path [field1.timestamp]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "cannot set [timestamp] with parent object of type [java.lang.String] as part of path [field1.timestamp]"
  },
  "status": 400
}

在Elasticsearch插件中,有没有办法在索引文档时填充多字段(例如field1.timestamp)的值?

英文:

I am writing IngestPlugin, which needs to populate a timestamp (multi-field) based on fields in IngestDocument. I have defined an index as follows

 {
  "mappings": {
    "properties": {
      "field1": {
        "type": "text",
        "fields": {
          "timestamp": {
            "type": "date",
            "format": "yyyy-MM-dd"
          }
        }
      },
      "field2": {
        "type": "text",
        "fields": {
          "timestamp": {
            "type": "date",
            "format": "yyyy-MM-dd"
          }
        }
      },
      "field3": {
        "type": "text"
      }
    }
  },
  "settings": {
    "default_pipeline": "my_pipeline"
  }
}

I tried to set value for "title.timestamp" using processor like this

@Override
public IngestDocument execute(IngestDocument ingestDocument) throws Exception {
    ingestDocument.setFieldValue("field1.timestamp", "2020-07-27");
    
    return ingestDocument;
}

I got the following error while inserting a document

{

  "error" : {

    "root_cause" : [

      {

        "type" : "illegal_argument_exception",

        "reason" : "cannot set [timestamp] with parent object of type [java.lang.String] as part of path [field1.timestamp]"

      }

    ],

    "type" : "illegal_argument_exception",

    "reason" : "cannot set [timestamp] with parent object of type [java.lang.String] as part of path [field1.timestamp]"

  },

  "status" : 400

}

Is there any way to populate multi-field (field1.timestamp) value on indesting document using Elasticsearch plugin?

答案1

得分: 0

你无法直接填充多字段的子字段。

由于field1的类型是text,你只能设置"field1": "2020-07-27",然后你会得到:

  • field1中的经过分析的text值为"2020-07-27"
  • field1.timestamp中的date值为"2020-07-27"

所以,你只需要简单地执行这个操作:

ingestDocument.setFieldValue("field1", "2020-07-27");
                                    ^                                     
                                    |
                         在这里去掉 .timestamp
英文:

You cannot populate a sub-field of a multi-field directly.

Since field1 is of type text, you can only set "field1": "2020-07-27" and you'll get:

  • the analyzed text value of "2020-07-27" in field1 and
  • the date value of "2020-07-27" in field1.timestamp

So you need to simply do this instead:

ingestDocument.setFieldValue("field1", "2020-07-27");
                                    ^
                                    |
                          remove .timestamp here

huangapple
  • 本文由 发表于 2020年7月27日 21:05:21
  • 转载请务必保留本文链接:https://java.coder-hub.com/63115962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定