2017-02-15 53 views
0

你好Elasticsearch Gurus在那裏。Elasticsearch映射和字段類型

考慮以下指標和文檔類型: 本地主機:9200/myindex/mydoctype

我現在有這個指標定義:

{ 
    "myindex": { 
    "aliases": {}, 
    "mappings": { 
     "mydoctype": { 
     "properties": { 
      "theNumber": { 
      "type": "integer" 
      }, 
      "theString": { 
      "type": "string" 
      } 
     } 
     } 
    }, 
    "settings": { 
     "index": { 
     "creation_date": "1487158714808", 
     "number_of_shards": "5", 
     "number_of_replicas": "1", 
     "version": { 
      "created": "1070599" 
     }, 
     "uuid": "cm2OtivhTO-RjuZPeHvL1w" 
     } 
    }, 
    "warmers": {} 
    } 
} 

,我能夠將這份文件加入:

{ 
    "theNumber" : 0, 
    "theString" : "zero" 
} 

但是我沒想到,就是,我還可以將這份文件加入:

{ 
    "theNumber" : 3.1418, 
    "theString" : 3, 
    "fiefoe" : "fiefoe" 
} 

...其中的字段類型不匹配。 以及有一個新的領域/專欄介紹。 由於我爲索引定義的映射,我並不期待這種行爲。

這是否與Elasticsearch無模式有關? 是否可以將Elasticsearch設置爲只接受爲該索引添加的每個文檔的那些類型和字段? 這是彈性搜索映射的工作原理嗎? (也許我不知道呵呵呵)

感謝=)

回答

2

Elasticsearch使用動態映射,所以當它發現沒有在映射存在的領域,它通過猜測它的類型嘗試建立索引。 您可以通過在根對象的映射中使用dynamic: false來禁用此行爲。在這種情況下,ElasticSearch將忽略未映射的字段。

{ 
    "myindex": { 
    "aliases": {}, 
    "mappings": { 
     "mydoctype": { 
     "dynamic": false, <----- 

     "properties": { 
      "theNumber": { 
      "type": "integer" 
      }, 
      "theString": { 
      "type": "string" 
      } 
     } 
     } 
    }, 
    "settings": { 
     "index": { 
     "creation_date": "1487158714808", 
     "number_of_shards": "5", 
     "number_of_replicas": "1", 
     "version": { 
      "created": "1070599" 
     }, 
     "uuid": "cm2OtivhTO-RjuZPeHvL1w" 
     } 
    }, 
    "warmers": {} 
    } 
} 

另外,如果你想拋出時未映射場試圖進行索引例外,您可以使用dynamic:strict

此文檔是here

+0

非常感謝@christinabo。這真的很有幫助。 –

+0

@ArtanisZeratul我希望它能解決這個問題。 – christinabo

+0

請在下方檢查我的答案,這可能對您也有幫助。謝謝。 –

0

請允許我回答我的問題...

此設置在這種情況下,工作對我來說:

API網址:本地主機:9200/myindex/_mapping/mydoctype

HTTP正文:

{ 
    "mydoctype" : { 
     "dynamic": "strict", 
     "properties" : { 
      "theNumber" : {"type" : "integer"}, 
      "theString" : {"type" : "string"}, 
      "stash": { 
        "type":  "object", 
        "dynamic": false 
       } 
     } 
    } 
} 

然後我嘗試添加該對象:

{ 
    "theNumber" : 5.55555, 
    "theString" : 5, 
    "fiefoe" : "fiefoe" 
} 

我得到這樣的響應:

{ 
    "error": "StrictDynamicMappingException[mapping set to strict, dynamic introduction of [fiefoe] within [mydoctype] is not allowed]", 
    "status": 400 
} 

感謝=)!

P.S. 參考: https://www.elastic.co/guide/en/elasticsearch/guide/1.x/dynamic-mapping.html

相關問題