[英]ElasticSearch indexing with nested collections in document
我一直在努力嘗試在ElasticSearch中將文檔索引到全新索引中的問題。 我的文檔看起來像這樣:
{
"id": "",
"name": "Process to run batch of steps",
"defaultErrorStep": {
"id": "d44fdeae-80ff-4509-8504-9dfbd7284631",
"name": "General Error Handler",
"type": "ERROR",
"reference": "error",
"onError": "DEFAULT"
},
"startingStep": "one",
"steps": [
{
"id": "d44fdeae-80ff-4509-8504-9dfbd7284631",
"name": "Step One",
"type": "CHAIN",
"reference": "one",
"onComplete": "two",
"onError": "DEFAULT",
"parameterKeys": {
"param-a": "value-a",
"param-b": "value-b",
"param-c": "value-c"
}
},
{
"id": "d44fdeae-80ff-4509-8504-9dfbd7284631",
"name": "Step Two",
"type": "CHAIN",
"reference": "two",
"onComplete": "two",
"onError": "DEFAULT",
"parameterKeys": {
"param-a": "value-a",
"param-b": "value-b",
"param-c": "value-c"
}
},
{
"id": "d44fdeae-80ff-4509-8504-9dfbd7284631",
"name": "Step Three",
"type": "BOOLEAN",
"reference": "three",
"onTrue": "four",
"onFalse": "five",
"onError": "DEFAULT",
"parameterKeys": {
"param-a": "value-a",
"param-b": "value-b",
"param-c": "value-c"
}
},
{
"id": "d44fdeae-80ff-4509-8504-9dfbd7284631",
"name": "Step Four",
"type": "LOOP",
"startingStep": "seven",
"steps": [
{
"id": "d44fdeae-80ff-4509-8504-9dfbd7284631",
"name": "Step Two",
"type": "CHAIN",
"reference": "six",
"onComplete": "seven",
"onError": "DEFAULT",
"parameterKeys": {
"param-a": "value-a",
"param-b": "value-b",
"param-c": "value-c"
}
},
{
"id": "d44fdeae-80ff-4509-8504-9dfbd7284631",
"name": "Step Five",
"type": "FINISH_VOID",
"end": false,
"reference": "seven",
"onError": "DEFAULT",
"parameterKeys": {
"param-a": "value-a",
"param-b": "value-b",
"param-c": "value-c"
}
}
],
"reference": "four",
"onComplete": "five",
"onError": "DEFAULT",
"parameterKeys": {
"param-a": "value-a",
"param-b": "value-b",
"param-c": "value-c"
}
},
{
"id": "d44fdeae-80ff-4509-8504-9dfbd7284631",
"name": "Step Five",
"type": "FINISH",
"end": true,
"reference": "five",
"onError": "DEFAULT",
"parameterKeys": {
"param-a": "value-a",
"param-b": "value-b",
"param-c": "value-c"
}
}
],
"configuration": {
"settings": {
"property-a": "a",
"property-b": "b",
"property-c": "c",
"property-d": "d",
"property-z": "z123"
}
}
}
我的問題是,由於屬性“ steps”的嵌套結構及其在其中具有“ steps”的循環對象的能力,在嘗試建立索引時,我遇到了字段重復的問題。 我理解我的文檔失敗的原因(但我認為),但是我需要對它們全部進行索引。 當我嘗試為文檔建立索引時,出現以下錯誤:
ElasticsearchException[Elasticsearch exception [type=json_parse_exception, reason=Duplicate field 'type'\n at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper@84a0697; line: 1, column: 186]]]
同樣,我理解為什么這是一個問題,但是我認為我可以使用索引中的映射來解決這個問題。 我嘗試過nested
對象類型,展flattened
對象類型,甚至在步驟字段上設置index:false
只是為了查看是否可以放入文檔。但是,沒有機會。 我知道這將是一個簡單的修復程序,在我看不到的地方,但是沒有人對我可以嘗試將該文檔建立索引的想法有任何想法。
我正在通過最新的Java SDK版本使用ElasticSearch 7.3.1。 我現在已經繞過了Java代碼,僅使用POSTMAN發送索引命令,但是仍然遇到相同的問題。
下面是我嘗試過的一種映射示例。
{
"_source" : {
"enabled": true
},
"properties" : {
"name": {
"type": "text",
"fields": {
"raw":{"type": "keyword"}
}
},
"steps":{
"type":"nested",
"properties":{
"steps":{
"type":"flattened",
"index":false
}
}
},
"configuration.settings":{"type":"flattened"}
}
}
以及覆蓋“ defaultErrorStep”對象的更明確的映射。
{
"_source" : {
"enabled": true
},
"properties" : {
"name": {
"type": "text",
"fields": {
"raw":{"type": "keyword"}
}
},
"defaultErrorStep":{
"type":"object",
"properties":{
"id":{"type":"text"},
"name":{"type":"text"},
"type":{"type":"text"},
"reference":{"type":"text"},
"onError":{"type":"text"}
}
},
"steps":{
"type":"nested",
"properties":{
"id":{"type": "text"},
"name":{
"type": "text",
"fields": {
"raw":{"type": "keyword"}
}
},
"type":{"type": "text"},
"reference":{"type": "text"},
"onComplete":{"type": "text"},
"onError":{"type": "text"},
"parameterKeys":{"type": "object"},
"onTrue":{"type": "text"},
"onFalse":{"type": "text"},
"startingStep":{"type": "text"},
"steps":{
"type":"nested",
"properties":{
"id":{"type": "text"},
"name":{
"type": "text",
"fields": {
"raw":{"type": "keyword"}
}
},
"type":{"type": "text"},
"reference":{"type": "text"},
"onComplete":{"type": "text"},
"onError":{"type": "text"},
"parameterKeys":{"type": "object"},
"onTrue":{"type": "text"},
"onFalse":{"type": "text"},
"startingStep":{"type": "text"},
"steps":{
"type": "flattened",
"index":false
},
"end":{"type": "boolean"}
}
},
"end":{"type": "boolean"}
}
},
"configuration.settings":{"type":"flattened"}
}
}
還請記住,文檔的性質是概述邏輯的流程/工作流程,並且結構是關鍵,我還要說一下有效的JSON。 因此,理論上,如果必須,steps屬性可以嵌套3、4、10個級別。 因此,理想情況下,我不想每次在數據中添加新級別時都更新映射。
任何人可以給我的幫助,以使該文檔建立索引將不勝感激。
謝謝,
編輯:
從那以后,我從索引中刪除了我的顯式映射,並讓動態映射接管了所有我的對象都適合動態映射支持的基本類型。 這已經成功了,我可以使用無限嵌套的步驟索引上面顯示的文檔,沒有問題。 然后,我使用JAVA SDK對相同的文檔結構嘗試了相同的操作,但由於相同的重復字段異常而失敗。 這向我表明問題出在JAVA SDK,而不是Elasticsearch本身固有的問題。
在我的情況下,動態映射是更好的選擇,因為我無法控制最終可以達到多少級。
有沒有人遇到過與基本產品不同的SDK問題?
我正在運行Elastic 7.3.1,並通過以下索引映射,我能夠在嵌套類型內部成功創建具有嵌套類型的索引。
PUT new_index_1
{
"mappings": {
"_source": {
"enabled": true
},
"properties": {
"name": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
},
"steps": {
"type": "nested",
"properties": {
"steps": {
"type": "flattened",
"index": false
}
}
},
"configuration.settings": {
"type": "flattened"
}
}
}
}
創建索引后對我也適用
PUT new_index_2
{
"mappings": {
"_source": {
"enabled": true
},
"properties": {
"name": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
},
"steps": {
"type": "nested",
"properties": {
"steps": {
"type": "nested"
}
}
}
}
}
}
索引文件
POST new_index_1/_doc
{
"name": "ajay",
"steps": [
{
"test": "working",
"steps": [
{
"name": "crow"
}
]
}
]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.