gpt4 book ai didi

.net - Elasticsearch NEST - 找不到索引后的文档

转载 作者:行者123 更新时间:2023-12-02 22:45:55 25 4
gpt4 key购买 nike

我正在使用 .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

为什么?

我必须以某种方式 promise 吗?

谢谢

编辑:但是当我再次运行控制台应用程序并注释掉创建时,找到了文档。

最佳答案

在文档为 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)
);

或调用刷新API

client.Refresh("my-index");

然而,在生产环境中,通常不建议这样做,因为写入许多小段会对集群的资源和段合并操作产生更大的性能影响。但是,它对于临时和测试目的很有用。

关于.net - Elasticsearch NEST - 找不到索引后的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52210005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com