Search code examples
c#elasticsearchnest

Elastic search Nest client not applying boost


I'm investigating a bug with search results not matching what's expected and have discovered it's because boost is not being applied.

The query is generated using NEST (6.6.0) using the following code:

queryContainer = new MultiMatchQuery
{
    Fuzziness = Fuzziness.Auto,
    Query = querystring,
    Type = TextQueryType.BestFields,
    Fields = Infer.Fields<RecipeSearchModel>(
        f1 => Infer.Field<RecipeSearchModel>(f => f.Title, 5),
        f2 => f2.Description,
        f3 => Infer.Field<RecipeSearchModel>(f => f.Ingredients, 3),
        f4 => f4.Method,
        f5 => Infer.Field<RecipeSearchModel>(f => f.Image.Alt, 4))
};

But the query generated is without any boost applied.:

      "multi_match": {
        "fields": [
          "title",
          "description",
          "ingredients",
          "method",
          "image.alt"
        ],
        "fuzziness": "AUTO",
        "query": "chocolate",
        "type": "best_fields"
      }

This appears to be correct from what I can tell from the documentation, why isn't this working?


Solution

  • Indeed looks like boost is being ignored somewhere, here is the link to github issue. For now, you can try another syntax:

    queryContainer = new MultiMatchQuery
    {
        Fuzziness = Fuzziness.Auto,
        Query = "query",
        Type = TextQueryType.BestFields,
        Fields = Infer.Fields<RecipeSearchModel>()
            .And(Infer.Field<RecipeSearchModel>(f => f.Title, 5))
            .And<RecipeSearchModel>(f => f.Description)
            .And(Infer.Field<RecipeSearchModel>(f => f.Ingredients, 3))
            .And<RecipeSearchModel>(f => f.Method)
            .And(Infer.Field<RecipeSearchModel>(f => f.Image.Alt, 4))
    };
    

    which generates following query to elasticsearch

    {
      "query": {
        "multi_match": {
          "fields": [
            "title^5",
            "description",
            "ingredients^3",
            "method",
            "image.alt^4"
          ],
          "fuzziness": "AUTO",
          "query": "query",
          "type": "best_fields"
        }
      }
    }
    

    Tested with NEST 6.6.0.

    Hope that helps.