C# NEST: Expression required for "field" specification in FieldValueFactor?

(Thomas Doman) #1

I'm using NEST and Elasticsearch.Net versions 1.9.2. I'm trying to create a FieldValueFactor scoring function which all seems to work properly except I can't figure out how to specify the field I want to use.

.Functions(fsd => fsd.FieldValueFactor(d => d.Field(???).Missing(0)))

I'd like to just specify a string literal here of the field I want to use as the value factor field just like I've done when I create the query DSL by hand. However, there isn't an overload for that. The only .Field() API takes:

Expression<Func<object, object>>

Even if I created an expression tree, what should it look like? All I want to do is indicate that I want, say, the value in the "kumquat" field used as the factor. Again, it's easy to do when crafting the query DSL by hand so I'm confused by this API.

(Russ Cam) #2

It looks like 1.x client is missing a method that allows a string to be passed; 1.x is no longer supported or being actively worked on however.

You can get around this by (ab)using the .Suffix() extension method

.Functions(fsd => fsd
    .FieldValueFactor(d => d
        .Field(_ => _.Suffix("kumquat"))

(Thomas Doman) #3

Thank you very much Russ! I will (ab)use the .Suffix() extension method. We have our upgrade all mapped out but due to our significant reliance on ES, I estimate the work at a couple of sprints for a couple of engineers so we haven't taken that effort forward yet. I really appreciate your assistance with this workaround, I'm getting the expected query DSL and results now.

(Russ Cam) #4

Glad it helped you out @Thomas_Doman! As a heads up for the changes in going from NEST 1.x to 5.x, I'd recommend having a read of the following:

There are quite a few changes in the API from 1.x to 2.x that not only reflect changes in the Elasticsearch APIs, but also to make the naming of methods and types in the client consistent with Elasticsearch's API e.g. .Field(...) instead of .OnField(...). The majority of the changes in 2.x to 5.x reflect changes in the Elasticsearch API.

(Thomas Doman) #5

Fantastic, thanks for the resource pointers, I really appreciate that! Yes, I've run into a few of the API inconsistencies e.g. .TermsLookup() and I'm looking forward to getting to upgrade, just not sure when it's going to happen.

(system) #6

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.