【问题标题】:Count of documents 0 after inserting data with Nest使用 Nest 插入数据后的文档计数为 0
【发布时间】:2022-01-19 16:07:19
【问题描述】:

我正在使用具有以下连接设置的 Nest:

    var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var settings = new ConnectionSettings(connectionPool, new InMemoryConnection());
    settings.DisableDirectStreaming(true); // needed to see good looking debug log on insert
    settings.DefaultIndex(Index);
    Client = new ElasticClient(settings);

使用new InMemoryConnection() 我希望使用 Nest 进行查询 - 在 Azure 云函数中更改数据。

奇怪的是,调试日志看起来很有希望索引:

    /*
    var res = await Client.IndexManyAsync(response.Elements, Index); //
    Console.WriteLine(res.DebugInformation);
    */
    /*
    var res = await Client.IndexAsync(response, i => i.Index(Index)); // Index = "data"
    Console.WriteLine(res.DebugInformation); // <--
    */

并且在插入后直接记录计数为 0:

// var anyDocs = await Client.CountAsync<OverpassElement>(c => c.Index(Index));
   var anyDocs = await Client.CountAsync<OverpassElement>(c => c);
   Console.WriteLine("count: " + anyDocs.Count);

..但插入记录的整个 json 数据。

插入后怎么算不出来(以便下一步搜索)?


其实我明白了:

通过对 POST 的成功 (200) 低级别调用构建的无效 NEST 响应:/data/_doc

并且在IndexResponse插入中有0项。

数据属于Element,类似于包含 4221 个此类项目的数组的以下部分:

{
    "type": "relation",
    "id": 8353694,
    "timestamp": "2018-06-04T22:54:27Z",
    "version": 1,
    "changeset": 59551528,
    "user": "asdf2",
    "uid": 1416503,
    "members": [
      {
        "type": "way",
        "ref": 89956942,
        "role": "from"
      },
      {
        "type": "node",
        "ref": 1042756547,
        "role": "via"
      },
      {
        "type": "way",
        "ref": 89956938,
        "role": "to"
      }
    ],
    "tags": {
      "restriction": "no_left_turn",
      "type": "restriction"
    }
  },
  

【问题讨论】:

    标签: nest


    【解决方案1】:

    ElasticSearch 与 NoSql 数据存储有许多相似之处。在这种情况下,默认情况下不保证“写后读”。当 index API 调用返回成功时,并不意味着“该文档现在可以搜索”;这意味着“ElasticSearch 已接受您的文档,并且它将很快可供搜索”。 ElasticSearch 默认使用最终一致性。

    但是,这在测试期间可能会很烦人。所以 ElasticSearch 有一个 Refresh API 基本上只是阻塞,直到所有已编入索引的文档都可用于搜索。我强烈建议您不要在生产中调用它;仅在测试代码中。

    【讨论】:

    • 我本质上是在寻找插入、搜索、清除数据 - 流
    【解决方案2】:

    作为老问题重提的风险,来自 Russ Cam 的 answer 解释说 InMemoryConnection 实际上并没有针对 Elasticsearch 运行操作。

    InMemoryConnection 实际上并不发送任何请求或接收来自 Elasticsearch 的任何响应; 与连接设置中的 .SetConnectionStatusHandler() 结合使用(或 NEST 2.x+ 中的 .OnRequestCompleted()) ,这是查看请求序列化形式的便捷方式。

    因此,您可以检查 NEST 从您的代码生成的查询,但您将无法观察结果。

    【讨论】:

      【解决方案3】:

      我不知道 Nest 是什么,但我敢打赌 100 美元,如果它使用 Transactional 概念,也许你应该提交它以便正确查看计数?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        • 2014-07-22
        • 1970-01-01
        • 2020-11-29
        • 2018-03-05
        相关资源
        最近更新 更多