o..*_*..o 5 .net elasticsearch nest
我正在使用 .NET NEST 在 Elasticsearch 中进行搜索。
当我索引文档并立即搜索它时,找不到它:
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node);
settings.DefaultIndex("products_test");
settings.DisableDirectStreaming(true);
ElasticClient client = new ElasticClient(settings);
Product p = new Product("My Product", "6");
client.IndexDocument(p);
var results = client.Search<Product>(s => s.Query(q => q.MatchAll()));
results.HitsMetadata.Total //is 0 and results.Hits are empty
Run Code Online (Sandbox Code Playgroud)
为什么?
我必须以某种方式做出承诺吗?
谢谢
编辑:但是当我再次运行控制台应用程序并注释掉创建时,找到了该文档。
在将文档写入索引的分片段之前,索引文档不可搜索。索引设置决定了这种情况发生的频率,默认值为 1 秒。请注意,索引文档在索引后立即可用,可通过 ID 检索。refresh_interval
对文档建立索引时,您可以指定索引后进行刷新,以便在返回响应后可以搜索该文档
var client = new ElasticClient();
client.Index(new MyDocument(1) { Message = "foo" }, i => i
.Refresh(Refresh.WaitFor)
);
Run Code Online (Sandbox Code Playgroud)
或者调用刷新API
client.Refresh("my-index");
Run Code Online (Sandbox Code Playgroud)
但在生产环境中,一般不建议这样做,因为写入许多小段会对集群的资源和段合并操作产生较大的性能影响。然而,它对于临时和测试目的可能很有用。