【问题标题】:How can I get the total number of documents found using the Nest?如何获取使用 Nest 找到的文档总数?
【发布时间】:2014-11-20 08:37:41
【问题描述】:

我使用嵌套执行相同的请求,并且直接在 ElastichSearch 中。 当我看到直接请求时,有多少文档匹配请求。

  "hits": 
  {
    "total": 1640,
    "max_score": 1,
    "hits": [...]
   }

我的查询:

 var search = client.Search<RCompany>(s => s.Index("MyIndex")
 .Query(qq => qq
 .Filtered(m => m.Filter(f => f.Bool(b => b
 .Must(
 a => a.Term(z => z.Company.Code, param1),
 a => a.Terms(z => z.Company.Id, param2),
 a => a.Terms(z => z.Company.Field1.Id, param3)
  )))
 .Query(b => b.Bool(q => q.Should
  (n => n.Match(a => a.OnField(d => d.Company.Field2).Query(param5).Operator(Operator.And)),
   n => n.Match(a => a.OnField(d => d.Company.Field3).Query(param5).Operator(Operator.And)),
   n => n.Match(a => a.OnField(d => d.Company.Field4).Query(param5).Operator(Operator.And)),
   n => n.Match(a => a.OnField(d => d.Company.Field5).Query(param5).Operator(Operator.And))
  )))))
  .Size(10)
  .SortDescending(n => n.DtCreate));

如何使用 Nest 找出适合请求的文档数量?

【问题讨论】:

    标签: elasticsearch nest


    【解决方案1】:

    ISearchResponse 上有一个 Total 属性,它保存与查询匹配的文档总数。在您的示例中,这将是 search.Total

    【讨论】:

    • 谢谢你,格雷格!提出了几个要求,但总数始终是 10,这与我的 Size 一致。这是误导。
    • SizeTotal 是两个不同的东西。 Size 表示与请求一起返回的结果数(默认为 10),并与 From(索引中的起始偏移量)结合使用。这些对于分页很有用。另一方面,Total 是与查询匹配的实际文档数。
    【解决方案2】:

    最好的方法是使用official documentation提出的Count方法

    这里是代码

    var result = client.Count<ElasticsearchProject>();
    

    【讨论】:

      猜你喜欢
      • 2020-06-27
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 2023-01-19
      • 2019-08-21
      • 2018-09-08
      • 2023-04-07
      • 2021-06-17
      相关资源
      最近更新 更多