#NEWLINE# def build_search( self ) : #NEWLINE# #INDENT# 'Builds the Elasticsearch search body represented by this S.\n\n        Loop over self.steps to build the search body that will be\n        sent to Elasticsearch. This returns a Python dict.\n\n        If you want the JSON that actually gets sent, then pass the return\n        value through :py:func:`elasticutils.utils.to_json`.\n\n        :returns: a Python dict\n\n        ' #NEWLINE# filters = [ ] #NEWLINE# filters_raw = None #NEWLINE# queries = [ ] #NEWLINE# query_raw = None #NEWLINE# sort = [ ] #NEWLINE# dict_fields = set ( ) #NEWLINE# list_fields = set ( ) #NEWLINE# facets = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# facets_raw = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# demote = None #NEWLINE# highlight_fields = set ( ) #NEWLINE# highlight_options = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# suggestions = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# explain = False #NEWLINE# as_list = as_dict = False #NEWLINE# search_type = None #NEWLINE# for ( action , value ) in self . steps : #NEWLINE# #INDENT# if ( action == 'order_by' ) : #NEWLINE# #INDENT# sort = [ ] #NEWLINE# for key in value : #NEWLINE# #INDENT# if ( isinstance ( key , string_types ) and key . startswith ( '-' ) ) : #NEWLINE# #INDENT# sort . append ( { #NEWLINE# #INDENT# key [ 1 : ] : 'desc' , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# sort . append ( key ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( action == 'values_list' ) : #NEWLINE# #INDENT# if ( not value ) : #NEWLINE# #INDENT# list_fields = set ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# list_fields |= set ( value ) #NEWLINE# #UNINDENT# ( as_list , as_dict ) = ( True , False ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( action == 'values_dict' ) : #NEWLINE# #INDENT# if ( not value ) : #NEWLINE# #INDENT# dict_fields = set ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# dict_fields |= set ( value ) #NEWLINE# #UNINDENT# ( as_list , as_dict ) = ( False , True ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( action == 'explain' ) : #NEWLINE# #INDENT# explain = value #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( action == 'query' ) : #NEWLINE# #INDENT# queries . append ( value ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( action == 'query_raw' ) : #NEWLINE# #INDENT# query_raw = value #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( action == 'demote' ) : #NEWLINE# #INDENT# demote = value #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( action == 'filter' ) : #NEWLINE# #INDENT# filters . extend ( self . _process_filters ( value ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( action == 'filter_raw' ) : #NEWLINE# #INDENT# filters_raw = value #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( action == 'facet' ) : #NEWLINE# #INDENT# facets . update ( _process_facets ( * value ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( action == 'facet_raw' ) : #NEWLINE# #INDENT# facets_raw . update ( dict ( value ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( action == 'highlight' ) : #NEWLINE# #INDENT# if ( value [ 0 ] == ( None , ) ) : #NEWLINE# #INDENT# highlight_fields = set ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# highlight_fields |= set ( value [ 0 ] ) #NEWLINE# #UNINDENT# highlight_options . update ( value [ 1 ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( action == 'search_type' ) : #NEWLINE# #INDENT# search_type = value #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( action == 'suggest' ) : #NEWLINE# #INDENT# suggestions [ value [ 0 ] ] = ( value [ 1 ] , value [ 2 ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( action in ( 'es' , 'indexes' , 'doctypes' , 'boost' ) ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise NotImplementedError ( action ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# qs = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# if filters_raw : #NEWLINE# #INDENT# qs [ 'filter' ] = filters_raw #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( len ( filters ) > 1 ) : #NEWLINE# #INDENT# qs [ 'filter' ] = { #NEWLINE# #INDENT# 'and' : filters , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if filters : #NEWLINE# #INDENT# qs [ 'filter' ] = filters [ 0 ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if query_raw : #NEWLINE# #INDENT# qs [ 'query' ] = query_raw #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# pq = self . _process_queries ( queries ) #NEWLINE# if ( demote is not None ) : #NEWLINE# #INDENT# qs [ 'query' ] = { #NEWLINE# #INDENT# 'boosting' : { #NEWLINE# #INDENT# 'negative' : self . _process_queries ( [ demote [ 1 ] ] ) , #NEWLINE# 'negative_boost' : demote [ 0 ] , #NEWLINE# #UNINDENT# } , #NEWLINE# #UNINDENT# } #NEWLINE# if pq : #NEWLINE# #INDENT# qs [ 'query' ] [ 'boosting' ] [ 'positive' ] = pq #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if pq : #NEWLINE# #INDENT# qs [ 'query' ] = pq #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if as_list : #NEWLINE# #INDENT# fields = qs [ 'fields' ] = ( list ( list_fields ) if list_fields else [ '*' ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if as_dict : #NEWLINE# #INDENT# fields = qs [ 'fields' ] = ( list ( dict_fields ) if dict_fields else [ '*' ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# fields = set ( ) #NEWLINE# #UNINDENT# #UNINDENT# if facets : #NEWLINE# #INDENT# qs [ 'facets' ] = facets #NEWLINE# for facet in facets . values ( ) : #NEWLINE# #INDENT# if ( ( facet . get ( 'facet_filter' , 1 ) is None ) and ( 'filter' in qs ) ) : #NEWLINE# #INDENT# facet [ 'facet_filter' ] = qs [ 'filter' ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if facets_raw : #NEWLINE# #INDENT# qs . setdefault ( 'facets' , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) . update ( facets_raw ) #NEWLINE# #UNINDENT# if sort : #NEWLINE# #INDENT# qs [ 'sort' ] = sort #NEWLINE# #UNINDENT# if self . start : #NEWLINE# #INDENT# qs [ 'from' ] = self . start #NEWLINE# #UNINDENT# if ( self . stop is not None ) : #NEWLINE# #INDENT# qs [ 'size' ] = ( self . stop - self . start ) #NEWLINE# #UNINDENT# if highlight_fields : #NEWLINE# #INDENT# qs [ 'highlight' ] = self . _build_highlight ( highlight_fields , highlight_options ) #NEWLINE# #UNINDENT# if list_fields : #NEWLINE# #INDENT# qs [ 'explain' ] = True #NEWLINE# #UNINDENT# for ( suggestion , ( term , kwargs ) ) in six . iteritems ( suggestions ) : #NEWLINE# #INDENT# qs . setdefault ( 'suggest' , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) [ suggestion ] = { #NEWLINE# #INDENT# 'text' : term , #NEWLINE# 'term' : { #NEWLINE# #INDENT# 'field' : kwargs . get ( 'field' , '_all' ) , #NEWLINE# #UNINDENT# } , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# ( self . fields , self . as_list , self . as_dict ) = ( fields , as_list , as_dict ) #NEWLINE# self . search_type = search_type #NEWLINE# return qs