首页 > 解决方案 > 使用 Nest 插入数据后的文档计数为 0

问题描述

我正在使用具有以下连接设置的 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 Cloud 函数中更改数据。

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

    /*
    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

0并且在IndexResponse插入中有项目。

数据看起来像包含 4221 个此类项目的Element数组的以下部分:

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

解决方案


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

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


推荐阅读