4
votes

I am trying to set-up Elasticsearch environment with .NET connection availability. I was able to do basic queries with customized fuzzy distances, but what I am not able to do is field boosting at the query time. I already tried several tutorials/questions, like Elasticsearch Nest Boost query or Elastic Search using NEST Field Boosting , but NEST doesn't recognize ".OnFieldsWithBoost" or ".OnFields".

I was able to do some field boosting via HTTP API of Elastic search:

POST /products/typeproduct/_search
{
  "query" : {
   "bool": {
      "should": [
        {
          "match": {
            "Title": {
              "query": "sometest",
              "boost": 10.0 
            }
          }
        },
        {
          "match": { 
            "Name": "sometest"
          }
        }
      ]
    }
  }
}

but again I was not able to match this query via NEST syntax, as it doesn't allow multiple "match"es in "should".

My final goal is to be able to boost certain fields (Title) over others (Name) and add some fuzziness into the match. Any help would be greatly appreciated.

My Elastic search version: 2.2.0 My NEST version: 2.0.2

1

1 Answers

6
votes

This is the fluent query:

var response = client.Search<Document>(search => search
    .Query(q => q.Bool(b => b
        .Should(
            s => s.Match(m => m.Query("sometest").Field(f => f.Title).Boost(1.1)),
            s => s.Match(m => m.Query("sometest").Field(f => f.Name).Fuzziness(Fuzziness.EditDistance(1)))
        ))));

which generates following query to elasticsearch:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": {
              "boost": 1.1,
              "query": "sometest"
            }
          }
        },
        {
          "match": {
            "name": {
              "query": "sometest",
              "fuzziness": 1
            }
          }
        }
      ]
    }
  }
}

I hope this is what you were looking for :)