nodes: 0, module; 1, function_definition; 2, function_name:default_sorter_factory; 3, parameters; 4, block; 5, identifier:search; 6, identifier:index; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, comment:# Get default sorting if sort is not specified.; 11, if_statement; 12, comment:# Parse sort argument; 13, expression_statement; 14, comment:# Get sort options; 15, expression_statement; 16, if_statement; 17, comment:# Get fields to sort query by; 18, expression_statement; 19, return_statement; 20, comment:"""Default sort query factory.
  
      :param query: Search query.
      :param index: Index to search in.
      :returns: Tuple of (query, URL arguments).
      """; 21, assignment; 22, assignment; 23, not_operator; 24, comment:# cast to six.text_type to handle unicodes in Python 2; 25, block; 26, assignment; 27, assignment; 28, comparison_operator:sort_options is None; 29, block; 30, assignment; 31, tuple; 32, identifier:sort_arg_name; 33, string; 34, identifier:urlfield; 35, call; 36, identifier:urlfield; 37, expression_statement; 38, expression_statement; 39, pattern_list; 40, call; 41, identifier:sort_options; 42, call; 43, identifier:sort_options; 44, None; 45, return_statement; 46, identifier:search; 47, call; 48, identifier:search; 49, dictionary; 50, string_content:sort; 51, attribute; 52, argument_list; 53, assignment; 54, assignment; 55, identifier:key; 56, identifier:asc; 57, identifier:parse_sort_field; 58, argument_list; 59, attribute; 60, argument_list; 61, tuple; 62, attribute; 63, argument_list; 64, pair; 65, attribute; 66, identifier:get; 67, identifier:sort_arg_name; 68, string; 69, keyword_argument; 70, identifier:has_query; 71, call; 72, identifier:urlfield; 73, call; 74, identifier:urlfield; 75, call; 76, identifier:get; 77, identifier:key; 78, identifier:search; 79, dictionary; 80, identifier:search; 81, identifier:sort; 82, list_splat; 83, identifier:sort_arg_name; 84, identifier:urlfield; 85, identifier:request; 86, identifier:values; 87, identifier:type; 88, identifier:str; 89, attribute; 90, argument_list; 91, attribute; 92, argument_list; 93, attribute; 94, argument_list; 95, list_comprehension; 96, attribute; 97, identifier:get; 98, string; 99, keyword_argument; 100, call; 101, identifier:get; 102, conditional_expression:'query' if has_query else 'noquery'; 103, string; 104, subscript; 105, identifier:get; 106, identifier:index; 107, dictionary; 108, call; 109, for_in_clause; 110, identifier:request; 111, identifier:values; 112, string_content:q; 113, identifier:type; 114, attribute; 115, attribute; 116, argument_list; 117, string; 118, identifier:has_query; 119, string; 120, attribute; 121, string; 122, identifier:eval_field; 123, argument_list; 124, identifier:f; 125, subscript; 126, identifier:six; 127, identifier:text_type; 128, subscript; 129, identifier:get; 130, identifier:index; 131, dictionary; 132, string_content:query; 133, string_content:noquery; 134, identifier:current_app; 135, identifier:config; 136, string_content:RECORDS_REST_SORT_OPTIONS; 137, identifier:f; 138, identifier:asc; 139, identifier:sort_options; 140, string; 141, attribute; 142, string; 143, string_content:fields; 144, identifier:current_app; 145, identifier:config; 146, string_content:RECORDS_REST_DEFAULT_SORT
code: def default_sorter_factory(search, index):
      """Default sort query factory.
  
      :param query: Search query.
      :param index: Index to search in.
      :returns: Tuple of (query, URL arguments).
      """
      sort_arg_name = 'sort'
      urlfield = request.values.get(sort_arg_name, '', type=str)
  
      # Get default sorting if sort is not specified.
      if not urlfield:
          # cast to six.text_type to handle unicodes in Python 2
          has_query = request.values.get('q', type=six.text_type)
          urlfield = current_app.config['RECORDS_REST_DEFAULT_SORT'].get(
              index, {}).get('query' if has_query else 'noquery', '')
  
      # Parse sort argument
      key, asc = parse_sort_field(urlfield)
  
      # Get sort options
      sort_options = current_app.config['RECORDS_REST_SORT_OPTIONS'].get(
          index, {}).get(key)
      if sort_options is None:
          return (search, {})
  
      # Get fields to sort query by
      search = search.sort(
          *[eval_field(f, asc) for f in sort_options['fields']]
      )
      return (search, {sort_arg_name: urlfield})