小编典典

Elasticsearch搜索查询以检索所有记录NEST

elasticsearch

文件夹中的文档很少,我想检查该文件夹中的所有文档是否都已编制索引。为此,对于该文件夹中的每个文档名称,我想循环遍历ES中索引的文档并进行比较。所以我想检索所有文档。

有喜欢同一个问题的其他几个可能的重复[检索AElasticSearch)NEST查询所有记录,并在此处输入链接的描述,但他们没有帮助我的文档已经从那个时候改变了。(有什么关于扫描当前文档中)

我尝试使用client.search<T>()。但是根据文档,将检索默认数量的10个结果。我想获得所有记录而没有提及记录的大小吗?(因为索引的大小发生了变化)

还是可以先获取索引的大小,然后将此数字作为输入发送到该大小以获取所有文档并循环遍历?


阅读 837

收藏
2020-06-22

共1个答案

小编典典

这是我解决问题的方法。希望这可以帮助。(参考https://www.elastic.co/guide/en/elasticsearch/client/net-
api/1.x/scroll.html,https://www.elastic.co/guide/en/elasticsearch/reference/
current / search-request-scroll.html#scroll-search-
context)

List<string> indexedList = new List<string>();
var scanResults = client.Search<ClassName>(s => s
                .From(0)
                .Size(2000)
                .MatchAll()
                .Fields(f=>f.Field(fi=>fi.propertyName)) //I used field to get only the value I needed rather than getting the whole document
                .SearchType(Elasticsearch.Net.SearchType.Scan)
                .Scroll("5m")
            );

        var results = client.Scroll<ClassName>("10m", scanResults.ScrollId);
        while (results.Documents.Any())
        {
            foreach(var doc in results.Fields)
            {
                indexedList.Add(doc.Value<string>("propertyName"));
            }

            results = client.Scroll<ClassName>("10m", results.ScrollId);
        }

编辑

var response = client.Search<Document>(s => s
                         .From(fromNum)
                         .Size(PageSize)
                         .Query(q => q ....
2020-06-22