我正在使用.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
为什么?
我必须以某种方式提交吗?
谢谢
编辑:但是当我再次运行控制台应用程序并注释掉创建时,找到了文档。
在文档为written to a shard segment of an index之前,索引文档无法搜索。 refresh_interval
index setting负责发生这种情况的频率,默认值为1秒。请注意,索引后的索引文档可立即使用,可通过ID检索。
索引文档时,您可以指定在索引后进行刷新,以便在返回响应后可以搜索文档
var client = new ElasticClient();
client.Index(new MyDocument(1) { Message = "foo" }, i => i
.Refresh(Refresh.WaitFor)
);
或调用Refresh API
client.Refresh("my-index");
但是,在生产环境中,通常不建议这样做,因为在资源和段合并操作方面,编写许多小段将对集群产生更大的性能影响。然而,它可用于临时和测试目的。