Elasticsearch NEST - 找不到索引后的文档
Elasticsearch NEST - document immediately after indexing is not found
我正在使用 .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. The refresh_interval
index setting负责这种情况发生的频率,默认为 1 秒。请注意,索引文档在索引后立即可用,可通过 ID 检索。
为文档编制索引时,您可以指定在编制索引后进行刷新,以便在返回响应后可以搜索文档
var client = new ElasticClient();
client.Index(new MyDocument(1) { Message = "foo" }, i => i
.Refresh(Refresh.WaitFor)
);
或调用刷新API
client.Refresh("my-index");
然而,在生产环境中,通常不建议这样做,因为写入许多小段会对集群的资源和段合并操作产生更大的性能影响。但是,它对于临时和测试目的很有用。
我正在使用 .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. The refresh_interval
index setting负责这种情况发生的频率,默认为 1 秒。请注意,索引文档在索引后立即可用,可通过 ID 检索。
为文档编制索引时,您可以指定在编制索引后进行刷新,以便在返回响应后可以搜索文档
var client = new ElasticClient();
client.Index(new MyDocument(1) { Message = "foo" }, i => i
.Refresh(Refresh.WaitFor)
);
或调用刷新API
client.Refresh("my-index");
然而,在生产环境中,通常不建议这样做,因为写入许多小段会对集群的资源和段合并操作产生更大的性能影响。但是,它对于临时和测试目的很有用。