Skip to content

Search to_dict() does not serialize rescore_query #1144

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

Closed
Andrii32 opened this issue Mar 22, 2019 · 2 comments · Fixed by #1458
Closed

Search to_dict() does not serialize rescore_query #1144

Andrii32 opened this issue Mar 22, 2019 · 2 comments · Fixed by #1458

Comments

@Andrii32
Copy link

Andrii32 commented Mar 22, 2019

Hi. It seems like to_dict() method of Search class does not serialize rescore_querycontent.

Example code:

search = Search(index=index_name)

positive_query = Q(
   "function_score",
   query=Q('term', tags='a'),
   script_score={"script": "_score * 1"}
)

negative_query = Q(
   "function_score",
   query=Q('term', tags='b'),
   script_score={"script": "_score * -100"}
)

search = search.query(positive_query)
search = search.extra(
   rescore={
       'window_size': 100,
       "query": {
           "rescore_query": negative_query
       }
   }
)
print(search.query().to_dict())

Result:

{
    "query": {
        "function_score": {
            "query": {
                "term": {
                    "tags": "a"
                }
            },
            "functions": [
                {
                    "script_score": {
                        "script": "_score * 1"
                    }
                }
            ]
        }
    },
    "rescore": {
        "window_size": 100,
        "query": {
            "rescore_query": FunctionScore(
                functions=[
                    ScriptScore(script="_score * -100")
                ],
                query=Term(tags="b")
            )
        }
    }
}

Expected result:

{
    "query": {
        "function_score": {
            "query": {
                "term": {
                    "tags": "a"
                }
            },
            "functions": [
                {
                    "script_score": {
                        "script": "_score * 1"
                    }
                }
            ]
        }
    },
    "rescore": {
        "window_size": 100,
        "query": {
            "rescore_query": {
                "function_score": {
                    "query": {
                        "term": {
                            "tags": "b"
                        }
                    },
                    "functions": [
                        {
                            "script_score": {
                                "script": "_score * -100"
                            }
                        }
                    ]
                }
            }
        }
    }
}

Despite that, search executes properly.

@Andrii32 Andrii32 changed the title Search to_dict() does not serialize rescore inner query Search to_dict() does not serialize rescore_query Mar 22, 2019
@kiawin
Copy link

kiawin commented Apr 16, 2019

If no one is working on it, I would like to try this out :)

@kiawin
Copy link

kiawin commented Apr 23, 2019

@Andrii32 From a quick look at elasticsearch_dsl/search.py, the following .extra() did not perform any kind of serialization (a.k.a to_dict() of this issue title), and that explains why it remains object.

    def extra(self, **kwargs):
        """
        Add extra keys to the request body. Mostly here for backwards
        compatibility.
        """
        s = self._clone()
        if 'from_' in kwargs:
            kwargs['from'] = kwargs.pop('from_')
        s._extra.update(kwargs)
        return s

A question, may I know what is possible to be put inside .extra(), so that I can see how we can handle the serialization. Alternatively, we can recurse dictionary sent into .extra() to identify Q object and serialize them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants