使用NEST ElasticClient仅检索内部_id

时间:2018-07-13 14:24:36

标签: c# elasticsearch nest elasticsearch-net

我尝试使用NEST ElasticClient执行搜索,并仅获取匹配的_id。

这是我的代码:

#document

但是文档(ElasticResult-Objects)的_id始终为null。我在做什么错了?

1 个答案:

答案 0 :(得分:2)

b = 'sqr(25)+5*3' # equation import math def f(s): s1=s.replace('sqr','') s2=s1.replace(s1.split(')')[0][1:],str(int(math.sqrt(int(s1.split(')')[0][1:]))))) return s2.replace(')','').replace('(','') print(f(b)) 不是_id文档的一部分,而是hits数组中每个匹配的匹配数据的一部分。

仅返回_source字段的最紧凑的方法是使用using response filtering,它在NEST中显示为_id

FilterPath

Elasticsearch对搜索请求的JSON响应

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);
    }
}