#NEWLINE# def get_sort_options( expressions = None , match_scorer = None , limit = 1000 ) : #NEWLINE# #INDENT# "A function to handle the sort expression API differences in 1.6.4\n  vs. 1.6.5+.\n\n An example of usage (NOTE: Do NOT put limit SortExpression or MatchScorer):\n\n  expr_list = [\n      search.SortExpression(expression='author', default_value='',\n                            direction=search.SortExpression.DESCENDING)]\n  sortopts = get_sort_options(expression=expr_list, limit=sort_limit)\n\n  The returned value is used in constructing the query options:\n\n  qoptions=search.QueryOptions(limit=doc_limit, sort_options=sortopts)\n\n  Another example illustrating sorting on an expression based on a\n  MatchScorer score:\n\n  expr_list = [\n      search.SortExpression(expression='_score + 0.001 * rating',\n                            default_value='',\n                            direction=search.SortExpression.DESCENDING)]\n  sortopts = get_sort_options(expression=expr_list,\n                              match_scorer=search.MatchScorer(),\n                              limit=sort_limit)\n\n\n  Args:\n    expression: a list of search.SortExpression. Do not set limit parameter on\n      SortExpression\n    match_scorer: a search.MatchScorer or search.RescoringMatchScorer. Do not\n      set limit parameter on either scorer\n    limit: the scoring limit\n\n  Returns: the sort options value, either list of SortOption (1.6.4) or\n  SortOptions (1.6.5), to set the sort_options field in the QueryOptions object.\n  " #NEWLINE# try : #NEWLINE# #INDENT# if search . SortOptions : #NEWLINE# #INDENT# logging . debug ( 'search.SortOptions is defined.' ) #NEWLINE# return search . SortOptions ( expressions = expressions , match_scorer = match_scorer , limit = limit ) #NEWLINE# #UNINDENT# #UNINDENT# except AttributeError : #NEWLINE# #INDENT# logging . debug ( 'search.SortOptions is not defined.' ) #NEWLINE# expr_list = [ ] #NEWLINE# if expressions : #NEWLINE# #INDENT# expr_list = [ search . SortExpression ( expression = e . expression , direction = e . direction , default_value = e . default_value , limit = expressions ) for e in expressions ] #NEWLINE# #UNINDENT# if isinstance ( match_scorer , search . MatchScorer ) : #NEWLINE# #INDENT# expr_list . append ( match_scorer . __class__ ( limit = limit ) ) #NEWLINE# #UNINDENT# logging . info ( 'sort expressions: %s' , expr_list ) #NEWLINE# return expr_list