【问题标题】:Elasticsearch NEST - document immediately after indexing is not foundElasticsearch NEST - 未找到索引后立即的文档
【发布时间】:2018-09-06 18:15:37
【问题描述】:

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

为什么?

我必须以某种方式提交吗?

谢谢

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

【问题讨论】:

    标签: .net elasticsearch nest


    【解决方案1】:

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

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

    【讨论】:

    • 哇,谢谢!是的,我的问题是在编写测​​试时出现的,所以你的解决方案非常适合我!谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多