簡體   English   中英

Elasticsearch 2.x索引映射_id

[英]Elasticsearch 2.x index mapping _id

我(愉快地)運行ElasticSearch 1.x一年以上。 現在是時候進行一些升級了-升級到2.1.x。 應該關閉節點,然后再(一對一)打開節點。 似乎很容易。
但是后來我遇到了麻煩。 主要問題是字段_uid ,我創建了自己的字段,以便我從另一個隨機對象(通過散列值)知道文檔的確切位置。 這樣,我知道只有確切的一個將被返回。 升級期間我得到了

MapperParsingException[Field [_uid] is a metadata field and cannot be added inside a document. Use the index API request parameters.]

但是,當我嘗試將以前的_uid映射到_id (這也應該足夠好)時,我會得到類似的結果。

我之所以使用_uid參數,是因為查找時間比termsQuery(或類似查詢)低很多。
我如何仍可以在每個文檔中使用_uid_id字段來快速(精確)查找某些特定文檔? 請注意,我必須一次調用成千上萬個確切的名稱,因此我需要一個類似於查詢的ID。 也可能會出現文檔的_uid_id不存在(在那種情況下,我想要像現在一樣,出現“假”結果)

注意:從1.x到2.x的升級相當大(過濾器消失了,名稱中沒有點,沒有對_xxx默認訪問_xxx

更新(無濟於事):
使用以下命令更新_uid_id的映射:

final XContentBuilder mappingBuilder = XContentFactory.jsonBuilder().startObject().startObject(type).startObject("_id").field("enabled", "true").field("default", "xxxx").endObject()
            .endObject().endObject();
 CLIENT.admin().indices().prepareCreate(index).addMapping(type, mappingBuilder)
                .setSettings(Settings.settingsBuilder().put("number_of_shards", nShards).put("number_of_replicas", nReplicas)).execute().actionGet();

結果是:

MapperParsingException[Failed to parse mapping [XXXX]: _id is not configurable]; nested: MapperParsingException[_id is not configurable];

更新:將名稱更改為_id而不是_uid因為后者是根據_type_id構建的。 因此,我需要能夠寫入_id

由於似乎沒有辦法設置_uid_id我將發布解決方案。 我將所有具有_uid文檔映射到uid (用於內部引用)。 在某個時候,您可以設置相關id

要批量插入id文檔,您可以:

final BulkRequestBuilder builder = client.prepareBulk();
for (final Doc doc : docs) {
    builder.add(client.prepareIndex(index, type, doc.getId()).setSource(doc.toJson()));
}
final BulkResponse bulkResponse = builder.execute().actionGet();

注意第三個參數,這個參數可以為null (或者是兩個值的參數,然后id將由ES生成)。
要通過id獲取一些文檔,您可以:

final List<String> uids = getUidsFromSomeMethod(); // ids for documents to get
final MultiGetRequestBuilder builder = CLIENT.prepareMultiGet();
builder.add(index_name, type, uids);
final MultiGetResponse multiResponse = builder.execute().actionGet();
// in this case I simply want to know whether the doc exists
if (only_want_to_know_whether_it_exists){
    for (final MultiGetItemResponse response : multiResponse.getResponses()) {
        final boolean exists = response.getResponse().isExists();
        exist.add(exists);
    }
} else {
    // retrieve the doc as json
    final String string = builder.getSourceAsString();
    // handle JSON
}

如果只想要1:

client.prepareGet().setIndex(index).setType(type).setId(id);

做-單個更新-使用curlmapping-id-field (注意:精確副本):

# Example documents
PUT my_index/my_type/1
{
  "text": "Document with ID 1"
}

PUT my_index/my_type/2
{
  "text": "Document with ID 2"
}

GET my_index/_search
{
  "query": {
    "terms": {
      "_id": [ "1", "2" ] 
    }
  },
  "script_fields": {
    "UID": {
      "script": "doc['_id']" 
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM