Elasticsearch 关于“空索引”的查询

2024-05-13

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

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

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

无论如何,当我使用 SearchQuery 查询 elasticsearch 以查找其中一个实体的文档(该文档包含该实体的唯一 id)时,elasticsearch 将抛出 ElasticSearchException,抱怨字段 xy 等不存在映射。

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

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

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


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

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

解决方案:解决方案是基于elasticsearch的版本。如果你在1.3.x 或更低版本那么你应该使用ignore_unmapped。如果您使用的是版本大于1.3.5那么你应该使用unmapped_type. 点击此处阅读官方文档。 https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_ignoring_unmapped_fields

如果您发现文档令人困惑,那么这个例子会让您清楚:

让我们创建两个索引测试索引1 and 测试索引2

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

这两个指数之间的唯一区别是 -测试索引1 has "server“场和文本索引2 has "computers“ 场地。

现在让我们在两个索引中插入测试数据。

指数测试数据测试索引1:

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

指数测试数据测试索引2:

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. Using "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"}}]}'
    
  2. Using "ignore_unmapped“对于elasticsearch版本

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

Output查询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 ]
    } ]
  }
}

Output查询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 ]
    } ]
  }
}

Note:

  1. 这些示例是在 elasticsearch 1.4 中创建的。
  2. 这些示例还演示了如何对嵌套字段进行排序。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Elasticsearch 关于“空索引”的查询 的相关文章

随机推荐

  • 如何向离子推送通知添加操作按钮?

    我想向离子推送通知添加一些操作按钮 我正在使用科尔多瓦pushv5 通知工作正常 但我不知道如何添加这些按钮 如何添加这些按钮 应在 POST 请求中添加操作按钮 registration ids my device id data tit
  • 无法在 WSL2 上运行 OpenGL

    我尝试在 WSL2 上运行 OpenGL 代码 但在尝试运行可执行文件时出现以下错误 GLFW error 65543 GLX Failed to create context GLXBadFBConfig Unable to create
  • 当 config.cache_classes = true 时缺少方法

    我有两个名为 Scope 和 ScopeItem 的 ActiveRecord 模型 具有 has many 关系 class Scope lt ActiveRecord Base has many scope items end clas
  • 如何从命令行向 REPL 添加导入?

    如何使 REPL 导入命令行中给出的包 Sample scala someMagicHere import sys error scala gt imports 1 import scala Predef 162 terms 78 are
  • 使用异步/等待返回值意外获取 API [重复]

    这个问题在这里已经有答案了 这是函数 const getUserIP async gt let response await fetch https jsonip com let json await response json conso
  • 如何在可能为空值的字段上创建唯一索引(Oracle 11g)?

    这是包含 3 列的示例表 ID UNIQUE VALUE UNIQUE GROUP ID 我希望可以允许以下记录 1 NULL NULL 2 NULL NULL or 3 NULL 7 4 123 7 or 注意 此条件不允许unique
  • Sybase 中神秘的“时间戳”数据类型是什么?

    我最近在工作中发现 Sybase 数据库中的一个表使用 时间戳 类型的列 如果我使用这种神秘的时间戳数据类型创建一个表 如下所示 create table dropme foo timestamp roo int null insert i
  • HTML:将表单字段复制到另一个表单(包括文件输入字段)?

    我发现出于安全原因 无法使用 javascript 设置表单文件输入字段值 我只想将文件输入复制到另一个表单并将其发布 我搜索了解决方法但找不到任何内容 这可能吗 更新 我的代码 function prepareUpload fileval
  • H2控制台看不到JAVA创建的表

    我已经从以下位置下载了 H2 控制台http www h2database com html download html http www h2database com html download html我已经在我的中配置了 URLjdb
  • 我的 switch 语句中的 if 语句在我的计算器中无法正常工作。需要帮助修复除以 0 错误

    我创建了一个基本计算器 但是用除法对其进行了编码 当我除以零时 它会在第二个文本框中向用户提供错误 但现在即使我除以 3 或任何其他不为 0 的数字 错误不断出现在我的第二个文本框中 namespace calc
  • 如何将 ifconfig 命令的输出保存到缓冲区中?

    我必须使用 C 和 VxWorks 将命令 ifconfig 的输出保存到字符缓冲区中 我该怎么做 ifconfig 是一个 shell 命令 因此您应该能够使用 gt 将其输出重定向到文件 然后读取该文件 您还可以查看手册中的 重定向 S
  • 创建动态子域

    自从我考虑一些网站正在实施的此功能以来已经有一段时间了 它看起来非常成功 类似的网站tumblr com blogger com wordpress com允许用户使用简单的 HTML PHP 表单从网站内注册新的子域名 以我目前对 PHP
  • 如何备份整个MySQL数据库的所有用户、权限和密码?

    我需要备份整个 MySQL 数据库 其中包含所有用户及其权限和密码的信息 我看到选项http www igvita com 2007 10 10 hands on mysql backup migration http www igvita
  • iPhone:隐藏搜索栏中的空格键

    我又要显得傻了 但这总比发疯好 这是我的问题 我有一个 UISearchBar 其中有一个我想隐藏的scopeBar 我这样做 searchBar showsScopeBar NO 我也打电话给 searchBar becomeFirstR
  • 如何使用 zend 导入 CSV

    如何使用 zend 框架导入 CSV 文件 我应该使用 zend file transfer 还是有任何我必须研究的特殊类 另外 如果我使用 zend file transfer 是否有任何特殊的 CSV 验证器 你不必使用任何 zend
  • 在react-navigation中动态更改DrawerNavigator的drawerPosition配置

    我的主要路线有一个 DrawerNavigator 配置 const App DrawerNavigator DrawerRoutes initialRouteName Main contentComponent navigation gt
  • 微服务、amqp 和服务注册/发现

    我正在研究微服务架构 实际上我想知道一些事情 我非常同意使用 返回 服务发现来在基于 REST 的微服务上发出请求 我需要知道发出请求的服务 或至少是服务器集群的前端 在哪里 因此在这种情况下能够发现 ip port 是有意义的 但我想知道
  • DataGridView SortCompare 事件不会触发

    使用 VS2008 C 和 NET 3 5 我正在使用数据绑定 DataGridView 控件来显示从 Web 服务读取的表格数据 在某些情况下 有一个数字列需要排序 我尝试了几种不同的方法来使其工作 但该列最终仍然按字母顺序排序 即 1
  • 在 Protractor / Webdriver 中等待页面重定向

    我有一个测试 单击按钮并重定向到用户仪表板 当发生这种情况时 Webdriver 返回 javascript error document unloaded while waiting for result 为了解决这个问题 我插入brow
  • Elasticsearch 关于“空索引”的查询

    在我的应用程序中 我使用了几个elasticsearch索引 它们在初始状态下不包含索引文档 我认为这可以称为 空 该文档的映射是正确且有效的 该应用程序还有一个包含实体的关系数据库 这些实体可能具有在 elasticsearch 中关联的