NEST搜索整个文档C#Elasticsearch [英] NEST Search whole document C# Elasticsearch

查看:100
本文介绍了NEST搜索整个文档C#Elasticsearch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Nest在Elasticsearch中查询一百万个文档.我的代码:

I want to make a query over a million documents in Elasticsearch using Nest. My code:

var response = client.Search<MyObject>(s => s
          .Index("test")
          .Type("one")
          .Query(q => q.
                Term(
                    t => t.name, "A"
                )
          )
          .Size(10000)
          .Scroll("10m")
          .Pretty()
        );

我的MyObject类:

My MyObject class:

public class MyObject
    {
        public int id { get; set; }
        public int age { get; set; }
        public string lastname { get; set; }
        public string name { get; set; }
    }

问题是,在前10k文档中未找到此查询时,它将不会继续搜索其余结果滚动API.

The problem is when this query is not found in the first 10k documents, it won't continue searching the rest of the results scroll API.

我的问题是如何实现这一目标(即,尽管没有点击,也可以在Scroll API中浏览整个页面.)

My question is how to achieve this (i.e moving through the whole pages in Scroll API despite there is no hits..)?

推荐答案

该查询将搜索所有文档,但只会返回前.Size个文档.

The query will search all documents, but will only return you the top .Size number of documents.

您可以使用.From().Size()对结果进行分页,但是,对超过一百万个文档进行分页时,可能会出现深度分页的问题.为此,最好使用滚动API 来有效地检索一百万个文档. NEST有一个可观察到的帮助器ScrollAll()来帮助解决此问题

You can paginate results using .From() and .Size(), however, deep pagination is likely a concern when paginating over a million documents. For this, you would be better to use the scroll API to efficiently retrieve 1 million documents. NEST has an observable helper ScrollAll() to help with this

var client = new ElasticClient();

// number of slices in slice scroll
var numberOfSlices = 4;

var scrollObserver = client.ScrollAll<MyObject>("1m", numberOfSlices, s => s
    .MaxDegreeOfParallelism(numberOfSlices)
    .Search(search => search
        .Index("test")
        .Type("one")
        .Term(t => t.name, "A")
    )
).Wait(TimeSpan.FromMinutes(60), r =>
{
    // do something with documents from a given response.
    var documents = r.SearchResponse.Documents;
});

这篇关于NEST搜索整个文档C#Elasticsearch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆