使用 NEST ElasticClient 仅检索内部 _id

Retrieve only internal _id with NEST ElasticClient

我尝试使用 NEST ElasticClient 执行搜索并仅获取命中的 _id。

这是我的代码:

var client = new ElasticClient();
var searchResponse = client.Search<ElasticResult>(new SearchRequest {
         From = this.query.Page * 100,
         Size = 100,
         Source = new SourceFilter {
              Includes = "_id"
         },
         Query = new QueryStringQuery {
              Query = this.query.Querystring
         }
});

public class ElasticResult {
    public string _id;
}

但是Documents (ElasticResult-Objects) 的_id 始终为空。我做错了什么?

_id 不是 _source 文档的一部分,而是命中数组中每个命中的命中元数据的一部分。

只有 _id 字段 return 的最紧凑方式是使用 using response filtering,它在 NEST

中公开为 FilterPath
private static void Main()
{
    var defaultIndex = "documents";
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

    var settings = new ConnectionSettings(pool)
        .DefaultIndex(defaultIndex)
        .DefaultTypeName("_doc");

    var client = new ElasticClient(settings);

    if (client.IndexExists(defaultIndex).Exists)
        client.DeleteIndex(defaultIndex);

    client.Bulk(b => b
        .IndexMany<object>(new[] {
            new { Message = "hello" },
            new { Message = "world" }
        })
        .Refresh(Refresh.WaitFor)
    );

    var searchResponse = client.Search<object>(new SearchRequest<object>
    {
        From = 0 * 100,
        Size = 100,
        FilterPath = new [] { "hits.hits._id" },
        Query = new QueryStringQuery
        {
            Query = ""
        }
    });

    foreach(var id in searchResponse.Hits.Select(h => h.Id))
    {
        // do something with the ids
        Console.WriteLine(id);
    }
}

Elasticsearch 对搜索请求的JSON 响应类似于

{
  "hits" : {
    "hits" : [
      {
        "_id" : "6gs8lmQB_8sm1yFaJDlq"
      },
      {
        "_id" : "6Qs8lmQB_8sm1yFaJDlq"
      }
    ]
  }
}