NEST elasticsearch.NET 搜索查询未返回结果(第 2 部分)

NEST elasticsearch.NET search query not returning results (part 2)

我在 NEST 中使用 object 初始化语法来形成搜索查询。当我将第二个 pdfQuery 与逻辑 OR 运算符一起包含时,我没有得到任何结果。如果我排除它,我会得到结果。

QueryContainer titleQuery = new MatchQuery
{
    Field = Property.Path<ElasticBook>(p => p.Title),
    Query = query,
    Boost = 50,
    Slop = 2,
    MinimumShouldMatch = "55%"
};

QueryContainer pdfQuery = new MatchQuery
{
    Field = Property.Path<ElasticBook>(p => p.Pdf),
    Query = query,
    CutoffFrequency = 0.001
};

var result = _client.Search<ElasticBook>(new SearchRequest("bookswithstop", "en")
{
    From = 0,
    Size = 10,
    Query = titleQuery || pdfQuery,
    Timeout = "20000",
    Fields = new []
    {
      Property.Path<ElasticBook>(p => p.Title)  
    } 
});

如果我调试并检查结果 var,我 copy-value 获得的请求属性之一:

{
 "timeout": "20000",
 "from": 0,
 "size": 10,
 "fields": [
   "title"
 ],
 "query": {
   "bool": {
     "should": [
       {
         "match": {
           "title": {
             "query": "Proper Guide To Excel 2010",
             "slop": 2,
             "boost": 50.0,
             "minimum_should_match": "55%"
           }
         }
       },
       {
         "match": {
           "pdf": {
             "query": "Proper Guide To Excel 2010",
             "cutoff_frequency": 0.001
           }
         }
       }
     ]
   }
 }
}

问题是,如果我将该查询复制到 sense - 它 returns 大约 100 个结果(尽管速度很慢)。我检查了 header 信息,NEST 的信息似乎也是正确的:

ConnectionStatus = {StatusCode: 200, 
    Method: POST, 
    Url: http://elasticsearch-blablablamrfreeman/bookswithstop/en/_search, 
    Request: {
  "timeout": "20000",
  "from": 0,
  "size": 10,
  "fields": [
    "title"
  ],
  "query": {
    "bool": {
      "shoul...

pdf 字段使用弹性搜索附件插件(位于@ https://github.com/elastic/elasticsearch-mapper-attachments),我之前 Newtonsoft.JSON system.outofmemoryexceptions 被抛出(但由于某种原因现在没有)。

因此,我唯一的建议是,我的查询和 NEST 可能存在一些序列化问题?如果是这种情况,我不确定为什么它会使用 200 代码成功执行并在文档 属性

中提供 0 个文档

任何人都可以向我解释一下我将如何解决这个问题吗?它显然不喜欢我的第二个搜索查询 (pdfQuery),但我不确定为什么 - 结果 JSON 请求语法似乎也是正确的!

我觉得这部分有问题

Fields = new []
    {
      Property.Path<ElasticBook>(p => p.Title)  
    } 

当你使用Fields选项时,elasticsearch没有returning _source字段,所以你不能通过result.Documents访问结果。相反,你必须使用 result.FieldSelections,这是非常不愉快的。

如果您只想 return 来自 elasticsearch 的特定字段并且仍然能够使用 result.Documents,您可以利用 source includes / excludes。使用 NEST,您可以按如下方式执行此操作:

var searchResponse = client.Search<Document>(s => s
    .Source(source => source.Include(f => f.Number))
    .Query(q => q.MatchAll()));

希望对您有所帮助。