class_docstr: Ranks Documents based on the value of their specific meta field.
  The ranking can be performed in descending order or ascending order.
  
  Usage example:
  ```
  from haystack import Document
  from haystack.components.rankers import MetaFieldRanker
  
  ranker = MetaFieldRanker(meta_field="rating")
  docs = [
      Document(text="Paris", meta={"rating": 1.3}),
      Document(text="Berlin", meta={"rating": 0.7}),
      Document(text="Barcelona", meta={"rating": 2.1}),
  ]
  
  output = ranker.run(documents=docs)
  docs = output["documents"]
  assert docs[0].text == "Barcelona"
docstr: Use this method to rank a list of Documents based on the selected meta field by:
  1. Sorting the Documents by the meta field in descending or ascending order.
  2. Merging the scores from the meta field with the scores from the previous component according to the strategy and weight provided.
  3. Returning the top-k documents.
  
  :param documents: Documents to be ranked.
  :param top_k: (optional) The number of Documents you want the Ranker to return.
          If not provided, the top_k provided at initialization time is used.
  :param weight: (optional) In range [0,1].
          0 disables ranking by a meta field.
          0.5 content and meta fields have the same impact for the ranking.
          1 means ranking by a meta field only. The highest value comes first.
          If not provided, the weight provided at initialization time is used.
  :param ranking_mode: (optional) The mode used to combine the Retriever's and Ranker's scores.
          Possible values are 'reciprocal_rank_fusion' (default) and 'linear_score'.
          Use the 'score' mode only with Retrievers or Rankers that return a score in range [0,1].
          If not provided, the ranking_mode provided at initialization time is used.
  :param sort_order: Whether to sort the meta field by ascending or descending order.
          Possible values are `descending` (default) and `ascending`.
          If not provided, the sort_order provided at initialization time is used.
code: def run(
      self,
      documents: List[Document],
      top_k: Optional[int] = None,
      weight: Optional[float] = None,
      ranking_mode: Optional[
          Literal["reciprocal_rank_fusion", "linear_score"]
      ] = None,
      sort_order: Optional[Literal["ascending", "descending"]] = None,
  ):
      """
      Use this method to rank a list of Documents based on the selected meta field by:
      1. Sorting the Documents by the meta field in descending or ascending order.
      2. Merging the scores from the meta field with the scores from the previous component according to the strategy and weight provided.
      3. Returning the top-k documents.
  
      :param documents: Documents to be ranked.
      :param top_k: (optional) The number of Documents you want the Ranker to return.
              If not provided, the top_k provided at initialization time is used.
      :param weight: (optional) In range [0,1].
              0 disables ranking by a meta field.
              0.5 content and meta fields have the same impact for the ranking.
              1 means ranking by a meta field only. The highest value comes first.
              If not provided, the weight provided at initialization time is used.
      :param ranking_mode: (optional) The mode used to combine the Retriever's and Ranker's scores.
              Possible values are 'reciprocal_rank_fusion' (default) and 'linear_score'.
              Use the 'score' mode only with Retrievers or Rankers that return a score in range [0,1].
              If not provided, the ranking_mode provided at initialization time is used.
      :param sort_order: Whether to sort the meta field by ascending or descending order.
              Possible values are `descending` (default) and `ascending`.
              If not provided, the sort_order provided at initialization time is used.
      """
      if not documents:
          return {"documents": []}
  
      top_k = top_k or self.top_k
      weight = weight or self.weight
      ranking_mode = ranking_mode or self.ranking_mode
      sort_order = sort_order or self.sort_order
      self._validate_params(
          weight=weight,
          top_k=top_k,
          ranking_mode=ranking_mode,
          sort_order=sort_order,
      )
  
      # If the weight is 0 then ranking by meta field is disabled and the original documents should be returned
      if weight == 0:
          return {"documents": documents[:top_k]}
  
      docs_with_meta_field = [
          doc for doc in documents if self.meta_field in doc.meta
      ]
      docs_missing_meta_field = [
          doc for doc in documents if self.meta_field not in doc.meta
      ]
  
      # If all docs are missing self.meta_field return original documents
      if len(docs_with_meta_field) == 0:
          logger.warning(
              "The parameter <meta_field> is currently set to '%s', but none of the provided Documents with IDs %s have this meta key.\n"
              "Set <meta_field> to the name of a field that is present within the provided Documents.\n"
              "Returning the <top_k> of the original Documents since there are no values to rank.",
              self.meta_field,
              ",".join([doc.id for doc in documents]),
          )
          return {"documents": documents[:top_k]}
  
      if len(docs_missing_meta_field) > 0:
          logger.warning(
              "The parameter <meta_field> is currently set to '%s' but the Documents with IDs %s don't have this meta key.\n"
              "These Documents will be placed at the end of the sorting order.",
              self.meta_field,
              ",".join([doc.id for doc in docs_missing_meta_field]),
          )
  
      # Sort the documents by self.meta_field
      reverse = sort_order == "descending"
      try:
          sorted_by_meta = sorted(
              docs_with_meta_field,
              key=lambda doc: doc.meta[self.meta_field],
              reverse=reverse,
          )
      except TypeError as error:
          # Return original documents if mixed types that are not comparable are returned (e.g. int and list)
          logger.warning(
              "Tried to sort Documents with IDs %s, but got TypeError with the message: %s\n"
              "Returning the <top_k> of the original Documents since meta field ranking is not possible.",
              ",".join([doc.id for doc in docs_with_meta_field]),
              error,
          )
          return {"documents": documents[:top_k]}
  
      # Add the docs missing the meta_field back on the end
      sorted_documents = sorted_by_meta + docs_missing_meta_field
      sorted_documents = self._merge_rankings(
          documents, sorted_documents
      )
      return {"documents": sorted_documents[:top_k]}
  
file_path: haystack/haystack/components/rankers/meta_field.py
imports: import logging
  from collections import defaultdict
  from typing import List, Dict, Any, Optional, Literal
  from haystack import Document, component, default_to_dict
label_desc: Use image node_id 4 for calling the MetaFieldRanker obj's underlying member method code with example usage: obj.run(documents, top_k, weight, ranking_mode, sort_order) and returns: dict, dict, dict, dict, dict