如何在Elasticsearch.Net(NEST)中的字段中使用POCO? [英] How to use POCOs with Fields in Elasticsearch.Net (NEST)?

查看:182
本文介绍了如何在Elasticsearch.Net(NEST)中的字段中使用POCO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Fields()进行搜索时,如何返回强类型对象列表?例如:

How to I get a strongly-typed list of objects back when doing a search that uses Fields()? For example:

var searchResult = client.Search<Person>(s => s
    .Fields("title", "name")
    .Query(q => q.Match(...etc...)
    .Highlight(...etc...)
);

使用.Fields()时,通用类型参数似乎无用,因为返回的Hits具有空的.Source属性.

It seems like the generic type parameter is useless when .Fields() is used because the Hits that are returned have a null .Source property.

(我希望有一种方法,而不必手动将搜索结果映射回我的原始Person POCO.)

(I'm hoping there's a way to do it without having to manually map the search results back to my original Person POCO.)

推荐答案

在查询中使用fields参数时,elasticsearch会在响应的fields部分返回指定的字段.

When you use fields parameter in your query, elasticsearch returns specified fields in fields section of response.

{
"took" : 36,
"timed_out" : false,
"_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
},
"hits" : {
    "total" : 18,
    "max_score" : 1.0,
    "hits" : [{
            "_index" : "nest_test_data-2672",
            "_type" : "elasticsearchprojects",
            "_id" : "1",
            "_score" : 1.0,
            "fields" : {
                "pingIP" : ["127.0.0.1"],
                "country" : ["Faroese"],
                "intValues" : [1464623485],
                "locScriptField" : [0],
                "stupidIntIWantAsLong" : [0],
                "floatValues" : [84.96025, 95.19422],
                "floatValue" : [31.93136],
                "myAttachment" : [""],
                "doubleValue" : [31.931359384176954],
                "suggest" : [""],
                "version" : [""],
                "content" : ["Bacon ipsum dolor sit amet tail non prosciutto shankle turducken, officia bresaola aute filet mignon pork belly do ex tenderloin. Ut laboris quis spare ribs est prosciutto, non short ribs voluptate fugiat. Adipisicing ex ad jowl short ribs corned beef. Commodo cillum aute, sint dolore ribeye ham hock bresaola id jowl ut. Velit mollit tenderloin non, biltong officia et venison irure chuck filet mignon. Meatloaf veniam sausage prosciutto qui cow. Spare ribs non bresaola, in venison sint short loin deserunt magna laborum pork loin cillum."],
                "longValue" : [-7046341211867792384],
                "myBinaryField" : [""],
                "name" : ["pyelasticsearch"],
                "boolValue" : [false],
                "id" : [1],
                "startedOn" : ["1994-02-28T12:24:26.9977119+01:00"]
            }
        }
    ]
}
}

您可以从searchResult.FieldSelectionssearchResult.Hits[...].Fields中检索它们.

You can retrieve them from searchResult.FieldSelections or searchResult.Hits[...].Fields.

以您为例源过滤应该方便得多.

In your case Source filtering should be much more convenient.

        [Test]
    public void MatchAllShortcut()
    {
        var results = this.Client.Search<ElasticsearchProject>(s => s
            .From(0)
            .Size(10)
            .Source(source=>source.Include(f => f.Id, f => f.Country))
            .SortAscending(f => f.LOC)
            .SortDescending(f => f.Country)
            .MatchAll()
        );

        Assert.NotNull(results);
        Assert.True(results.IsValid);

        Assert.NotNull(results.Hits);
        Assert.GreaterOrEqual(results.Hits.Count(), 10);
        Assert.True(results.Hits.All(h => !string.IsNullOrEmpty(h.Source.Country)));

        Assert.NotNull(results.Documents);
        Assert.GreaterOrEqual(results.Documents.Count(), 10);
        Assert.True(results.Documents.All(d => !string.IsNullOrEmpty(d.Country)));
    }

这篇关于如何在Elasticsearch.Net(NEST)中的字段中使用POCO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆