2017-08-29 154 views
0

我正在使用elasticsearch。我看到每個文檔都有元字段_id。我想用這個元字段搜索文檔,因爲我沒有任何其他字段作爲文檔中的唯一字段。但_id是一個字符串,可以有破折號,除非我們添加字段映射爲type :keyword,否則無法搜索破折號。但是可以如here所述。所以現在我想在文檔中添加另一個字段newField,並使其與_id相同。一種方法是:首先創建文檔並將_id分配到該字段並再次保存文檔。但是這將有2個連接,這不是很好。所以我想找到一些解決方案來設置newField,同時創建文檔本身。它甚至有可能嗎?elasticsearch:在創建文檔時將元字段_id複製到其他字段

回答

0

您可以搜索包含破折號文檔:

PUT my_index/tweet/testwith- 
{ 
    "fullname" : "Jane Doe", 
    "text" : "The twitter test!" 
} 

我們剛剛創建的文檔在其ID破折號

GET my_index/tweet/_search 
{ 
    "query": { 
    "terms": { 
     "_id": [ 
     "testwith-" 
     ] 
    } 
    } 
} 

我們尋找具有以下標識的文件: testwith-

{ 
    "took": 9, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 1, 
    "max_score": 1, 
    "hits": [ 
     { 
     "_index": "my_index", 
     "_type": "tweet", 
     "_id": "testwith-", 
     "_score": 1, 
     "_source": { 
      "fullname": "Jane Doe", 
      "text": "The twitter test!" 
     } 
     } 
    ] 
    } 
} 

我們發現它。我們可以搜索那些有內容的文檔。

相關問題