Source code for airflow.providers.opensearch.log.os_response
# Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF 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.from__future__importannotationsfromcollections.abcimportIteratordef_wrap(val):ifisinstance(val,dict):returnAttributeDict(val)returnval
[docs]classAttributeList:"""Helper class to provide attribute like access to List objects."""def__init__(self,_list):ifnotisinstance(_list,list):_list=list(_list)self._l_=_list
[docs]def__getitem__(self,k):"""Retrieve an item or a slice from the list. If the item is a dictionary, it is wrapped in an AttributeDict."""val=self._l_[k]ifisinstance(val,slice):returnAttributeList(val)return_wrap(val)
[docs]def__iter__(self):"""Provide an iterator for the list or the dictionary."""return(_wrap(i)foriinself._l_)
[docs]def__bool__(self):"""Check if the list is non-empty."""returnbool(self._l_)
[docs]classAttributeDict:"""Helper class to provide attribute like access to Dictionary objects."""def__init__(self,d):super().__setattr__("_d_",d)
[docs]def__getattr__(self,attr_name):"""Retrieve an item as an attribute from the dictionary."""try:returnself.__getitem__(attr_name)exceptKeyError:raiseAttributeError(f"{self.__class__.__name__!r} object has no attribute {attr_name!r}")
[docs]def__getitem__(self,key):"""Retrieve an item using a key from the dictionary."""return_wrap(self._d_[key])
[docs]classHit(AttributeDict):""" The Hit class is used to manage and access elements in a document. It inherits from the AttributeDict class and provides attribute-like access to its elements, similar to a dictionary. """def__init__(self,document):data={}if"_source"indocument:data=document["_source"]if"fields"indocument:data.update(document["fields"])super().__init__(data)super().__setattr__("meta",HitMeta(document))
[docs]classHitMeta(AttributeDict):""" The HitMeta class is used to manage and access metadata of a document. This class inherits from the AttributeDict class and provides attribute-like access to its elements. """def__init__(self,document,exclude=("_source","_fields")):d={k[1:]ifk.startswith("_")elsek:vfor(k,v)indocument.items()ifknotinexclude}if"type"ind:# make sure we are consistent everywhere in pythond["doc_type"]=d.pop("type")super().__init__(d)
[docs]classOpensearchResponse(AttributeDict):""" The OpensearchResponse class is used to manage and access the response from an Opensearch search. This class can be iterated over directly to access hits in the response. Indexing the class instance with an integer or slice will also access the hits. The class also evaluates to True if there are any hits in the response. The hits property returns an AttributeList of hits in the response, with each hit transformed into an instance of the doc_class if provided. The response parameter stores the dictionary returned by the Elasticsearch client search method. """def__init__(self,search,response,doc_class=None):super().__setattr__("_search",search)super().__setattr__("_doc_class",doc_class)super().__init__(response)
[docs]def__iter__(self)->Iterator[Hit]:"""Provide an iterator over the hits in the Elasticsearch response."""returniter(self.hits)
[docs]def__getitem__(self,key):"""Retrieve a specific hit or a slice of hits from the Elasticsearch response."""ifisinstance(key,(slice,int)):returnself.hits[key]returnsuper().__getitem__(key)
[docs]def__bool__(self):"""Evaluate the presence of hits in the Elasticsearch response."""returnbool(self.hits)
@property
[docs]defhits(self)->list[Hit]:""" This property provides access to the hits (i.e., the results) of the Opensearch response. The hits are represented as an `AttributeList` of `Hit` instances, which allow for easy, attribute-like access to the hit data. The hits are lazily loaded, meaning they're not processed until this property is accessed. Upon first access, the hits data from the response is processed using the `_get_result` method of the associated `Search` instance (i.e. an instance from ElasticsearchTaskHandler class), and the results are stored for future accesses. Each hit also includes all the additional data present in the "hits" field of the response, accessible as attributes of the hit. """ifnothasattr(self,"_hits"):h=self._d_["hits"]try:hits=AttributeList(map(self._search._get_result,h["hits"]))exceptAttributeErrorase:raiseTypeError("Could not parse hits.",e)super().__setattr__("_hits",hits)forkinh:setattr(self._hits,k,_wrap(h[k]))returnself._hits