Imran Azad
Imran Azad

Reputation: 1048

Filter function score with Nest

I am trying to build the following query using using the DSL syntax:

    GET /_search
    {
      "query": {
        "function_score": {
          "filter": { 
            "term": { "city": "Barcelona" }
          },
          "functions": [ 
            {
              "filter": { "term": { "features": "wifi" }}, 
              "weight": 1
            },
            {
              "filter": { "term": { "features": "garden" }}, 
              "weight": 1
            },
            {
              "filter": { "term": { "features": "pool" }}, 
              "weight": 2 
            }
          ],
          "score_mode": "sum", 
        }
      }
    }

However it seems there's no filter option in the function_score when using the NEST client. I can confirm the query works in elastic.

Upvotes: 0

Views: 1439

Answers (1)

Russ Cam
Russ Cam

Reputation: 125488

I'm not sure which version of Elasticsearch you're targeting, but there hasn't been a filter property on a function_score query since Elasticsearch 2.0, when queries and filters were merged. NEST exposes what is available within the Elasticsearch API, so the property is available in NEST 1.x but not in any later versions.

Filters on individual functions exist

var response = client.Search<object>(s => s
    .Query(q => q
        .FunctionScore(fs => fs
            .Functions(fu => fu
                .Weight(w => w
                    .Weight(1)
                    .Filter(wf => wf
                        .Term("features", "pool")
                    )
                )
            )
        )
    )
);

Upvotes: 3

Related Questions