Ejemplo n.º 1
0
    def run_query(self, es_query, es_type=None):
        """
        Run a more advanced POST based ES query

        Returns the raw query json back, or None if there's an error
        """

        logger.info(
            "ESlog: [%s.%s] ESquery: %s" %
            (self.__class__.__name__, self.domain, json.dumps(es_query)))
        if 'fields' in es_query or 'script_fields' in es_query:
            #nasty hack to add domain field to query that does specific fields.
            #do nothing if there's no field query because we get everything
            fields = es_query.get('fields', [])
            fields.append('domain')
            es_query['fields'] = fields

        try:
            es_results = self.es_interface.search(self.index,
                                                  es_type,
                                                  body=es_query)
            report_and_fail_on_shard_failures(es_results)
        except ElasticsearchException as e:
            if 'query_string' in es_query.get('query',
                                              {}).get('filtered',
                                                      {}).get('query', {}):
                # the error may have been caused by a bad query string
                # re-run with no query string to check
                querystring = es_query['query']['filtered']['query'][
                    'query_string']['query']
                new_query = es_query
                new_query['query']['filtered']['query'] = {"match_all": {}}
                new_results = self.run_query(new_query)
                if new_results:
                    # the request succeeded without that query string
                    # an error with a blank query will return None
                    raise ESUserError("Error with elasticsearch query: %s" %
                                      querystring)

            msg = "Error in elasticsearch query [%s]: %s\nquery: %s" % (
                self.index, str(e), es_query)
            raise ESError(msg)

        hits = []
        for res in es_results['hits']['hits']:
            if '_source' in res:
                res_domain = res['_source'].get('domain', None)
            elif 'fields' in res:
                res['fields'] = flatten_field_dict(res)
                res_domain = res['fields'].get('domain', None)

            # security check
            if res_domain == self.domain:
                hits.append(res)
            else:
                logger.info(
                    "Requester domain %s does not match result domain %s" %
                    (self.domain, res_domain))
        es_results['hits']['hits'] = hits
        return es_results
Ejemplo n.º 2
0
    def run_query(self, es_query, es_type=None):
        """
        Run a more advanced POST based ES query

        Returns the raw query json back, or None if there's an error
        """

        logger.info("ESlog: [%s.%s] ESquery: %s" % (self.__class__.__name__, self.domain, json.dumps(es_query)))
        if 'fields' in es_query or 'script_fields' in es_query:
            #nasty hack to add domain field to query that does specific fields.
            #do nothing if there's no field query because we get everything
            fields = es_query.get('fields', [])
            fields.append('domain')
            es_query['fields'] = fields

        try:
            es_results = self.es.search(self.index, es_type, body=es_query)
            report_and_fail_on_shard_failures(es_results)
        except ElasticsearchException as e:
            if 'query_string' in es_query.get('query', {}).get('filtered', {}).get('query', {}):
                # the error may have been caused by a bad query string
                # re-run with no query string to check
                querystring = es_query['query']['filtered']['query']['query_string']['query']
                new_query = es_query
                new_query['query']['filtered']['query'] = {"match_all": {}}
                new_results = self.run_query(new_query)
                if new_results:
                    # the request succeeded without that query string
                    # an error with a blank query will return None
                    raise ESUserError("Error with elasticsearch query: %s" %
                        querystring)

            msg = "Error in elasticsearch query [%s]: %s\nquery: %s" % (self.index, str(e), es_query)
            raise ESError(msg)

        hits = []
        for res in es_results['hits']['hits']:
            if '_source' in res:
                res_domain = res['_source'].get('domain', None)
            elif 'fields' in res:
                res['fields'] = flatten_field_dict(res)
                res_domain = res['fields'].get('domain', None)

            # security check
            if res_domain == self.domain:
                hits.append(res)
            else:
                logger.info("Requester domain %s does not match result domain %s" % (
                    self.domain, res_domain))
        es_results['hits']['hits'] = hits
        return es_results
Ejemplo n.º 3
0
    def run_query(self, es_query, es_type=None, security_check=True):
        """
        Must be called with a "fields" parameter
        Returns the raw query json back, or None if there's an error
        """

        logger.info(
            "ESlog: [%s.%s] ESquery: %s" %
            (self.__class__.__name__, self.domain, json.dumps(es_query)))

        self.validate_query(es_query)

        try:
            es_results = self.es_interface.search(self.index,
                                                  es_type,
                                                  body=es_query)
            report_and_fail_on_shard_failures(es_results)
        except ElasticsearchException as e:
            msg = "Error in elasticsearch query [%s]: %s\nquery: %s" % (
                self.index, str(e), es_query)
            notify_exception(None, message=msg)
            return None

        hits = []
        for res in es_results['hits']['hits']:
            if '_source' in res:
                raise ESUserError(
                    "This query does not support full document lookups")

            # security check
            if security_check:
                res_domain = res['fields'].get('domain_memberships.domain',
                                               None)

                if res_domain == self.domain:
                    hits.append(res)
                else:
                    logger.info(
                        "Requester domain %s does not match result domain %s" %
                        (self.domain, res_domain))
            else:
                hits.append(res)
        es_results['hits']['hits'] = hits
        return es_results
Ejemplo n.º 4
0
    def run_query(self, es_query, es_type=None, security_check=True):
        """
        Must be called with a "fields" parameter
        Returns the raw query json back, or None if there's an error
        """

        logger.info("ESlog: [%s.%s] ESquery: %s" % (
            self.__class__.__name__, self.domain, json.dumps(es_query)))

        self.validate_query(es_query)

        try:
            es_results = self.es.search(self.index, es_type, body=es_query)
            report_and_fail_on_shard_failures(es_results)
        except ElasticsearchException as e:
            msg = "Error in elasticsearch query [%s]: %s\nquery: %s" % (
                self.index, str(e), es_query)
            notify_exception(None, message=msg)
            return None

        hits = []
        for res in es_results['hits']['hits']:
            if '_source' in res:
                raise ESUserError(
                    "This query does not support full document lookups")

            # security check
            if security_check:
                res_domain = res['fields'].get('domain_memberships.domain', None)

                if res_domain == self.domain:
                    hits.append(res)
                else:
                    logger.info(
                        "Requester domain %s does not match result domain %s" % (
                        self.domain, res_domain))
            else:
                hits.append(res)
        es_results['hits']['hits'] = hits
        return es_results