<s> from __future__ import unicode_literals , absolute_import <EOL> from . import Filter <EOL> from copy import deepcopy <EOL> from six import string_types <EOL> from pyeqs . dsl import MatchAll , QueryString , ScriptScore <EOL> class QueryBuilder ( object ) : <EOL> def __init__ ( self , query_string = None ) : <EOL> super ( QueryBuilder , self ) . __init__ ( ) <EOL> self . _query = { } <EOL> self . _query_dsl = { } <EOL> self . _query_string = query_string <EOL> self . _filtered = False <EOL> self . _filter_dsl = None <EOL> self . _scored = False <EOL> self . _score_dsl = None <EOL> self . _min_score = None <EOL> self . _track_scores = False <EOL> self . _sorted = False <EOL> self . _sorting = None <EOL> self . _fields = [ ] <EOL> self . _aggregated = False <EOL> self . _aggregation_dsl = None <EOL> self . _build_query ( ) <EOL> def _build_query ( self ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( self . _query_string , QueryString ) : <EOL> self . _query_dsl = self . _query_string <EOL> elif isinstance ( self . _query_string , string_types ) : <EOL> self . _query_dsl = QueryString ( self . _query_string ) <EOL> else : <EOL> self . _query_dsl = MatchAll ( ) <EOL> def _build_filtered_query ( self , f , operator ) : <EOL> """<STR_LIT>""" <EOL> self . _filtered = True <EOL> if isinstance ( f , Filter ) : <EOL> filter_object = f <EOL> else : <EOL> filter_object = Filter ( operator ) . filter ( f ) <EOL> self . _filter_dsl = filter_object <EOL> def filter ( self , f , operator = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if self . _filtered : <EOL> self . _filter_dsl . filter ( f ) <EOL> else : <EOL> self . _build_filtered_query ( f , operator ) <EOL> return self <EOL> def _build_sorted_query ( self ) : <EOL> self . _sorted = True <EOL> self . _sorting = [ ] <EOL> def sort ( self , sorting ) : <EOL> if not self . _sorted : <EOL> self . _build_sorted_query ( ) <EOL> self . _sorting . append ( sorting ) <EOL> return self <EOL> def score ( self , scoring_block , boost_mode = "<STR_LIT:replace>" , score_mode = "<STR_LIT>" , min_score = None , track_scores = False ) : <EOL> if not self . _scored : <EOL> self . _scored = True <EOL> self . _score_dsl = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : boost_mode , <EOL> "<STR_LIT>" : score_mode <EOL> } <EOL> } <EOL> if isinstance ( scoring_block , ScriptScore ) : <EOL> self . _score_dsl [ "<STR_LIT>" ] [ "<STR_LIT>" ] . append ( { "<STR_LIT>" : scoring_block } ) <EOL> else : <EOL> self . _score_dsl [ "<STR_LIT>" ] [ "<STR_LIT>" ] . append ( scoring_block ) <EOL> if min_score is not None : <EOL> self . _min_score = min_score <EOL> if track_scores : <EOL> self . _track_scores = track_scores <EOL> return self <EOL> def _build_aggregation_query ( self , aggregation ) : <EOL> self . _aggregated = True <EOL> return aggregation <EOL> def aggregate ( self , aggregation ) : <EOL> if self . _aggregated : <EOL> self . _aggregation_dsl . update ( aggregation ) <EOL> else : <EOL> self . _aggregation_dsl = self . _build_aggregation_query ( aggregation ) <EOL> return self <EOL> def fields ( self , fields ) : <EOL> self . _fields . append ( fields ) <EOL> def _finalize_query ( self ) : <EOL> query = { <EOL> "<STR_LIT>" : self . _query_dsl <EOL> } <EOL> if self . _filtered : <EOL> filtered_query = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : self . _filter_dsl <EOL> } <EOL> } <EOL> } <EOL> filtered_query [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] = deepcopy ( query [ "<STR_LIT>" ] ) <EOL> query = filtered_query <EOL> if self . _scored : <EOL> scored_query = { <EOL> "<STR_LIT>" : self . _score_dsl <EOL> } <EOL> if self . _min_score is not None : <EOL> scored_query [ "<STR_LIT>" ] = self . _min_score <EOL> if self . _track_scores : <EOL> scored_query [ "<STR_LIT>" ] = self . _track_scores <EOL> scored_query [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] = deepcopy ( query [ "<STR_LIT>" ] ) <EOL> query = scored_query <EOL> if self . _sorted : <EOL> query [ "<STR_LIT>" ] = self . _sorting <EOL> if self . _fields : <EOL> query [ "<STR_LIT>" ] = self . _fields <EOL> if self . _aggregated : <EOL> query [ "<STR_LIT>" ] = self . _aggregation_dsl <EOL> return query </s>
