code: def test_sorting_function():
      schema = fields.Schema(
          id=fields.STORED, text=fields.TEXT(stored=True, vector=True)
      )
      ix = RamStorage().create_index(schema)
      w = ix.writer()
      domain = ("alfa", "bravo", "charlie")
      count = 1
      for w1 in domain:
          for w2 in domain:
              for w3 in domain:
                  for w4 in domain:
                      w.add_document(
                          id=count, text=u(" ").join((w1, w2, w3, w4))
                      )
                      count += 1
      w.commit()
  
      def fn(searcher, docnum):
          v = dict(searcher.vector_as("frequency", docnum, "text"))
          # Sort documents that have equal number of "alfa"
          # and "bravo" first
          return 0 - 1.0 / (
              abs(v.get("alfa", 0) - v.get("bravo", 0)) + 1.0
          )
  
      fnfacet = sorting.FunctionFacet(fn)
  
      with ix.searcher() as s:
          q = query.And(
              [
                  query.Term("text", u("alfa")),
                  query.Term("text", u("bravo")),
              ]
          )
          results = s.search(q, sortedby=fnfacet)
          r = [hit["text"] for hit in results]
          for t in r[:10]:
              tks = t.split()
              assert tks.count("alfa") == tks.count("bravo")
  
imports: from __future__ import with_statement
  from datetime import datetime, timedelta
  import random
  from whoosh import fields, query, sorting, columns
  from whoosh.compat import u
  from whoosh.compat import permutations, xrange
  from whoosh.filedb.filestore import RamStorage
  from whoosh.util.testing import TempIndex
label_desc: Use image node_id 28 for calling a global function with example usage: test_sorting_function() and returns: unknown