Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NEST 5.x Fields Usage

I have two snippets of code from nest 2.3 that I haven't been able to get to cooperate in the latest 5.0.0-rc3.

var titleField = Infer.Field<Page>(p => p.Title, 2);
        var metaDescriptionField = Infer.Field<Page>(p => p.MetaDescription, 1.5);
        var metaKeywordsField = Infer.Field<Page>(p => p.Keywords, 2);
        var bodyField = Infer.Field<Page>(p => p.Body);

        MultiMatchQuery multiMatchQuery = new MultiMatchQuery()
        {
            Fields = new [] {
                bodyField, 
                metaKeywordsField,
                metaKeywordsField,
                titleField
            },
            Query = search.Term
        };

The build error here is "Cannot implicitly convert Nest.Field[] to Nest.Fields". I can do something like

MultiMatchQuery multiMatchQuery = new MultiMatchQuery()
        {
            Fields = Infer.Fields<Page>(p => p.Title, p => p.MetaDescription, p => p.Keywords, p => p.Body),
            Query = search.Term
        };

But then I lose the field weighting.

Second field useagle I've been having troubles with is

var searchResponse = client.Search<Page>(s => s
            .MatchAll()
            .From(from)
            .Size(size)
            .Fields(f => f.Field(fi => fi.Id).Field(fi => fi.SourceId))
            );

Build error here is 'Nest.SearchDescriptor' does not contain a definition for 'Fields' and no extension method 'Fields' accepting a first argument of type 'Nest.SearchDescriptor' could be found (are you missing a using directive or an assembly reference?

I haven't had any luck with getting something compile-able in the case.

like image 770
Andrew Walters Avatar asked Oct 17 '22 21:10

Andrew Walters


1 Answers

The implicit operator that converts Field[] to Fields is missing in 5.x in the latest release; I will add this in addition to other helpful overloads, to go into the next release. In the meantime, you can construct a Fields from fields with strong typing and boosting using

Fields fields = ((Fields)Infer.Field<Document>(f => f.Property1, 1.2))
                    .And<Document>(f => f.Property2, 2)
                    .And<Document>(f => f.Property3, 5);

You can also use strings too

Fields fields = new[]
{
    "property1^1.2",
    "property2^2",
    "property3^5"
};

For the second part, .Fields() on SearchRequest is now .StoredFields(), in line with the change in Elasticsearch, to indicate that it is to be used to retrieve stored fields only (those set to store:true in the mapping). As noted in the issue, if you were using .Fields() to retrieve a partial document from the _source field, it is recommended that you use source filtering for this.

like image 147
Russ Cam Avatar answered Oct 21 '22 03:10

Russ Cam