gpt4 book ai didi

exception - Elasticsearch 查询 "empty index"

转载 作者:行者123 更新时间:2023-12-02 22:43:54 27 4
gpt4 key购买 nike

在我的应用程序中,我使用了多个 elasticsearch 索引,它们在初始状态下不包含索引文档。我认为可以称为“空”:)文档的映射正确且有效。

该应用程序还有一个包含实体的关系数据库,这些实体可能具有在 elasticsearch 中关联的文档。

在应用程序的初始状态下,只有实体没有文档是很常见的,因此没有一个文档被索引,因此是“空索引”。尽管如此,索引已经创建,文档的映射也已放入索引并出现在索引元数据中。

无论如何,当我使用 SearchQuery 查询 elasticsearch 以查找其中一个实体的文档(文档包含来自实体的唯一 ID)时,elasticsearch 将抛出 ElasticSearchException,提示字段 xy 等没有映射。

但是如果我先将一个空白文档插入索引,查询就不会失败。

有没有一种方法可以“初始化”索引以防止查询失败并摆脱愚蠢的“虚拟文档解决方法”?

更新:另外,虚拟文档的变通方法会污染索引,例如计数查询现在总是返回 +1....因此我也在变通方法中添加了删除...

最佳答案

您的问题缺乏细节且不清楚。如果您提供了索引架构和查询的要点,那将会有所帮助。您还应该提供您正在使用的 elasticsearch 版本。

您提到的“无映射”异常与使用某些数据初始化索引无关。您很可能正在对不存在的字段进行排序。如果您一次查询多个索引,这很常见。

解决方案:解决方案是根据elasticsearch的版本。如果您使用的是 1.3.x 或更低版本,那么您应该使用 ignore_unmapped。如果您使用的版本大于 1.3.5,那么您应该使用 unmapped_typeClick here to read official documentation.

如果您觉得文档令人困惑,那么这个例子会让您一目了然:

让我们创建两个索引 testindex1testindex2

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"}}}}}}}}}'

这两个索引之间的唯一区别是 - 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"}]}]}'

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"}]}]}'

查询示例:

  1. 对 elasticsearch 版本 > 1.3.x 使用“unmapped_type

        curl -XPOST 'localhost:9200/testindex2/_search?pretty' -d '{"fields":["firstname"],"query":{"match_all":{}},"sort":[{"servers.location.name":{"order":"desc","unmapped_type":"string"}}]}'
  2. 对 elasticsearch 版本 <= 1.3.5 使用“ignore_unmapped

    curl -XPOST 'localhost:9200/testindex2/_search?pretty' -d '{"fields":["firstname"],"query":{"match_all":{}},"sort":[{"servers.location.name":{"order":"desc","ignore_unmapped":"true"}}]}'
query1 的

输出:

{
"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 ]
} ]
}
}
query2 的

输出:

{
"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 ]
} ]
}
}

注意:

  1. 这些示例是在 elasticserch 1.4 上创建的。
  2. 这些示例还演示了如何对嵌套字段进行排序。

关于exception - Elasticsearch 查询 "empty index",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23103603/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com