# SPDX-License-Identifier: Apache-2.0## The OpenSearch Contributors require contributions made to# this file be licensed under the Apache-2.0 license or a# compatible open source license.## Modifications Copyright OpenSearch Contributors. See# GitHub history for details.## Licensed to Elasticsearch B.V. under one or more contributor# license agreements. See the NOTICE file distributed with# this work for additional information regarding copyright# ownership. Elasticsearch B.V. licenses this file to you under# the Apache License, Version 2.0 (the "License"); you may# not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.importcollections.abcascollections_abcfromtypingimportAny,Optionalfrom.utilsimportDslBasedefSF(name_or_sf:Any,**params:Any)->Any:# pylint: disable=invalid-name# {"script_score": {"script": "_score"}, "filter": {}}ifisinstance(name_or_sf,collections_abc.Mapping):ifparams:raiseValueError("SF() cannot accept parameters when passing in a dict.")kwargs={}sf=name_or_sf.copy()# type: ignoreforkinScoreFunction._param_defs:ifkinname_or_sf:kwargs[k]=sf.pop(k)# not sf, so just filter+weight, which used to be boost factorifnotsf:name="boost_factor"# {'FUNCTION': {...}}eliflen(sf)==1:name,params=sf.popitem()else:raiseValueError(f"SF() got an unexpected fields in the dictionary: {sf!r}")# boost factor special case, see https://github.com/elastic/elasticsearch/issues/6343ifnotisinstance(params,collections_abc.Mapping):params={"value":params}# mix known params (from _param_defs) and from inside the functionkwargs.update(params)returnScoreFunction.get_dsl_class(name)(**kwargs)# ScriptScore(script="_score", filter=Q())ifisinstance(name_or_sf,ScoreFunction):ifparams:raiseValueError("SF() cannot accept parameters when passing in a ScoreFunction object.")returnname_or_sf# "script_score", script="_score", filter=Q()returnScoreFunction.get_dsl_class(name_or_sf)(**params)
[docs]defto_dict(self)->Any:d=super().to_dict()# filter and query dicts should be at the same level as usforkinself._param_defs:ifkind[self.name]:d[k]=d[self.name].pop(k)returnd