Example #1
0
    def __init__(self, query, query_filter):
        ''' Construct a filtered query '''
        if not isinstance(query, Query):
            raise QueryError("not a Query")
        if not isinstance(query_filter, Filter):
            raise QueryError("not a Filter")

        if ElasticSettings.version()['major'] < 2:
            logger.warn('USING DEPRECATED FILTERED QUERY')
            self.query = {"filtered": {"query": query.query}}
            self.query["filtered"].update(query_filter.filter)
        else:
            if 'match_all' in query.query:
                query.query_wrap()
            self.query = BoolQuery(must_arr=query, b_filter=query_filter).query
Example #2
0
    def create_score_function(cls, score_funtion_type, *args, function_filter=None, weight=None, **kwargs):
        SCORE_FUNCTION = ScoreFunction.SCORE_FUNCTION
        if score_funtion_type not in SCORE_FUNCTION:
            raise QueryError("not a defined score function type")

        if score_funtion_type == 'weight':
            return ScoreFunction({'weight': args[0]}, function_filter=function_filter, weight=weight)

        score_function = {score_funtion_type: {}}
        for k, v in kwargs.items():
            if k not in SCORE_FUNCTION[score_funtion_type]:
                raise QueryError(k+" is not a defined parameter")
            if not isinstance(v, SCORE_FUNCTION[score_funtion_type][k]):
                raise QueryError(str(v)+" incorrect type for "+k)
            score_function[score_funtion_type][k] = v
        return ScoreFunction(score_function, function_filter=function_filter, weight=weight)
Example #3
0
 def __init__(self, query):
     '''
     @type  query: dictionary
     @param query: The query in JSON format.
     '''
     if not isinstance(query, dict):
         raise QueryError("query is not a dictionary")
     self.query = query
Example #4
0
    def __init__(self, query_or_filter, score_functions, score_mode=None,
                 boost_mode=None, max_boost=None, min_score=None):
        ''' Construct a function score query '''
        if isinstance(query_or_filter, Query):
            self.query = {"function_score": {"query": query_or_filter.query}}
        elif isinstance(query_or_filter, Filter):
            self.query = {"function_score": query_or_filter.filter}
        else:
            raise QueryError("not a Query or Filter")

        self.query["function_score"]["functions"] = []
        for sf in score_functions:
            if not isinstance(sf, ScoreFunction):
                raise QueryError("not a ScoreFunction")
            self.query["function_score"]["functions"].append(sf.score_funtion)

        if score_mode is not None and isinstance(score_mode, str):
            self.query["function_score"]['score_mode'] = score_mode
        if boost_mode is not None and isinstance(boost_mode, str):
            self.query["function_score"]['boost_mode'] = boost_mode
        if max_boost is not None and isinstance(max_boost, float):
            self.query["function_score"]['max_boost'] = max_boost
        if min_score is not None and isinstance(min_score, float):
            self.query["function_score"]['min_score'] = min_score
Example #5
0
 def nested(cls, path, query, score_mode=None):
     ''' Factory method for Nested query.
     @type  path: string
     @param path: nested object path.
     @type  query: Query
     @param query: query.
     @type  score_mode: string
     @param score_mode: how inner children matching affects scoring of parent (e.g. avg, sum, max and none).
     '''
     if not isinstance(query, Query):
         raise QueryError("not a Query")
     if score_mode is not None:
         return cls({"nested": {"path": path, "query": query.query, "score_mode": score_mode}})
     else:
         return cls({"nested": {"path": path, "query": query.query}})
Example #6
0
    def query_string(cls, query_term, **kwargs):
        ''' Factory method for
        U{Query String Query<www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html>}
        Simple wildcards can be used with the fields supplied
        (e.g. "fields" : ["city.*"]).

        @type  query_term: string
        @param query_term: The query string.
        @type  kwargs: L{STRING_OPTS}
        @keyword kwargs: Optional parameters for Query String Query.
        @return: L{Query}
        '''
        query = {"query_string": {"query": query_term}}

        for key, value in kwargs.items():
            if key not in Query.STRING_OPTS:
                raise QueryError("option "+key+" unrecognised as a String Query option")
            query["query_string"][key] = value
        return cls(query)
Example #7
0
 def filter(self, b_filter):
     if not isinstance(b_filter, Filter):
         raise QueryError("not a Filter")
     self.query["bool"].update(b_filter.filter)
Example #8
0
 def __init__(self, child_type, query):
     if not isinstance(query, Query):
         raise QueryError("not a Query")
     self.query = {"has_child": {"type": child_type, "query": query.query}}
Example #9
0
 def __init__(self, parent_type, query):
     if not isinstance(query, Query):
         raise QueryError("not a Query")
     self.query = {"has_parent": {"type": parent_type, "query": query.query}}
Example #10
0
 def _is_array_query(cls, arr):
     ''' Evaluate if array contents are Query objects. '''
     if not all(isinstance(y, (Query)) for y in arr):
         raise QueryError("not a Query")
     return True