code: def range_search_max_results(
      index,
      query_iterator,
      radius,
      max_results=None,
      min_results=None,
      shard=False,
      ngpu=0,
      clip_to_min=False,
  ):
      """Performs a range search with many queries (given by an iterator)
      and adjusts the threshold on-the-fly so that the total results
      table does not grow larger than max_results.
  
      If ngpu != 0, the function moves the index to this many GPUs to
      speed up search.
      """
      # TODO: all result manipulations are in python, should move to C++ if perf
      # critical
      is_binary_index = isinstance(index, faiss.IndexBinary)
  
      if min_results is None:
          assert max_results is not None
          min_results = int(0.8 * max_results)
  
      if max_results is None:
          assert min_results is not None
          max_results = int(min_results * 1.5)
  
      if ngpu == -1:
          ngpu = faiss.get_num_gpus()
  
      if ngpu:
          LOG.info("running on %d GPUs" % ngpu)
          co = faiss.GpuMultipleClonerOptions()
          co.shard = shard
          index_gpu = faiss.index_cpu_to_all_gpus(
              index, co=co, ngpu=ngpu
          )
      else:
          index_gpu = None
  
      t_start = time.time()
      t_search = t_post_process = 0
      qtot = totres = raw_totres = 0
      res_batches = []
  
      for xqi in query_iterator:
          t0 = time.time()
          LOG.debug(f"searching {len(xqi)} vectors")
          if index_gpu:
              lims_i, Di, Ii = range_search_gpu(
                  xqi, radius, index_gpu, index
              )
          else:
              lims_i, Di, Ii = index.range_search(xqi, radius)
  
          nres_i = lims_i[1:] - lims_i[:-1]
          raw_totres += len(Di)
          qtot += len(xqi)
  
          t1 = time.time()
          if is_binary_index:
              # weird Faiss quirk that returns floats for Hamming distances
              Di = Di.astype("int16")
  
          totres += len(Di)
          res_batches.append((nres_i, Di, Ii))
  
          if max_results is not None and totres > max_results:
              LOG.info(
                  "too many results %d > %d, scaling back radius"
                  % (totres, max_results)
              )
              radius, totres = apply_maxres(
                  res_batches,
                  min_results,
                  keep_max=index.metric_type
                  == faiss.METRIC_INNER_PRODUCT,
              )
          t2 = time.time()
          t_search += t1 - t0
          t_post_process += t2 - t1
          LOG.debug(
              "   [%.3f s] %d queries done, %d results"
              % (time.time() - t_start, qtot, totres)
          )
  
      LOG.info(
          "search done in %.3f s + %.3f s, total %d results, end threshold %g"
          % (t_search, t_post_process, totres, radius)
      )
  
      if clip_to_min and totres > min_results:
          radius, totres = apply_maxres(
              res_batches,
              min_results,
              keep_max=index.metric_type == faiss.METRIC_INNER_PRODUCT,
          )
  
      nres = np.hstack([nres_i for nres_i, dis_i, ids_i in res_batches])
      dis = np.hstack([dis_i for nres_i, dis_i, ids_i in res_batches])
      ids = np.hstack([ids_i for nres_i, dis_i, ids_i in res_batches])
  
      lims = np.zeros(len(nres) + 1, dtype="uint64")
      lims[1:] = np.cumsum(nres)
  
      return radius, lims, dis, ids
  
file_path: faiss/contrib/exhaustive_search.py
imports: import faiss
  import time
  import numpy
  import logging
label_desc: Use image node_id 7 for calling a global function with example usage: range_search_max_results(index, query_iterator, radius, max_results, min_results, shard, ngpu, clip_to_min) and returns: radius,  lims,  dis,  ids