英文:
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"
infield1
and - the
date
value of"2020-07-27"
infield1.timestamp
So you need to simply do this instead:
ingestDocument.setFieldValue("field1", "2020-07-27");
^
|
remove .timestamp here
专注分享java语言的经验与见解,让所有开发者获益!
评论