Skip to content

Nest Weight generating wrong query in some cases #3904

Closed
@aaronsarkissian

Description

@aaronsarkissian

NEST/Elasticsearch.Net version: 7.0.0

Elasticsearch version: OSS-7.2.0

Description of the problem including expected versus actual behavior:
I have a FunctionScore with ScriptScore where the lang is set to 'painless'. I need to add a Weight(2) to the Script, but it generated a wrong query where the Weight(2) is duplicated.

Here is a snippet of my code:

.FunctionScore(c => c.
    Query(qq => qq.Bool(b => b
        .Must(must)
        .MustNot(mustNot)
        .Filter(elkFilter)
        )
    )
    .Functions(f =>
        (gaussDecayValue?? f)
        .ScriptScore(ss => ss
            .Script(sc => sc
                .Source(@"double score = (doc['viewcount'].value * params.viewcount + doc['votes'].value * params.votes + doc['answers'].value * params.answers);
                if (score <= 1) {
                  return 0;
                }
                return Math.log10(score);")
                .Lang("painless")
                .Params(p => p
                    .Add("viewcount", ViewCountWeight)
                    .Add("votes", VotesWeight)
                    .Add("answers", AnswersWeight)
                )
            )
            .Weight(2)
        )
    )
    .ScoreMode(FunctionScoreMode.Sum)
    .BoostMode(boostModeVal)
)

If I write it one scope out, it just sums the weight to the score, but my desired result is multiplication.

I was able to achieve that by writing a direct query in Kibana, but it seems that Nest is generating a wrong query.

Here is the Desired Query:

"functions": [
      {
        "script_score": {
          "script": {
            "source": """
	   double score = (doc['viewcount'].value * params.viewcount + doc['votes'].value * params.votes +  doc['answers'].value * params.answers);
	   if (score <= 1) {
	     return 0;
	   }
	   return Math.log10(score);
	   """,
      "lang": "painless",
      "params": {
        "viewcount": 0.05,
        "votes": 0.4,
        "answers": 0.55
      }
      }
    },
    "weight": 2
  }
],

And here is the generated query from Nest:

"functions": [
      {
        "script_score": {
          "script": {
            "source": """
	   double score = (doc['viewcount'].value * params.viewcount + doc['votes'].value * params.votes doc['answers'].value * params.answers);
	   if (score <= 1) {
	     return 0;
	   }
	   return Math.log10(score);
	   """,
      "lang": "painless",
      "params": {
        "viewcount": 0.05,
        "votes": 0.4,
        "answers": 0.55
      }
      },
      "weight": 2
    },
    "weight": 2
  }
],

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions