【问题标题】:index new document and get the indexed document in the same query索引新文档并在同一查询中获取索引文档
【发布时间】:2020-04-10 13:22:25
【问题描述】:

可以索引一个新文档,索引成功后还给他吗?

我尝试获取返回的 _id,但我使用了 2 个查询,索引操作需要一些时间,而第二个查询找不到 _id,因此它并不总是完美。

这是索引文档的查询:

const query = await elsaticClient.index({
routing: "dasdsad34_d",
index: "milan",
body: {
  text: "san siro",
  user: {
    user_id: "3",
    username: "maldini",
  },
  tags: ["Forza Milan","grande milan"],
  publish_date: new Date(),
  likes: [],
  users_tags: [1,5],
  type: {
    name: "comment",
    parent: "dasdsad34_d",
  },
},

});

【问题讨论】:

  • 那么您在索引文档和之后通过其 id 查询该文档之间遇到了“延迟”?

标签: node.js elasticsearch elasticsearch-query


【解决方案1】:

不,默认行为是不可能的。默认情况下,Elasticsearch 仅提供近乎实时的支持。它的默认刷新间隔是 1 秒,因为索引刷新被认为是一项昂贵的操作。

为了克服这个问题,在您的索引操作中,您可以添加 refresh=true。您可以从以下链接获取更多详细信息。

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html

请注意,这不推荐选项,因为这会带来巨大的开销。仅当您插入到该索引的有问题的数字非常低时才使用它。

推荐的方法是在您的索引操作中使用 refresh=wait_for。但这有一个缺点,就是要等待一秒钟才能完成自然刷新。因此,如果您将默认刷新间隔设置为 1 并且可以接受这是一个可接受的折衷方案,那么这就是要走的路。

但是,如果您设置了更高的刷新间隔,则索引操作的等待时间将与刷新间隔一样长。所以请谨慎选择。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 2013-07-27
    • 1970-01-01
    相关资源
    最近更新 更多