#NEWLINE# def generate_simple_search( search_form , language , with_highlights = False ) : #NEWLINE# #INDENT# 'Generates an S given a form\n\n    :arg search_form: a validated SimpleSearch form\n    :arg language: the language code\n    :arg with_highlights: whether or not to ask for highlights\n\n    :returns: a fully formed S\n\n    ' #NEWLINE# searcher = es_utils . AnalyzerS ( ) . es ( urls = settings . ES_URLS ) . indexes ( es_utils . read_index ( 'default' ) ) #NEWLINE# cleaned = search_form . cleaned_data #NEWLINE# doctypes = [ ] #NEWLINE# final_filter = es_utils . F ( ) #NEWLINE# cleaned_q = cleaned [ 'q' ] #NEWLINE# products = cleaned [ 'product' ] #NEWLINE# if ( cleaned [ 'w' ] & constants . WHERE_WIKI ) : #NEWLINE# #INDENT# wiki_f = es_utils . F ( model = 'wiki_document' , document_category__in = settings . SEARCH_DEFAULT_CATEGORIES , document_locale = language , document_is_archived = False ) #NEWLINE# for p in products : #NEWLINE# #INDENT# wiki_f &= es_utils . F ( product = p ) #NEWLINE# #UNINDENT# doctypes . append ( DocumentMappingType . get_mapping_type_name ( ) ) #NEWLINE# final_filter |= wiki_f #NEWLINE# #UNINDENT# if ( cleaned [ 'w' ] & constants . WHERE_SUPPORT ) : #NEWLINE# #INDENT# question_f = es_utils . F ( model = 'questions_question' , question_is_archived = False , question_has_helpful = True ) #NEWLINE# for p in products : #NEWLINE# #INDENT# question_f &= es_utils . F ( product = p ) #NEWLINE# #UNINDENT# doctypes . append ( QuestionMappingType . get_mapping_type_name ( ) ) #NEWLINE# final_filter |= question_f #NEWLINE# #UNINDENT# searcher = searcher . doctypes ( * doctypes ) #NEWLINE# searcher = searcher . filter ( final_filter ) #NEWLINE# if cleaned [ 'explain' ] : #NEWLINE# #INDENT# searcher = searcher . explain ( ) #NEWLINE# #UNINDENT# if with_highlights : #NEWLINE# #INDENT# searcher = searcher . highlight ( 'question_content' , 'document_summary' , pre_tags = [ '<b>' ] , post_tags = [ '</b>' ] , number_of_fragments = 0 ) #NEWLINE# #UNINDENT# searcher = apply_boosts ( searcher ) #NEWLINE# query_fields = chain ( * [ cls . get_query_fields ( ) for cls in [ DocumentMappingType , QuestionMappingType ] ] ) #NEWLINE# query = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for field in query_fields : #NEWLINE# #INDENT# for query_type in [ 'match' , 'match_phrase' ] : #NEWLINE# #INDENT# query [ ( '%s__%s' % ( field , query_type ) ) ] = cleaned_q #NEWLINE# #UNINDENT# #UNINDENT# query = es_utils . es_query_with_analyzer ( query , language ) #NEWLINE# searcher = searcher . query ( should = True , ** query ) #NEWLINE# return searcher