1
votes

When using elasticsearch-7 I'm confused by es compound queries syntax.

Though reading es documents repeatedly but i just find standard syntax of Boolean or Constant score seperately.

As it illuminate,i understand what is 'query context' and what is 'filter context'.But when combining these two query type in a single query i don't know what it mean.

Let's see a example:

GET /classes_test/_search
{
  "size": "21",
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [
            {
              "match": {
                "class_name": "29386556"
              }
            }
          ],
          "should": [
            {
              "term": {
                "master": "7033560"
              }
            },
            {
              "term": {
                "assistant": "7033560"
              }
            },
            {
              "term": {
                "students": "7033560"
              }
            }
          ],
          "minimum_should_match": 1,
          "must_not": [
            {
              "term": {
                "class_id": 0
              }
            }
          ],
          "filter": [
            {
              "term": {
                "class_status": "1"
              }
            }
          ]
        }
      }
    }
  }
}

This query can be executed and response well.Each item in response content has a '_score' value with 1.0.

So,is it mean that the sub bool query as a entirety is in a filter context though it has a 'must' and 'should'?

Also i found boolean query can have a constant score sub query.

Why es allow these syntax but has no more words to explain?

1

1 Answers

2
votes

If you use a constant_score query, you'll never get scores different than 1.0, unless you specify boost parameters in which case the score will match those.

If you need scoring you obviously need to ditch constant_score.

In your case, your match query on class_name cannot yield any other score than 1 or 0 since this is basically a yes/no filter, not a matching based on full-text search.

To sum up, all your query executes in a filter context (hence score 0 or 1) since you don't rely on full-text search. So you get scoring whenever you use full-text search, not because you use a match query. In your case, you can merge all must constraints into filter, it won't make any difference since you only have filters (yes/no matches) and no full-text search.