nest - 如何在 Elasticsearch.Net (NEST) 中将 POCO 与字段一起使用?

标签 nest

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

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

使用 .Fields() 时,泛型类型参数似乎毫无用处,因为返回的 Hits.Source 为空> 属性(property)。

(我希望有一种方法可以做到这一点,而无需手动将搜索结果映射回我原来的 Person POCO。)

最佳答案

当您在查询中使用 fields 参数时,elasticsearch 将返回响应的 fields 部分中的指定字段。

{
"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 检索它们。

就您而言Source filtering应该会方便很多。

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

关于nest - 如何在 Elasticsearch.Net (NEST) 中将 POCO 与字段一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28773772/

相关文章:

c# - 使用 NEST for ElasticSearch 获取空结果

c# - 在某些字段上查询时,使用 NEST 搜索不会返回结果

elasticsearch - Elasticsearch Nest和CopyTo

ElasticSearch NEST 执行原始查询 DSL

elasticsearch - 使用 NEST 客户端通过 Term 查询进行搜索以进行 Elasticsearch

c# - 将时间戳记添加到ElasticSearch-Nest 2.0中添加的每个文档中

c# - 如何使用NEST获得包含给定子词的搜索结果?

elasticsearch - 使用 NEST 和属性映射在 ElasticSearch 中设置路由

elasticsearch - Elasticsearch Nest Top Hits 聚合

elasticsearch - NEST v1.0.2与Elasticsearch 1.4的兼容性