2016-02-01 46 views
0

我試圖推某些消息像這樣elasticsearchElasticsearch:es.index()改變時消息被推映射

id=1 
list=asd,bcv mnmn,kjkj, pop asd dgf 

這樣,每個消息具有id字段,它是一個字符串,以及list包含字符串值列表的字段

當我將此推入彈性模式並嘗試在kibana中創建圖表時,默認分析器會踢入並按空格字符拆分我的list。因此它打破了我的價值觀。我試圖創建一個映射爲我的索引

mapping=''' 
{ 
"test": 
    { 
    "properties": { 
     "DocumentID": { 
      "type": "string" 
     }, 
     "Tags":{ 
      "type" : "string", 
      "index" : "not_analyzed" 
     } 
     } 
    } 
}''' 

es = Elasticsearch([{'host': server, 'port': port}]) 
indexName = "testindex"  
es.indices.create(index=indexName, body=mapping) 

所以這應該創建索引與我定義的映射。現在,我只是推動消息

es.index(indexName, docType, messageBody) 

但即使是現在,Kibana打破了我的價值觀!爲什麼映射不適用?

,當我做

GET /testindex/_mapping/test 

我得到

{ 
    "testindex": { 
    "mappings": { 
     "test": { 
     "properties": { 
      "DocumentID": { 
      "type": "string" 
      }, 
      "Tags": { 
      "type": "string" 
      } 
     } 
     } 
    } 
    } 
} 

爲什麼映射的變化?我該如何指定映射類型時,我做

es.index() 

回答

1

你非常接近。您需要在創建索引時提供根目標mappings對象,並且在使用_mapping端點時不需要它,這就是put_mapping工作的原因,而create則沒有。你可以在api中看到。

mapping = ''' 
{ 
    "mappings": { 
     "test": { 
      "properties": { 
       "DocumentID": { 
        "type": "string" 
       }, 
       "Tags": { 
        "type": "string", 
        "index": "not_analyzed" 
       } 
      } 
     } 
    } 
} 
''' 

現在,這將如預期

es.indices.create(index=indexName, body=mapping) 

希望這有助於

+0

感謝很多:) – AbtPst

+0

很高興我能幫助:) – ChintanShah25

0

我能得到正確的映射通過

es.indices.create(index=indexName) 
es.indices.put_mapping(docType, mapping, indexName) 

工作,我不明白爲什麼

es.indices.create(index=indexName, body=mapping) 

沒有工作。這應該按照API工作。