def def_get(): start_time = time.time() args = args_parse(request) cr = copy.deepcopy(result) utils.logger.debug(' '.join(['Search Document', args['field'], args['words']])) def_indexer = get_definition_indexer('definition') def_search = Searcher(def_indexer) with def_search.open() as searcher: r = def_search.search(searcher, args['field'], args['words'], args['page_num'], args['page_size']) r.results.fragmenter = my_frag cr['data']['schema'] = ['title'] for h in r: hr = dict([(s, h[s].encode('utf-8') if type(h[s]) is unicode else str(h[s])) for s in cr['data']['schema'] if s in h]) with open(os.path.join(def_indexer.dir, h['path']), 'r') as file: hr['content'] = json.loads(file.read())['content'] hr['content'] = h.highlights('content', text=hr['content']) cr['data']['hits'].append(hr) # get definition score scores = definition_service(CONFIG['DEFINITION_SERVICE_URL'], [h['content'] for h in cr['data']['hits']]) if len(scores) != len(cr['data']['hits']): raise Exception('return %d scores for %d results' % (len(scores), len(cr['data']['hits']))) for i in range(len(cr['data']['hits'])): cr['data']['hits'][i]['score'] = scores[i] cr['data']['hits'] = sorted(cr['data']['hits'], key=lambda x:x['score'], reverse=True) cr['data']['schema'] = ['title', 'content', 'score'] cr['data']['info']['hits'] = r.results.scored_length() cr['data']['info']['time'] = time.time() - start_time return json.dumps(cr)
def sen_search(): args = args_parse(request) cr = copy.deepcopy(result) utils.logger.debug(' '.join(['Search Sentence', args['field'], args['words']])) sen_searcher = Searcher(get_eng_indexer(session['qq_openid'], 'sen')) with sen_searcher.open() as searcher: r = sen_searcher.search(searcher, args['field'], args['words'], args['page_num'], args['page_size']) cr['data']['schema'] = ['id', 'content', 'tags', 'categories', 'date', 'comments'] for h in r: cr['data']['hits'].append({s:h[s].encode('utf-8') if type(h[s]) is unicode else str(h[s]) for s in cr['data']['schema']}) return json.dumps(cr)
def iter_search(index_type, field, words): search = Searcher(get_eng_indexer(session['qq_openid'], index_type)) with search.open() as searcher: pn = 1 while True: r = search.search(searcher, field, words, pn, DEFAULT_PAGE_SIZE) count = 0 for h in r: count += 1 yield h if count < DEFAULT_PAGE_SIZE: break pn += 1
def doc_search(): args = args_parse(request) cr = copy.deepcopy(result) utils.logger.debug(' '.join(['Search Document', args['field'], args['words']])) doc_search = Searcher(get_eng_indexer(session['qq_openid'], 'doc')) with doc_search.open() as searcher: r = doc_search.search(searcher, args['field'], args['words'], args['page_num'], args['page_size']) cr['data']['schema'] = ['id', 'title', 'comments', 'categories', 'tags', 'date'] for h in r: hr = {s:h[s].encode('utf-8') if type(h[s]) is unicode else str(h[s]) for s in cr['data']['schema']} with open(h['path'], 'r') as file: hr['content'] = json.loads(file.read())['content'] cr['data']['hits'].append(hr) cr['data']['schema'].append('content') return json.dumps(cr)