滚动查找所有记录elasticsearch c#nest

wr98u20j  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(328)

query以滚动查询中的匹配记录
这是c中的nest查询#要从nest c中获取所有记录#找到许多问题,这些问题可以通过使用不同的方法linq方法来解决,但是我想这样做,如果有任何建议和帮助,将不胜感激

string[] MERCHANTNO = MerchantId.Split(",");
var mustClause = new List<QueryContainer>();
var filterClause = new List<QueryContainer>();
var filters = new List<QueryContainer>();
filters.Add(new TermsQuery{
    Field = new Field("MERCHANTNO"),
    Terms = MERCHANTNO,
});

Logger.LogInformation(clsName, funcName, "Filter Clause is:", filters);

var SearchRequest = new SearchRequest<AcquirerDTO>(idxName) {
    Size       = 10000,
    SearchType = Elasticsearch.Net.SearchType.QueryThenFetch,
    Scroll     = "5m",
    Query = new BoolQuery { Must = filters }
};

var searchResponse = await _elasticClient.SearchAsync<AcquirerDTO>( SearchRequest );
3htmauhk

3htmauhk1#

在elasticsearch中滚动所有记录的代码是
滤波器

filters.Add(new TermsQuery {
                        Field = new Field("MERCHANTNO"),       >>> Value needs to be searched
                        Terms = MERCHANTNO,
                    });

日期范围筛选器

filterClause.Add(new DateRangeQuery {
                    Boost = 1.1,
                    Field = new Field("filedate"),
                    GreaterThanOrEqualTo = DateMath.Anchored(yesterday),
                    LessThanOrEqualTo = DateMath.Anchored(Today),
                    Format = "yyyy-MM-dd",
                    TimeZone = "+01:00"
                });

滚动搜索请求

var SearchRequest = new SearchRequest<AcquirerDTO>(idxName) {
                    From = 0,
                    Scroll = scrollTimeoutMinutes,
                    Size = scrollPageSize,
                    Query = new BoolQuery
                    {
                        Must = filters,
                        Filter = filterClause
                    }
                };
                var searchResponse = await _elasticClient.SearchAsync<AcquirerDTO>(SearchRequest);

                if (searchResponse.ApiCall.ResponseBodyInBytes != null) {
                    var requestJson = System.Text.Encoding.UTF8.GetString(searchResponse.ApiCall.RequestBodyInBytes);
                    var JsonFormatQuery = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(requestJson), Formatting.Indented);

                }

这是在kibana中滚动所有结果的代码

List<AcquirerDTO> results = new List<AcquirerDTO>();
                if (searchResponse.Documents.Any())
                    results.AddRange(searchResponse.Documents);

                string scrollid = searchResponse.ScrollId;
                bool isScrollSetHasData = true;

                while (isScrollSetHasData)
                {
                    ISearchResponse<AcquirerDTO> loopingResponse = _elasticClient.Scroll<AcquirerDTO>(scrollTimeoutMinutes, scrollid);
                    if (loopingResponse.IsValid)
                    {
                        results.AddRange(loopingResponse.Documents);
                        scrollid = loopingResponse.ScrollId;
                    }
                    isScrollSetHasData = loopingResponse.Documents.Any();
                }
                var records = results;

相关问题