sko*_*ohn 1 mapping indexing search exception elasticsearch
在我的应用程序中,我使用了几个 elasticsearch 索引,它们在初始状态下不包含索引文档。我认为这可以称为“空”:) 文档的映射正确且有效。
该应用程序还有一个包含实体的关系数据库,这些实体可能具有在 elasticsearch 中关联的文档。
在应用程序的初始状态,只有实体没有文档是很常见的,因此没有一个文档被索引,因此是“空索引”。尽管如此,索引已被创建,并且文档的映射也已被放入索引并存在于索引元数据中。
无论如何,当我使用 SearchQuery 查询 elasticsearch 以查找实体之一的文档(该文档包含来自实体的唯一 id)时,elasticsearch 将抛出 ElasticSearchException,它抱怨字段 xy 等不存在映射。
但是,如果我首先将一个空白文档插入索引中,则查询不会失败。
有没有办法“初始化”索引以防止查询失败并摆脱愚蠢的“虚拟文档解决方法”?
更新:另外,使用虚拟文档的解决方法会污染索引,例如,计数查询现在总是返回 +1....所以我也向解决方法添加了删除...
小智 5
你的问题缺乏细节,不清楚。如果您提供了索引架构和查询的要点,那会有所帮助。您还应该提供您正在使用的 elasticsearch 版本。
您提到的“无映射”异常与使用某些数据初始化索引无关。很可能您正在对不存在的字段进行排序。如果您一次查询多个索引,这很常见。
解决方案:解决方案基于elasticsearch的版本。如果您使用的是1.3.x 或更低版本,那么您应该使用ignore_unmapped
. 如果您使用的版本高于 1.3.5,那么您应该使用unmapped_type
.
点击这里阅读官方文档。
如果您发现文档令人困惑,那么此示例将清楚说明:
让我们创建两个索引testindex1和testindex2
curl -XPUT localhost:9200/testindex1 -d '{"mappings":{"type1":{"properties":{"firstname":{"type":"string"},"servers":{"type":"nested","properties":{"name":{"type":"string"},"location":{"type":"nested","properties":{"name":{"type":"string"}}}}}}}}}'
curl -XPUT localhost:9200/testindex2 -d '{"mappings":{"type1":{"properties":{"firstname":{"type":"string"},"computers":{"type":"nested","properties":{"name":{"type":"string"},"location":{"type":"nested","properties":{"name":{"type":"string"}}}}}}}}}'
Run Code Online (Sandbox Code Playgroud)
这两个索引之间的唯一区别是 - testindex1具有“ server
”字段,而textindex2具有“ computers
”字段。
现在让我们在两个索引中插入测试数据。
testindex1上的索引测试数据:
curl -XPUT localhost:9200/testindex1/type1/1 -d '{"firstname":"servertom","servers":[{"name":"server1","location":[{"name":"location1"},{"name":"location2"}]},{"name":"server2","location":[{"name":"location1"}]}]}'
curl -XPUT localhost:9200/testindex1/type1/2 -d '{"firstname":"serverjerry","servers":[{"name":"server2","location":[{"name":"location5"}]}]}'
Run Code Online (Sandbox Code Playgroud)
testindex2上的索引测试数据:
curl -XPUT localhost:9200/testindex2/type1/1 -d '{"firstname":"computertom","computers":[{"name":"computer1","location":[{"name":"location1"},{"name":"location2"}]},{"name":"computer2","location":[{"name":"location1"}]}]}'
curl -XPUT localhost:9200/testindex2/type1/2 -d '{"firstname":"computerjerry","computers":[{"name":"computer2","location":[{"name":"location5"}]}]}'
Run Code Online (Sandbox Code Playgroud)
查询示例:
使用“ unmapped_type
”表示elasticsearch版本> 1.3.x
curl -XPOST 'localhost:9200/testindex2/_search?pretty' -d '{"fields":["firstname"],"query":{"match_all":{}},"sort":[{"servers.location.name":{"order":"desc","unmapped_type":"string"}}]}'
Run Code Online (Sandbox Code Playgroud)使用“ ignore_unmapped
”作为elasticsearch版本<= 1.3.5
curl -XPOST 'localhost:9200/testindex2/_search?pretty' -d '{"fields":["firstname"],"query":{"match_all":{}},"sort":[{"servers.location.name":{"order":"desc","ignore_unmapped":"true"}}]}'
Run Code Online (Sandbox Code Playgroud)查询 1 的输出:
{
"took" : 15,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : null,
"hits" : [ {
"_index" : "testindex2",
"_type" : "type1",
"_id" : "1",
"_score" : null,
"fields" : {
"firstname" : [ "computertom" ]
},
"sort" : [ null ]
}, {
"_index" : "testindex2",
"_type" : "type1",
"_id" : "2",
"_score" : null,
"fields" : {
"firstname" : [ "computerjerry" ]
},
"sort" : [ null ]
} ]
}
}
Run Code Online (Sandbox Code Playgroud)
查询 2 的输出:
{
"took" : 10,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : null,
"hits" : [ {
"_index" : "testindex2",
"_type" : "type1",
"_id" : "1",
"_score" : null,
"fields" : {
"firstname" : [ "computertom" ]
},
"sort" : [ -9223372036854775808 ]
}, {
"_index" : "testindex2",
"_type" : "type1",
"_id" : "2",
"_score" : null,
"fields" : {
"firstname" : [ "computerjerry" ]
},
"sort" : [ -9223372036854775808 ]
} ]
}
}
Run Code Online (Sandbox Code Playgroud)
笔记:
归档时间: |
|
查看次数: |
5829 次 |
最近记录: |