elasticsearchpyes

Does rescore support nested queries?


I am trying to rescore my query results using a nested query, but I get the following exception, which I believe means that rescore does not support nested queries:

nested: ElasticsearchIllegalArgumentException[rescore doesn't support [path]];

Is that so ?

The nested objects contain a key field that should be matched and a weight field that should be used as score. Here is the query:

POST myindex/_search
{
 "query" : {
    "match" : {
       "field1" : {
          "query" : "my_query_string",
          "type" : "boolean"
       }
    }
 },
 "rescore" : {
    "window_size" : 50,
    "query": {
      "nested": {
        "path": "path.to.nested.object",
        "score_mode" : "avg",
        "query": {
          "function_score": {
            "query":{
              "constant_score": {
                "query": { 
                  "match": {
                    "path.to.nested.object.key": "my_query_string"
                  }
                }
              }
            },
            "script_score": {
              "script": "doc['path.to.nested.object.weight'].value"
            }
          }
        }
      }
    }
  }
}

Solution

  • There is a syntax error in your query With re-score you need to use rescore_query since query_rescorer is the only implementation supported at the moment.

    The following should work :

    POST myindex/_search
    {
       "query": {
          "match": {
             "field1": {
                "query": "my_query_string",
                "type": "boolean"
             }
          }
       },
       "rescore": {
          "window_size": 50,
          "query": {
             "rescore_query": {
                "nested": {
                   "path": "path.to.nested.object",
                   "score_mode": "avg",
                   "query": {
                      "function_score": {
                         "query": {
                            "constant_score": {
                               "query": {
                                  "match": {
                                     "path.to.nested.object.key": "my_query_string"
                                  }
                               }
                            }
                         },
                         "script_score": {
                            "script": "doc['path.to.nested.object.weight'].value"
                         }
                      }
                   }
                }
             }
          }
       }
    }