<s> from __future__ import absolute_import , division , print_function , unicode_literals <EOL> import warnings <EOL> from django . conf import settings <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . utils import six <EOL> from haystack . backends import BaseEngine , BaseSearchBackend , BaseSearchQuery , EmptyResults , log_query <EOL> from haystack . constants import DJANGO_CT , DJANGO_ID , ID <EOL> from haystack . exceptions import MissingDependency , MoreLikeThisError , SkipDocument <EOL> from haystack . inputs import Clean , Exact , PythonData , Raw <EOL> from haystack . models import SearchResult <EOL> from haystack . utils import log as logging <EOL> from haystack . utils import get_identifier , get_model_ct <EOL> from haystack . utils . app_loading import haystack_get_model <EOL> try : <EOL> from pysolr import Solr , SolrError <EOL> except ImportError : <EOL> raise MissingDependency ( "<STR_LIT>" ) <EOL> class SolrSearchBackend ( BaseSearchBackend ) : <EOL> RESERVED_WORDS = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> RESERVED_CHARACTERS = ( <EOL> '<STR_LIT:\\>' , '<STR_LIT:+>' , '<STR_LIT:->' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:!>' , '<STR_LIT:(>' , '<STR_LIT:)>' , '<STR_LIT:{>' , '<STR_LIT:}>' , <EOL> '<STR_LIT:[>' , '<STR_LIT:]>' , '<STR_LIT>' , '<STR_LIT:">' , '<STR_LIT>' , '<STR_LIT:*>' , '<STR_LIT:?>' , '<STR_LIT::>' , '<STR_LIT:/>' , <EOL> ) <EOL> def __init__ ( self , connection_alias , ** connection_options ) : <EOL> super ( SolrSearchBackend , self ) . __init__ ( connection_alias , ** connection_options ) <EOL> if not '<STR_LIT>' in connection_options : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" % connection_alias ) <EOL> self . conn = Solr ( connection_options [ '<STR_LIT>' ] , timeout = self . timeout , ** connection_options . get ( '<STR_LIT>' , { } ) ) <EOL> self . log = logging . getLogger ( '<STR_LIT>' ) <EOL> def update ( self , index , iterable , commit = True ) : <EOL> docs = [ ] <EOL> for obj in iterable : <EOL> try : <EOL> docs . append ( index . full_prepare ( obj ) ) <EOL> except SkipDocument : <EOL> self . log . debug ( u"<STR_LIT>" , obj ) <EOL> except UnicodeDecodeError : <EOL> if not self . silently_fail : <EOL> raise <EOL> self . log . error ( u"<STR_LIT>" , exc_info = True , <EOL> extra = { "<STR_LIT:data>" : { "<STR_LIT:index>" : index , <EOL> "<STR_LIT:object>" : get_identifier ( obj ) } } ) <EOL> if len ( docs ) > <NUM_LIT:0> : <EOL> try : <EOL> self . conn . add ( docs , commit = commit , boost = index . get_field_weights ( ) ) <EOL> except ( IOError , SolrError ) as e : <EOL> if not self . silently_fail : <EOL> raise <EOL> self . log . error ( "<STR_LIT>" , e , exc_info = True ) <EOL> def remove ( self , obj_or_string , commit = True ) : <EOL> solr_id = get_identifier ( obj_or_string ) <EOL> try : <EOL> kwargs = { <EOL> '<STR_LIT>' : commit , <EOL> '<STR_LIT:id>' : solr_id <EOL> } <EOL> self . conn . delete ( ** kwargs ) <EOL> except ( IOError , SolrError ) as e : <EOL> if not self . silently_fail : <EOL> raise <EOL> self . log . error ( "<STR_LIT>" , solr_id , e , exc_info = True ) <EOL> def clear ( self , models = None , commit = True ) : <EOL> if models is not None : <EOL> assert isinstance ( models , ( list , tuple ) ) <EOL> try : <EOL> if models is None : <EOL> self . conn . delete ( q = '<STR_LIT>' , commit = commit ) <EOL> else : <EOL> models_to_delete = [ ] <EOL> for model in models : <EOL> models_to_delete . append ( "<STR_LIT>" % ( DJANGO_CT , get_model_ct ( model ) ) ) <EOL> self . conn . delete ( q = "<STR_LIT>" . join ( models_to_delete ) , commit = commit ) <EOL> if commit : <EOL> self . conn . optimize ( ) <EOL> except ( IOError , SolrError ) as e : <EOL> if not self . silently_fail : <EOL> raise <EOL> if models is not None : <EOL> self . log . error ( "<STR_LIT>" , '<STR_LIT:U+002C>' . join ( models_to_delete ) , e , <EOL> exc_info = True ) <EOL> else : <EOL> self . log . error ( "<STR_LIT>" , e , exc_info = True ) <EOL> @ log_query <EOL> def search ( self , query_string , ** kwargs ) : <EOL> if len ( query_string ) == <NUM_LIT:0> : <EOL> return { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> } <EOL> search_kwargs = self . build_search_kwargs ( query_string , ** kwargs ) <EOL> try : <EOL> raw_results = self . conn . search ( query_string , ** search_kwargs ) <EOL> except ( IOError , SolrError ) as e : <EOL> if not self . silently_fail : <EOL> raise <EOL> self . log . error ( "<STR_LIT>" , query_string , e , exc_info = True ) <EOL> raw_results = EmptyResults ( ) <EOL> return self . _process_results ( raw_results , highlight = kwargs . get ( '<STR_LIT>' ) , result_class = kwargs . get ( '<STR_LIT>' , SearchResult ) , distance_point = kwargs . get ( '<STR_LIT>' ) ) <EOL> def build_search_kwargs ( self , query_string , sort_by = None , start_offset = <NUM_LIT:0> , end_offset = None , <EOL> fields = '<STR_LIT>' , highlight = False , facets = None , <EOL> date_facets = None , query_facets = None , <EOL> narrow_queries = None , spelling_query = None , <EOL> within = None , dwithin = None , distance_point = None , <EOL> models = None , limit_to_registered_models = None , <EOL> result_class = None , stats = None ) : <EOL> kwargs = { '<STR_LIT>' : '<STR_LIT>' } <EOL> if fields : <EOL> if isinstance ( fields , ( list , set ) ) : <EOL> fields = "<STR_LIT:U+0020>" . join ( fields ) <EOL> kwargs [ '<STR_LIT>' ] = fields <EOL> if sort_by is not None : <EOL> if sort_by in [ '<STR_LIT>' , '<STR_LIT>' ] and distance_point : <EOL> lng , lat = distance_point [ '<STR_LIT>' ] . get_coords ( ) <EOL> kwargs [ '<STR_LIT>' ] = distance_point [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' % ( lat , lng ) <EOL> if sort_by == '<STR_LIT>' : <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> if sort_by . startswith ( '<STR_LIT>' ) : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> kwargs [ '<STR_LIT>' ] = sort_by <EOL> if start_offset is not None : <EOL> kwargs [ '<STR_LIT:start>' ] = start_offset <EOL> if end_offset is not None : <EOL> kwargs [ '<STR_LIT>' ] = end_offset - start_offset <EOL> if highlight : <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT:true>' <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if isinstance ( highlight , dict ) : <EOL> kwargs . update ( highlight ) <EOL> if self . include_spelling is True : <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT:true>' <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT:true>' <EOL> kwargs [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> if spelling_query : <EOL> kwargs [ '<STR_LIT>' ] = spelling_query <EOL> if facets is not None : <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> kwargs [ '<STR_LIT>' ] = facets . keys ( ) <EOL> for facet_field , options in facets . items ( ) : <EOL> for key , value in options . items ( ) : <EOL> kwargs [ '<STR_LIT>' % ( facet_field , key ) ] = self . conn . _from_python ( value ) <EOL> if date_facets is not None : <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> kwargs [ '<STR_LIT>' ] = date_facets . keys ( ) <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT:none>' <EOL> for key , value in date_facets . items ( ) : <EOL> kwargs [ "<STR_LIT>" % key ] = self . conn . _from_python ( value . get ( '<STR_LIT>' ) ) <EOL> kwargs [ "<STR_LIT>" % key ] = self . conn . _from_python ( value . get ( '<STR_LIT>' ) ) <EOL> gap_by_string = value . get ( '<STR_LIT>' ) . upper ( ) <EOL> gap_string = "<STR_LIT>" % ( value . get ( '<STR_LIT>' ) , gap_by_string ) <EOL> if value . get ( '<STR_LIT>' ) != <NUM_LIT:1> : <EOL> gap_string += "<STR_LIT:S>" <EOL> kwargs [ "<STR_LIT>" % key ] = '<STR_LIT>' % ( gap_string , gap_by_string ) <EOL> if query_facets is not None : <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> kwargs [ '<STR_LIT>' ] = [ "<STR_LIT>" % ( field , value ) for field , value in query_facets ] <EOL> if limit_to_registered_models is None : <EOL> limit_to_registered_models = getattr ( settings , '<STR_LIT>' , True ) <EOL> if models and len ( models ) : <EOL> model_choices = sorted ( get_model_ct ( model ) for model in models ) <EOL> elif limit_to_registered_models : <EOL> model_choices = self . build_models_list ( ) <EOL> else : <EOL> model_choices = [ ] <EOL> if len ( model_choices ) > <NUM_LIT:0> : <EOL> if narrow_queries is None : <EOL> narrow_queries = set ( ) <EOL> narrow_queries . add ( '<STR_LIT>' % ( DJANGO_CT , '<STR_LIT>' . join ( model_choices ) ) ) <EOL> if narrow_queries is not None : <EOL> kwargs [ '<STR_LIT>' ] = list ( narrow_queries ) <EOL> if stats : <EOL> kwargs [ '<STR_LIT>' ] = "<STR_LIT:true>" <EOL> for k in stats . keys ( ) : <EOL> kwargs [ '<STR_LIT>' ] = k <EOL> for facet in stats [ k ] : <EOL> kwargs [ '<STR_LIT>' % k ] = facet <EOL> if within is not None : <EOL> from haystack . utils . geo import generate_bounding_box <EOL> kwargs . setdefault ( '<STR_LIT>' , [ ] ) <EOL> ( ( min_lat , min_lng ) , ( max_lat , max_lng ) ) = generate_bounding_box ( within [ '<STR_LIT>' ] , within [ '<STR_LIT>' ] ) <EOL> bbox = '<STR_LIT>' % ( within [ '<STR_LIT>' ] , min_lat , min_lng , max_lat , max_lng ) <EOL> kwargs [ '<STR_LIT>' ] . append ( bbox ) <EOL> if dwithin is not None : <EOL> kwargs . setdefault ( '<STR_LIT>' , [ ] ) <EOL> lng , lat = dwithin [ '<STR_LIT>' ] . get_coords ( ) <EOL> geofilt = '<STR_LIT>' % ( lat , lng , dwithin [ '<STR_LIT>' ] , dwithin [ '<STR_LIT>' ] . km ) <EOL> kwargs [ '<STR_LIT>' ] . append ( geofilt ) <EOL> if self . distance_available and distance_point : <EOL> pass <EOL> return kwargs <EOL> def more_like_this ( self , model_instance , additional_query_string = None , <EOL> start_offset = <NUM_LIT:0> , end_offset = None , models = None , <EOL> limit_to_registered_models = None , result_class = None , ** kwargs ) : <EOL> from haystack import connections <EOL> model_klass = model_instance . _meta . concrete_model <EOL> index = connections [ self . connection_alias ] . get_unified_index ( ) . get_index ( model_klass ) <EOL> field_name = index . get_content_field ( ) <EOL> params = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> if start_offset is not None : <EOL> params [ '<STR_LIT:start>' ] = start_offset <EOL> if end_offset is not None : <EOL> params [ '<STR_LIT>' ] = end_offset <EOL> narrow_queries = set ( ) <EOL> if limit_to_registered_models is None : <EOL> limit_to_registered_models = getattr ( settings , '<STR_LIT>' , True ) <EOL> if models and len ( models ) : <EOL> model_choices = sorted ( get_model_ct ( model ) for model in models ) <EOL> elif limit_to_registered_models : <EOL> model_choices = self . build_models_list ( ) <EOL> else : <EOL> model_choices = [ ] <EOL> if len ( model_choices ) > <NUM_LIT:0> : <EOL> if narrow_queries is None : <EOL> narrow_queries = set ( ) <EOL> narrow_queries . add ( '<STR_LIT>' % ( DJANGO_CT , '<STR_LIT>' . join ( model_choices ) ) ) <EOL> if additional_query_string : <EOL> narrow_queries . add ( additional_query_string ) <EOL> if narrow_queries : <EOL> params [ '<STR_LIT>' ] = list ( narrow_queries ) <EOL> query = "<STR_LIT>" % ( ID , get_identifier ( model_instance ) ) <EOL> try : <EOL> raw_results = self . conn . more_like_this ( query , field_name , ** params ) <EOL> except ( IOError , SolrError ) as e : <EOL> if not self . silently_fail : <EOL> raise <EOL> self . log . error ( "<STR_LIT>" , <EOL> query , e , exc_info = True ) <EOL> raw_results = EmptyResults ( ) <EOL> return self . _process_results ( raw_results , result_class = result_class ) <EOL> def _process_results ( self , raw_results , highlight = False , result_class = None , distance_point = None ) : <EOL> from haystack import connections <EOL> results = [ ] <EOL> hits = raw_results . hits <EOL> facets = { } <EOL> stats = { } <EOL> spelling_suggestion = None <EOL> if result_class is None : <EOL> result_class = SearchResult <EOL> if hasattr ( raw_results , '<STR_LIT>' ) : <EOL> stats = raw_results . stats . get ( '<STR_LIT>' , { } ) <EOL> if hasattr ( raw_results , '<STR_LIT>' ) : <EOL> facets = { <EOL> '<STR_LIT>' : raw_results . facets . get ( '<STR_LIT>' , { } ) , <EOL> '<STR_LIT>' : raw_results . facets . get ( '<STR_LIT>' , { } ) , <EOL> '<STR_LIT>' : raw_results . facets . get ( '<STR_LIT>' , { } ) , <EOL> } <EOL> for key in [ '<STR_LIT>' ] : <EOL> for facet_field in facets [ key ] : <EOL> facets [ key ] [ facet_field ] = list ( zip ( facets [ key ] [ facet_field ] [ : : <NUM_LIT:2> ] , facets [ key ] [ facet_field ] [ <NUM_LIT:1> : : <NUM_LIT:2> ] ) ) <EOL> if self . include_spelling is True : <EOL> if hasattr ( raw_results , '<STR_LIT>' ) : <EOL> if len ( raw_results . spellcheck . get ( '<STR_LIT>' , [ ] ) ) : <EOL> spelling_suggestion = raw_results . spellcheck . get ( '<STR_LIT>' ) [ - <NUM_LIT:1> ] <EOL> unified_index = connections [ self . connection_alias ] . get_unified_index ( ) <EOL> indexed_models = unified_index . get_indexed_models ( ) <EOL> for raw_result in raw_results . docs : <EOL> app_label , model_name = raw_result [ DJANGO_CT ] . split ( '<STR_LIT:.>' ) <EOL> additional_fields = { } <EOL> model = haystack_get_model ( app_label , model_name ) <EOL> if model and model in indexed_models : <EOL> index = unified_index . get_index ( model ) <EOL> index_field_map = index . field_map <EOL> for key , value in raw_result . items ( ) : <EOL> string_key = str ( key ) <EOL> if string_key in index_field_map : <EOL> string_key = index_field_map [ key ] <EOL> if string_key in index . fields and hasattr ( index . fields [ string_key ] , '<STR_LIT>' ) : <EOL> additional_fields [ string_key ] = index . fields [ string_key ] . convert ( value ) <EOL> else : <EOL> additional_fields [ string_key ] = self . conn . _to_python ( value ) <EOL> del ( additional_fields [ DJANGO_CT ] ) <EOL> del ( additional_fields [ DJANGO_ID ] ) <EOL> del ( additional_fields [ '<STR_LIT>' ] ) <EOL> if raw_result [ ID ] in getattr ( raw_results , '<STR_LIT>' , { } ) : <EOL> additional_fields [ '<STR_LIT>' ] = raw_results . highlighting [ raw_result [ ID ] ] <EOL> if distance_point : <EOL> additional_fields [ '<STR_LIT>' ] = distance_point <EOL> if raw_result . get ( '<STR_LIT>' ) : <EOL> from haystack . utils . geo import Distance <EOL> additional_fields [ '<STR_LIT>' ] = Distance ( km = float ( raw_result [ '<STR_LIT>' ] ) ) <EOL> else : <EOL> additional_fields [ '<STR_LIT>' ] = None <EOL> result = result_class ( app_label , model_name , raw_result [ DJANGO_ID ] , raw_result [ '<STR_LIT>' ] , ** additional_fields ) <EOL> results . append ( result ) <EOL> else : <EOL> hits -= <NUM_LIT:1> <EOL> return { <EOL> '<STR_LIT>' : results , <EOL> '<STR_LIT>' : hits , <EOL> '<STR_LIT>' : stats , <EOL> '<STR_LIT>' : facets , <EOL> '<STR_LIT>' : spelling_suggestion , <EOL> } <EOL> def build_schema ( self , fields ) : <EOL> content_field_name = '<STR_LIT>' <EOL> schema_fields = [ ] <EOL> for field_name , field_class in fields . items ( ) : <EOL> field_data = { <EOL> '<STR_LIT>' : field_class . index_fieldname , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:true>' , <EOL> '<STR_LIT>' : '<STR_LIT:true>' , <EOL> '<STR_LIT>' : '<STR_LIT:false>' , <EOL> } <EOL> if field_class . document is True : <EOL> content_field_name = field_class . index_fieldname <EOL> if field_class . field_type in [ '<STR_LIT:date>' , '<STR_LIT>' ] : <EOL> field_data [ '<STR_LIT:type>' ] = '<STR_LIT:date>' <EOL> elif field_class . field_type == '<STR_LIT>' : <EOL> field_data [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> elif field_class . field_type == '<STR_LIT:float>' : <EOL> field_data [ '<STR_LIT:type>' ] = '<STR_LIT:float>' <EOL> elif field_class . field_type == '<STR_LIT>' : <EOL> field_data [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> elif field_class . field_type == '<STR_LIT>' : <EOL> field_data [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> elif field_class . field_type == '<STR_LIT>' : <EOL> field_data [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> elif field_class . field_type == '<STR_LIT:location>' : <EOL> field_data [ '<STR_LIT:type>' ] = '<STR_LIT:location>' <EOL> if field_class . is_multivalued : <EOL> field_data [ '<STR_LIT>' ] = '<STR_LIT:true>' <EOL> if field_class . stored is False : <EOL> field_data [ '<STR_LIT>' ] = '<STR_LIT:false>' <EOL> if field_class . indexed is False : <EOL> field_data [ '<STR_LIT>' ] = '<STR_LIT:false>' <EOL> if field_data [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> field_data [ '<STR_LIT:type>' ] = '<STR_LIT:string>' <EOL> if hasattr ( field_class , '<STR_LIT>' ) : <EOL> if field_data [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> field_data [ '<STR_LIT:type>' ] = '<STR_LIT:string>' <EOL> schema_fields . append ( field_data ) <EOL> return ( content_field_name , schema_fields ) <EOL> def extract_file_contents ( self , file_obj ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . conn . extract ( file_obj ) <EOL> except Exception as e : <EOL> self . log . warning ( u"<STR_LIT>" , e , <EOL> exc_info = True , extra = { "<STR_LIT:data>" : { "<STR_LIT:file>" : file_obj } } ) <EOL> return None <EOL> class SolrSearchQuery ( BaseSearchQuery ) : <EOL> def matching_all_fragment ( self ) : <EOL> return '<STR_LIT>' <EOL> def build_query_fragment ( self , field , filter_type , value ) : <EOL> from haystack import connections <EOL> query_frag = '<STR_LIT>' <EOL> if not hasattr ( value , '<STR_LIT>' ) : <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> value = list ( value ) <EOL> if isinstance ( value , six . string_types ) : <EOL> value = Clean ( value ) <EOL> else : <EOL> value = PythonData ( value ) <EOL> prepared_value = value . prepare ( self ) <EOL> if not isinstance ( prepared_value , ( set , list , tuple ) ) : <EOL> prepared_value = self . backend . conn . _from_python ( prepared_value ) <EOL> if field == '<STR_LIT:content>' : <EOL> index_fieldname = '<STR_LIT>' <EOL> else : <EOL> index_fieldname = u'<STR_LIT>' % connections [ self . _using ] . get_unified_index ( ) . get_index_fieldname ( field ) <EOL> filter_types = { <EOL> '<STR_LIT>' : u'<STR_LIT:%s>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT:%s>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> } <EOL> if value . post_process is False : <EOL> query_frag = prepared_value <EOL> else : <EOL> if filter_type in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if value . input_type_name == '<STR_LIT>' : <EOL> query_frag = prepared_value <EOL> else : <EOL> terms = [ ] <EOL> for possible_value in prepared_value . split ( '<STR_LIT:U+0020>' ) : <EOL> terms . append ( filter_types [ filter_type ] % self . backend . conn . _from_python ( possible_value ) ) <EOL> if len ( terms ) == <NUM_LIT:1> : <EOL> query_frag = terms [ <NUM_LIT:0> ] <EOL> else : <EOL> query_frag = u"<STR_LIT>" % "<STR_LIT>" . join ( terms ) <EOL> elif filter_type == '<STR_LIT>' : <EOL> in_options = [ ] <EOL> if not prepared_value : <EOL> query_frag = u'<STR_LIT>' <EOL> else : <EOL> for possible_value in prepared_value : <EOL> in_options . append ( u'<STR_LIT>' % self . backend . conn . _from_python ( possible_value ) ) <EOL> query_frag = u"<STR_LIT>" % "<STR_LIT>" . join ( in_options ) <EOL> elif filter_type == '<STR_LIT>' : <EOL> start = self . backend . conn . _from_python ( prepared_value [ <NUM_LIT:0> ] ) <EOL> end = self . backend . conn . _from_python ( prepared_value [ <NUM_LIT:1> ] ) <EOL> query_frag = u'<STR_LIT>' % ( start , end ) <EOL> elif filter_type == '<STR_LIT>' : <EOL> if value . input_type_name == '<STR_LIT>' : <EOL> query_frag = prepared_value <EOL> else : <EOL> prepared_value = Exact ( prepared_value ) . prepare ( self ) <EOL> query_frag = filter_types [ filter_type ] % prepared_value <EOL> else : <EOL> if value . input_type_name != '<STR_LIT>' : <EOL> prepared_value = Exact ( prepared_value ) . prepare ( self ) <EOL> query_frag = filter_types [ filter_type ] % prepared_value <EOL> if len ( query_frag ) and not isinstance ( value , Raw ) : <EOL> if not query_frag . startswith ( '<STR_LIT:(>' ) and not query_frag . endswith ( '<STR_LIT:)>' ) : <EOL> query_frag = "<STR_LIT>" % query_frag <EOL> return u"<STR_LIT>" % ( index_fieldname , query_frag ) <EOL> def build_alt_parser_query ( self , parser_name , query_string = '<STR_LIT>' , ** kwargs ) : <EOL> if query_string : <EOL> query_string = Clean ( query_string ) . prepare ( self ) <EOL> kwarg_bits = [ ] <EOL> for key in sorted ( kwargs . keys ( ) ) : <EOL> if isinstance ( kwargs [ key ] , six . string_types ) and '<STR_LIT:U+0020>' in kwargs [ key ] : <EOL> kwarg_bits . append ( u"<STR_LIT>" % ( key , kwargs [ key ] ) ) <EOL> else : <EOL> kwarg_bits . append ( u"<STR_LIT>" % ( key , kwargs [ key ] ) ) <EOL> return u'<STR_LIT>' % ( parser_name , Clean ( '<STR_LIT:U+0020>' . join ( kwarg_bits ) ) , query_string ) <EOL> def build_params ( self , spelling_query = None , ** kwargs ) : <EOL> search_kwargs = { <EOL> '<STR_LIT>' : self . start_offset , <EOL> '<STR_LIT>' : self . result_class <EOL> } <EOL> order_by_list = None <EOL> if self . order_by : <EOL> if order_by_list is None : <EOL> order_by_list = [ ] <EOL> for order_by in self . order_by : <EOL> if order_by . startswith ( '<STR_LIT:->' ) : <EOL> order_by_list . append ( '<STR_LIT>' % order_by [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> order_by_list . append ( '<STR_LIT>' % order_by ) <EOL> search_kwargs [ '<STR_LIT>' ] = "<STR_LIT:U+002CU+0020>" . join ( order_by_list ) <EOL> if self . date_facets : <EOL> search_kwargs [ '<STR_LIT>' ] = self . date_facets <EOL> if self . distance_point : <EOL> search_kwargs [ '<STR_LIT>' ] = self . distance_point <EOL> if self . dwithin : <EOL> search_kwargs [ '<STR_LIT>' ] = self . dwithin <EOL> if self . end_offset is not None : <EOL> search_kwargs [ '<STR_LIT>' ] = self . end_offset <EOL> if self . facets : <EOL> search_kwargs [ '<STR_LIT>' ] = self . facets <EOL> if self . fields : <EOL> search_kwargs [ '<STR_LIT>' ] = self . fields <EOL> if self . highlight : <EOL> search_kwargs [ '<STR_LIT>' ] = self . highlight <EOL> if self . models : <EOL> search_kwargs [ '<STR_LIT>' ] = self . models <EOL> if self . narrow_queries : <EOL> search_kwargs [ '<STR_LIT>' ] = self . narrow_queries <EOL> if self . query_facets : <EOL> search_kwargs [ '<STR_LIT>' ] = self . query_facets <EOL> if self . within : <EOL> search_kwargs [ '<STR_LIT>' ] = self . within <EOL> if spelling_query : <EOL> search_kwargs [ '<STR_LIT>' ] = spelling_query <EOL> if self . stats : <EOL> search_kwargs [ '<STR_LIT>' ] = self . stats <EOL> return search_kwargs <EOL> def run ( self , spelling_query = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> final_query = self . build_query ( ) <EOL> search_kwargs = self . build_params ( spelling_query , ** kwargs ) <EOL> if kwargs : <EOL> search_kwargs . update ( kwargs ) <EOL> results = self . backend . search ( final_query , ** search_kwargs ) <EOL> self . _results = results . get ( '<STR_LIT>' , [ ] ) <EOL> self . _hit_count = results . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . _facet_counts = self . post_process_facets ( results ) <EOL> self . _stats = results . get ( '<STR_LIT>' , { } ) <EOL> self . _spelling_suggestion = results . get ( '<STR_LIT>' , None ) <EOL> def run_mlt ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . _more_like_this is False or self . _mlt_instance is None : <EOL> raise MoreLikeThisError ( "<STR_LIT>" ) <EOL> additional_query_string = self . build_query ( ) <EOL> search_kwargs = { <EOL> '<STR_LIT>' : self . start_offset , <EOL> '<STR_LIT>' : self . result_class , <EOL> '<STR_LIT>' : self . models <EOL> } <EOL> if self . end_offset is not None : <EOL> search_kwargs [ '<STR_LIT>' ] = self . end_offset - self . start_offset <EOL> results = self . backend . more_like_this ( self . _mlt_instance , additional_query_string , ** search_kwargs ) <EOL> self . _results = results . get ( '<STR_LIT>' , [ ] ) <EOL> self . _hit_count = results . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> class SolrEngine ( BaseEngine ) : <EOL> backend = SolrSearchBackend <EOL> query = SolrSearchQuery </s>
