Elasticsearch boost nested field score relevance query

I have an elasticsearch index (students) with following mapping:

{
    "name": {
        "type": "text"
    },
    "age": {
        "type": "integer"
    },
    "tests": {
        "type": "nested",
        "properties": {
            "id": {
                "type": "integer"
            },
            "score": {
                "type": "float"
            }
        }
    }
}
}

tests is a nested field containing the tests that a student has passed along with that test's score. I want to query students with some matching name, some age range and more importantly with tests in a following way:

  • user can query data for tests in 2 ways
  • first is mustPassedTests, these tests along with provided score should be present in student's test with the given id and given score >=
  • second is shouldPassTests, its okay if these aren't present in a student (if any mustPass is provided )
  • if there is a mustPassTest and shouldPassTest in the query, shouldPass just does not take part in filtering but simply have impact on score
  • if only shouldPassTest test is queried (and not mustPass), any one of the given shouldPass tests must be present in student tests

for example for a student having data as {name:"jason",tests:[{id:1, score: 12}]} and another as {"name":"flock", tests:[{id:1, score: 10}, {id:2, score:8}]}, if I query for mustPass tests with mustPassTest as [{id: 1, score>=10}] , shouldPassTest as [{id: 2, score>=10}] then both students should be returned but "flock" as ranked higher since he posses both tests(must with appropriate score range and should as present in his test)

how can the query be written ?? (using rest client) Elasticsearch Version: 7.5.x

elastic

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.