ArangoDB-Community / pyArango

Python Driver for ArangoDB with built-in validation
https://pyarango.readthedocs.io/en/latest/
Apache License 2.0
238 stars 90 forks source link

only return the first result by using pyArango #132

Closed yzho0907 closed 5 years ago

yzho0907 commented 5 years ago

I tried to process a test qury by using this: queryResult = db.AQLQuery(aql, rawResults=False, batchSize=1, bindVars=bindVars), aql is fine。 but in queryResult.response['result'] only contains one result, the same query returns 10 results(limit 10) in ArangoDB web interface. Any idea how to fix it or clue?many thx。

yzho0907 commented 5 years ago

var batchSize limits the size of result.

dilipvamsi commented 5 years ago

Hi @yzho0907 , database.py has fetch_list which has the functionality to return the complete list even though batchSize is less than the result size.

    def fetch_list(
            self, aql_query, bind_vars=None, batch_size=200,
            dont_raise_error_if_empty=False, logger=None,
            log_level=logging.DEBUG
    ):
        """Fetch list of elements by running a query and merging all the batches.
        Parameters
        ----------
        aql_query : str
            aql query string.
        bind_vars : dict, optional
            dictonary of bind variables (the default is None)
        batch_size : int, optional
            fetching batch size (the default is 200)
        dont_raise_error_if_empty: bool, optional
            do not raise error if the returned is empty. (the default is False)
        logger : Logger, optional
            logger to log the query and result.
            (the default is None means don't log)
        log_level: Logger.loglevel, optional
            level of the log. (the default is logging.DEBUG)
        Raises
        ------
        AQLFetchError
            When unable to fetch results
        Returns
        -------
        list(any)
            a list returned by query.
        """
        try:
            log = self.__get_logger(logger, log_level)
            if log is not None:
                log(aql_query)
            query = self.AQLQuery(
                aql_query, batchSize=batch_size, rawResults=True,
                bindVars=(bind_vars if bind_vars is not None else {})
            )
            batch_index = 0
            result = []
            while True:
                if len(query.response['result']) is 0:
                    break
                result.extend(query.response['result'])
                batch_index += 1
                query.nextBatch()
        except StopIteration:
            if log is not None:
                log(result)
            if len(result) is not 0:
                return result
        except:
            raise
        if batch_index == 0 and dont_raise_error_if_empty:
            return []
        raise AQLFetchError(
            "No results matched for query in fetching the batch index: %s." % (
                batch_index
            )

Regards, Dilip Vamsi.