2016-06-09 56 views
1

在我的索引「process」中,我有一個類型「logs」。對於這種類型的,我有這樣的mappping:部分動態映射

{ 
    "process": { 
     "mappings": { 
      "logs": { 
       "properties": { 
        "channel": { 
         "type": "string" 
        }, 
        "context": { 
         "type": "object" 
        }, 
        "datetime": { 
         "type": "date", 
         "format": "dateOptionalTime" 
        }, 
        "extra": { 
         "type": "object" 
        "level": { 
         "type": "long" 
        }, 
        "level_name": { 
         "type": "string" 
        }, 
        "message": { 
         "type": "string" 
        } 
       } 
      } 
     } 
    } 
} 

的事情是,在「背景」屬性和「額外」的屬性是動態的領域,每個文檔都可以有不同數量不同的額外&上下文子元素名。 當我設置這個映射,然後我檢查http://localhost:9200/process/_mapping/logs,我看到這個非常映射。所以它的工作。此外,設置的答案是{acknowledge: true}。到現在爲止還挺好。

然後,我添加了幾個文件,或者至少我嘗試。第一個文件改變ES映射!

新的映射是:

{ 
    "process" : { 
    "mappings" : { 
     "logs" : { 
     "properties" : { 
      "channel" : { 
      "type" : "string" 
      }, 
      "context" : { 
      "properties" : { 
       "columns" : { 
       "type" : "string" 
       }, 
       "count" : { 
       "type" : "long" 
       }, 
       "errorMessage" : { 
       "type" : "string" 
       }, 
       "serviice" : { 
       "type" : "string" 
       } 
      } 
      }, 
      "datetime" : { 
      "type" : "date", 
      "format" : "dateOptionalTime" 
      }, 
      "extra" : { 
      "properties" : { 
       "uid" : { 
       "type" : "string" 
       } 
      } 
      }, 
      "level" : { 
      "type" : "long" 
      }, 
      "level_name" : { 
      "type" : "string" 
      }, 
      "message" : { 
      "type" : "string" 
      } 
     } 
     } 
    } 
    } 
} 

當我嘗試添加一個新的文檔,它不符合這個新的映射,並觸發此錯誤:

MapperParsingException[object mapping [context] trying to serialize a value with no field associated with it, current value [pid]] 

所以我很清楚遺漏了什麼。

  1. 爲什麼我的映射被覆蓋?我試圖刪除整個索引,並從頭開始構建一切,同樣的行爲。
  2. 是我想實現的可能嗎?還是我有錯誤的方法?

編輯:被插入,似乎更新映射文件:

{ 
    "message": "Starting background process `{service}`, registered under the ID ", 
    "context": { 
    "id": null, 
    "serviice": "fiduceo.import" 
    }, 
    "level": 250, 
    "level_name": "NOTICE", 
    "channel": "process", 
    "datetime": "2016-06-09T11:23:42.304859+02:00", 
    "extra": { 
    "uid": "d1cb925" 
    } 
} 

,誰失敗了一句:

{ 
    "message": "Background process created (pid: {pid})", 
    "context": [ 
    "pid", 
    18871 
    ], 
    "level": 250, 
    "level_name": "NOTICE", 
    "channel": "process", 
    "datetime": "2016-06-09T11:23:41.519456+02:00", 
    "extra": { 
    "uid": "09fe183" 
    } 
} 
+0

1.我不會說你的映射被覆蓋,但更新。這意味着所有定義的字段保持原樣,並且添加缺少的字段(不在映射中但在文檔中)。 2.你想實現的目標一般是可能的,但是你與字段映射和JSON輸入有一些衝突。請提供您的JSON文檔,該文檔對於此映射而言失敗。 – Hansa

回答

1

上下文字段包含無效JSON

它應該看起來像這樣:

"context": { 
    "pid": 18871 
    }, 

相比,你的版本:

"context": [ 
    "pid", 
    18871 
    ] 

編輯:爲了給一個完整的答案(和重複一下寫在註釋):

  1. 你映射不會被覆蓋,但會被更新。這發生在包含在新索引JSON文檔中但不包含初始映射的字段中。
  2. 這是可能的。你的一般方法是正確的。
+0

我已經注意到了,並且已經修復了它。由於潛在的問題仍然存在,我沒有顯示更正的版本。爲什麼我不能添加無鍵(隱式數字索引)的子元素?這也可能意味着context.foo元素必須始終包含相同的數據結構? – JesusTheHun

+1

它是否仍然是您收到的完全相同的錯誤消息? 您可以忽略新文檔中的映射值,但不允許將一個數字和一個字符串存儲到同一個字段中。 – Hansa

+1

檢查有關「正確」版本的缺陷的更新答案。第一次使用[]存儲數組,第二次使用{ – Hansa