Elasticsearch 2.x 索引映射_id
Elasticsearch 2.x index mapping _id
我运行ElasticSearch1.x(高兴)一年多了。现在是升级到 2.1.x 的时候了。节点应该关闭然后(一个接一个)再次打开。看起来很简单。
但是后来我运行陷入了困境。主要问题是我自己创建的字段 _uid
,这样我就可以从另一个 运行dom 中知道文档的确切位置(通过散列值)。这样我就知道只会返回确切的那个。在升级期间我得到
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
不存在(在这种情况下,我想要像现在一样的 'false-like' 结果)
注意:从 1.x 到 2.x 的升级非常大(过滤器消失,名称中没有点,没有默认访问 _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
我将 post 我的解决方案。我将所有具有 _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);
做 - 单一更新 - 使用 curl
是 mapping-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']"
}
}
}
我运行ElasticSearch1.x(高兴)一年多了。现在是升级到 2.1.x 的时候了。节点应该关闭然后(一个接一个)再次打开。看起来很简单。
但是后来我运行陷入了困境。主要问题是我自己创建的字段 _uid
,这样我就可以从另一个 运行dom 中知道文档的确切位置(通过散列值)。这样我就知道只会返回确切的那个。在升级期间我得到
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
不存在(在这种情况下,我想要像现在一样的 'false-like' 结果)
注意:从 1.x 到 2.x 的升级非常大(过滤器消失,名称中没有点,没有默认访问 _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
我将 post 我的解决方案。我将所有具有 _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);
做 - 单一更新 - 使用 curl
是 mapping-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']"
}
}
}