Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not possible to deserialize/create a score function of type "weight" #820

Closed
brentam opened this issue Jan 23, 2024 · 1 comment · Fixed by #880
Closed

Not possible to deserialize/create a score function of type "weight" #820

brentam opened this issue Jan 23, 2024 · 1 comment · Fixed by #880
Labels
bug Something isn't working

Comments

@brentam
Copy link
Contributor

brentam commented Jan 23, 2024

What is the bug?

not possible to build/deserialize a score function of type "weight".
Therefore, we are not able to construct queries that contains the "weight" score function type.

This search query is a correct query:

{
  "query": {
    "function_score": {
      "query": { "match_all": {} },
      "boost": "5",
      "functions": [
        {
          "filter": { "match": { "test": "bar" } },
          "random_score": {},
          "weight": 23
        },
        {
          "filter": { "match": { "test": "cat" } },
          "weight": 42
        }
      ],
      "max_boost": 42,
      "score_mode": "max",
      "boost_mode": "multiply",
      "min_score": 42
    }
  }
}

Notice the function array, the second one is a correct function type "weight" as per the documentation.

        {
          "filter": { "match": { "test": "cat" } },
          "weight": 42
        }

Unfortunatelly, there is no way to deserialize or build that score function using the api.
The problem seems in the FunctionScore.Kind enum

    public enum Kind implements JsonEnum {
        Exp("exp"),

        Gauss("gauss"),

        Linear("linear"),

        FieldValueFactor("field_value_factor"),

        RandomScore("random_score"),

        ScriptScore("script_score"),

        ;

        private final String jsonValue;

        Kind(String jsonValue) {
            this.jsonValue = jsonValue;
        }

        public String jsonValue() {
            return this.jsonValue;
        }

    }

These are hardcoded values with the json field expected by all the types.
The issue is that she "weight" function is characterized by the abscense of any of the values above,

How can one reproduce the bug?

Look at the test below:
the willThrowException test should not throw any exception

public class FunctionScoreKindTest extends Assert {

private final JsonpMapper mapper = new JsonbJsonpMapper();

    @Test
    public void willDeserialize() {

        String str = "{\n" +
                "          \"filter\": { \"match\": { \"test\": \"bar\" } },\n" +
                "          \"random_score\": {},\n" +
                "          \"weight\": 23\n" +
                "        }";

        JsonParser parser = mapper.jsonProvider().createParser(new StringReader(str));
        FunctionScore functionScore = FunctionScore._DESERIALIZER.deserialize(parser, mapper);
        assertTrue(functionScore.isRandomScore());
    }

    @Test
    public void willThrowException() {
        String str = " {\n" +
                "          \"filter\": { \"match\": { \"test\": \"cat\" } },\n" +
                "          \"weight\": 42\n" +
                "        }";

        JsonParser parser = mapper.jsonProvider().createParser(new StringReader(str));

        MissingRequiredPropertyException exception=assertThrows(MissingRequiredPropertyException.class, () -> {
             FunctionScore._DESERIALIZER.deserialize(parser, mapper);
        });
        assertEquals("Missing required property 'Builder.<variant kind>'", exception.getMessage());
    }

}

What is the expected behavior?

We should be able to build/deserialize the "weight" type function

What is your host/environment?

MacOs Sonoma

Do you have any screenshots?

N/A

Do you have any additional context?

documentation for score-functions
https://opensearch.org/docs/latest/query-dsl/compound/function-score
https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-function-score-query.html#score-functions

@brentam brentam added bug Something isn't working untriaged labels Jan 23, 2024
@dblock
Copy link
Member

dblock commented Feb 5, 2024

Want to contribute the missing code @brentam? Also I would check what we have in https://github.com/opensearch-project/opensearch-api-specification

@dblock dblock removed the untriaged label Feb 5, 2024
russcam added a commit to russcam/opensearch-java that referenced this issue Feb 26, 2024
This commit supports function score functions with an
optional function variant. The function is optional as it
is perfectly valid to have a function with only a weight e.g.
a function score query with multiple functions and a score mode
of "first", where the last weight function acts as some default value.

Closes opensearch-project#820
russcam added a commit to russcam/opensearch-java that referenced this issue Feb 26, 2024
This commit supports function score functions with an
optional function variant. The function is optional as it
is perfectly valid to have a function with only a weight e.g.
a function score query with multiple functions and a score mode
of "first", where the last weight function acts as some default value.

Closes opensearch-project#820

Signed-off-by: Russ Cam <russcam@canva.com>
VachaShah pushed a commit that referenced this issue Feb 27, 2024
* Support optional function score variant

This commit supports function score functions with an
optional function variant. The function is optional as it
is perfectly valid to have a function with only a weight e.g.
a function score query with multiple functions and a score mode
of "first", where the last weight function acts as some default value.

Closes #820

Signed-off-by: Russ Cam <russcam@canva.com>

* Address CI build issues

Signed-off-by: Russ Cam <russcam@canva.com>

* remove changelog entry for 3.0

Signed-off-by: Russ Cam <russcam@canva.com>

---------

Signed-off-by: Russ Cam <russcam@canva.com>
opensearch-trigger-bot bot pushed a commit that referenced this issue Feb 27, 2024
* Support optional function score variant

This commit supports function score functions with an
optional function variant. The function is optional as it
is perfectly valid to have a function with only a weight e.g.
a function score query with multiple functions and a score mode
of "first", where the last weight function acts as some default value.

Closes #820

Signed-off-by: Russ Cam <russcam@canva.com>

* Address CI build issues

Signed-off-by: Russ Cam <russcam@canva.com>

* remove changelog entry for 3.0

Signed-off-by: Russ Cam <russcam@canva.com>

---------

Signed-off-by: Russ Cam <russcam@canva.com>
(cherry picked from commit 045c805)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
VachaShah pushed a commit that referenced this issue Feb 27, 2024
* Support optional function score variant

This commit supports function score functions with an
optional function variant. The function is optional as it
is perfectly valid to have a function with only a weight e.g.
a function score query with multiple functions and a score mode
of "first", where the last weight function acts as some default value.

Closes #820



* Address CI build issues



* remove changelog entry for 3.0



---------


(cherry picked from commit 045c805)

Signed-off-by: Russ Cam <russcam@canva.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants