nodes: 0, module; 1, function_definition; 2, function_name:search; 3, parameters; 4, block; 5, identifier:cls; 6, identifier:term; 7, default_parameter; 8, expression_statement; 9, if_statement; 10, comment:# If fields are provided, override the ones in the class; 11, if_statement; 12, expression_statement; 13, comment:# Cache the LIKE terms; 14, expression_statement; 15, expression_statement; 16, comment:# Cache the order by terms; 17, comment:# @TODO Peewee's order_by supports an `extend` kwarg will will allow; 18, comment:# for updating of the order by part of the query, but it's only; 19, comment:# supported in Peewee 2.8.5 and newer. Determine if we can support this; 20, comment:# before switching.; 21, comment:# http://docs.peewee-orm.com/en/stable/peewee/api.html#SelectQuery.order_by; 22, expression_statement; 23, comment:# Store the clauses seperately because it is needed to perform an OR on; 24, comment:# them and that's somehow impossible with their query builder in; 25, comment:# a loop.; 26, expression_statement; 27, for_statement; 28, comment:# Apply the clauses to the query; 29, expression_statement; 30, comment:# Apply the sort order so it's influenced by the search term relevance.; 31, expression_statement; 32, return_statement; 33, identifier:fields; 34, tuple; 35, comment:"""Generic SQL search function that uses SQL ``LIKE`` to search the
          database for matching records. The records are sorted by their
          relavancey to the search term.
  
          The query searches and sorts on the folling criteria, in order, where
          the target string is ``exactly``:
  
          1. Straight equality (``x = 'exactly'``)
          2. Right hand ``LIKE`` (``x LIKE 'exact%'``)
          3. Substring ``LIKE`` (``x LIKE %act%``)
  
          Args:
              term (str): The search term to apply to the query.
  
          Keyword Args:
              fields (list|tuple|None): An optional list of fields to apply the
                  search to. If not provided, the class variable
                  ``Meta.search_fields`` will be used by default.
          Returns:
              peewee.SelectQuery: An unexecuted query for the records.
  
          Raises:
              AttributeError: Raised if `search_fields` isn't defined in the
                  class and `fields` aren't provided for the function.
          """; 36, not_operator; 37, block; 38, not_operator; 39, block; 40, assignment; 41, assignment; 42, assignment; 43, assignment; 44, assignment; 45, identifier:field_name; 46, identifier:fields; 47, comment:# Cache the field, raising an exception if the field doesn't; 48, comment:# exist.; 49, block; 50, assignment; 51, assignment; 52, identifier:query; 53, call; 54, raise_statement; 55, identifier:fields; 56, expression_statement; 57, identifier:query; 58, call; 59, identifier:like_term; 60, call; 61, identifier:full_like_term; 62, call; 63, identifier:order_by; 64, list; 65, identifier:clauses; 66, list; 67, expression_statement; 68, comment:# Apply the search term case insensitively; 69, expression_statement; 70, expression_statement; 71, identifier:query; 72, call; 73, identifier:query; 74, call; 75, identifier:any; 76, argument_list; 77, call; 78, assignment; 79, attribute; 80, argument_list; 81, attribute; 82, argument_list; 83, attribute; 84, argument_list; 85, assignment; 86, call; 87, call; 88, attribute; 89, argument_list; 90, attribute; 91, argument_list; 92, tuple; 93, identifier:AttributeError; 94, argument_list; 95, identifier:fields; 96, attribute; 97, identifier:cls; 98, identifier:select; 99, string; 100, identifier:join; 101, tuple; 102, string; 103, identifier:join; 104, tuple; 105, identifier:field; 106, call; 107, attribute; 108, argument_list; 109, attribute; 110, argument_list; 111, identifier:query; 112, identifier:where; 113, call; 114, identifier:query; 115, identifier:order_by; 116, list_splat; 117, attribute; 118, identifier:fields; 119, concatenated_string; 120, attribute; 121, identifier:search_fields; 122, identifier:term; 123, string; 124, string; 125, identifier:term; 126, string; 127, identifier:getattr; 128, argument_list; 129, identifier:clauses; 130, identifier:append; 131, binary_operator:(field == term) |
                  (field ** like_term) |
                  (field ** full_like_term); 132, identifier:order_by; 133, identifier:append; 134, call; 135, identifier:reduce; 136, argument_list; 137, identifier:order_by; 138, attribute; 139, identifier:search_fields; 140, string:"A list of searchable fields must be provided in the class's "; 141, string:"search_fields or provided to this function in the `fields` "; 142, string:"kwarg."; 143, identifier:cls; 144, identifier:_meta; 145, string_content:%; 146, string_content:%; 147, string_content:%; 148, identifier:cls; 149, identifier:field_name; 150, binary_operator:(field == term) |
                  (field ** like_term); 151, parenthesized_expression; 152, attribute; 153, argument_list; 154, attribute; 155, identifier:clauses; 156, identifier:cls; 157, identifier:_meta; 158, parenthesized_expression; 159, parenthesized_expression; 160, binary_operator:field ** full_like_term; 161, call; 162, identifier:asc; 163, identifier:operator; 164, identifier:or_; 165, comparison_operator:field == term; 166, binary_operator:field ** like_term; 167, identifier:field; 168, identifier:full_like_term; 169, identifier:case; 170, argument_list; 171, identifier:field; 172, identifier:term; 173, identifier:field; 174, identifier:like_term; 175, None; 176, tuple; 177, keyword_argument; 178, comment:# Straight matches should show up first; 179, tuple; 180, comment:# Similar terms should show up second; 181, tuple; 182, comment:# Substring matches should show up third; 183, tuple; 184, identifier:default; 185, integer:3; 186, comparison_operator:field == term; 187, integer:0; 188, binary_operator:field ** like_term; 189, integer:1; 190, binary_operator:field ** full_like_term; 191, integer:2; 192, identifier:field; 193, identifier:term; 194, identifier:field; 195, identifier:like_term; 196, identifier:field; 197, identifier:full_like_term
code: def search(cls, term, fields=()):
          """Generic SQL search function that uses SQL ``LIKE`` to search the
          database for matching records. The records are sorted by their
          relavancey to the search term.
  
          The query searches and sorts on the folling criteria, in order, where
          the target string is ``exactly``:
  
          1. Straight equality (``x = 'exactly'``)
          2. Right hand ``LIKE`` (``x LIKE 'exact%'``)
          3. Substring ``LIKE`` (``x LIKE %act%``)
  
          Args:
              term (str): The search term to apply to the query.
  
          Keyword Args:
              fields (list|tuple|None): An optional list of fields to apply the
                  search to. If not provided, the class variable
                  ``Meta.search_fields`` will be used by default.
          Returns:
              peewee.SelectQuery: An unexecuted query for the records.
  
          Raises:
              AttributeError: Raised if `search_fields` isn't defined in the
                  class and `fields` aren't provided for the function.
          """
          if not any((cls._meta.search_fields, fields)):
              raise AttributeError(
                  "A list of searchable fields must be provided in the class's "
                  "search_fields or provided to this function in the `fields` "
                  "kwarg."
              )
  
          # If fields are provided, override the ones in the class
          if not fields:
              fields = cls._meta.search_fields
  
          query = cls.select()
  
          # Cache the LIKE terms
          like_term = ''.join((term, '%'))
          full_like_term = ''.join(('%', term, '%'))
  
          # Cache the order by terms
          # @TODO Peewee's order_by supports an `extend` kwarg will will allow
          # for updating of the order by part of the query, but it's only
          # supported in Peewee 2.8.5 and newer. Determine if we can support this
          # before switching.
          # http://docs.peewee-orm.com/en/stable/peewee/api.html#SelectQuery.order_by
          order_by = []
  
          # Store the clauses seperately because it is needed to perform an OR on
          # them and that's somehow impossible with their query builder in
          # a loop.
          clauses = []
  
          for field_name in fields:
              # Cache the field, raising an exception if the field doesn't
              # exist.
              field = getattr(cls, field_name)
  
              # Apply the search term case insensitively
              clauses.append(
                  (field == term) |
                  (field ** like_term) |
                  (field ** full_like_term)
              )
  
              order_by.append(case(None, (
                  # Straight matches should show up first
                  (field == term, 0),
                  # Similar terms should show up second
                  (field ** like_term, 1),
                  # Substring matches should show up third
                  (field ** full_like_term, 2),
              ), default=3).asc())
  
          # Apply the clauses to the query
          query = query.where(reduce(operator.or_, clauses))
  
          # Apply the sort order so it's influenced by the search term relevance.
          query = query.order_by(*order_by)
  
          return query