indexes all the URIs defined by the query into Elasticsearch

def _index_group_with_sub(self):
        """ indexes all the URIs defined by the query into Elasticsearch """

        lg = logging.getLogger("%s.%s" % (self.ln, inspect.stack()[0][3]))
        lg.setLevel(self.log_level)
        # get a list of all the uri to index
        results = run_sparql_query(sparql=self.query, namespace=self.namespace)
        # results = results[:100]
        # Start processing through uri
        self.time_start = datetime.datetime.now()
        batch_size = 12000
        if len(results) > batch_size:
            batch_end = batch_size
        else:
            batch_end = len(results)
        batch_start = 0
        batch_num = 1
        self.batch_data = {}
        self.batch_data[batch_num] = []
        end = False
        last = False
        while not end:
            lg.debug("batch %s: %s-%s", batch_num, batch_start, batch_end)
            sub_batch = []
            j = 0
            for i, subj in enumerate(results[batch_start:batch_end]):
                qry_size = 50
                if j < qry_size:
                    sub_batch.append(subj)
                if j == qry_size or i == batch_end:
                    sub_batch.append(subj)
                    th = threading.Thread(name=batch_start + i + 1,
                                          target=self._index_sub,
                                          args=(sub_batch,
                                                i+1,batch_num,))
                    th.start()
                    j = 0
                    sub_batch = []
            lg.debug(datetime.datetime.now() - self.time_start)
            main_thread = threading.main_thread()
            for t in threading.enumerate():
                if t is main_thread:
                    continue
                t.join()
            action_list = \
                    self.es_worker.make_action_list(self.batch_data[batch_num])
            self.es_worker.bulk_save(action_list)
            del self.batch_data[batch_num]
            batch_end += batch_size
            batch_start += batch_size
            if last:
                end = True
            if len(results) <= batch_end:
                batch_end = len(results)
                last = True
            batch_num += 1
            self.batch_data[batch_num] = []
            lg.debug(datetime.datetime.now() - self.time_start)