<s> from pprint import pprint <EOL> from aleph . index import TYPE_RECORD <EOL> from aleph . search . util import add_filter , execute_basic <EOL> from aleph . search . fragments import text_query_string , match_all <EOL> def tabular_query ( document_id , sheet , args ) : <EOL> scored = False <EOL> q = match_all ( ) <EOL> text = args . get ( '<STR_LIT:q>' , '<STR_LIT>' ) . strip ( ) <EOL> if len ( text ) : <EOL> scored = True <EOL> q = text_query_string ( text ) <EOL> try : <EOL> rows = [ int ( r ) for r in args . getlist ( '<STR_LIT>' ) ] <EOL> except Exception : <EOL> rows = [ ] <EOL> if len ( rows ) : <EOL> scored = True <EOL> q = { <EOL> "<STR_LIT:bool>" : { <EOL> "<STR_LIT>" : q , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { '<STR_LIT>' : { '<STR_LIT>' : rows } } , <EOL> "<STR_LIT>" : <NUM_LIT:1000> <EOL> } <EOL> } <EOL> } <EOL> } <EOL> q = add_filter ( q , { '<STR_LIT>' : { '<STR_LIT>' : document_id } } ) <EOL> q = add_filter ( q , { '<STR_LIT>' : { '<STR_LIT>' : sheet } } ) <EOL> sort = [ { '<STR_LIT>' : '<STR_LIT>' } ] <EOL> if scored : <EOL> sort . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT:size>' : <NUM_LIT:100> , <EOL> '<STR_LIT>' : q , <EOL> '<STR_LIT>' : sort , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> } <EOL> def execute_tabular_query ( query ) : <EOL> """<STR_LIT>""" <EOL> result , hits , output = execute_basic ( TYPE_RECORD , query ) <EOL> for rec in hits . get ( '<STR_LIT>' , [ ] ) : <EOL> record = rec . get ( '<STR_LIT>' ) . get ( '<STR_LIT>' ) <EOL> record [ '<STR_LIT>' ] = rec . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' ) <EOL> output [ '<STR_LIT>' ] . append ( record ) <EOL> return output </s>
