class SearchData(): def __init__(self, ip='127.0.0.1', solr_core=SOLR_CORE_NAME): self.solr_url = 'http://' + ip + ':8999/solr' self.solr_core = solr_core self.solr = SOLR(self.solr_url) def search_answer(self, select='*:*', scene_topic=[]): try: fields = ['answers', 'emotion_url', 'media', 'timeout'] select_parts = [] for scene in scene_topic: s = 'scene_str:' + scene if scene_topic[scene] != []: s = '('+ s + ' AND (topic_str:'+ \ ' OR topic_str:'.join(scene_topic[scene]) +'))' select_parts.append(s) select = '(' + ' OR '.join(select_parts) + ') AND ' + select data = [ x for x in self.solr.query_solr(self.solr_core, select, fields, 1).docs ] data = data[0] return { 'answer': random.sample(data['answers'], 1)[0], 'emotion': data['emotion_url'][0], 'media': data['media'][0], 'timeout': data['timeout'][0] } except: traceback.print_exc() return { 'answer': None, 'emotion': None, 'media': None, 'timeout': None } def search_questions(self, select='*:*', scene_topic=[], fields=['question'], max_num=10): try: def pro_data(data): for key in data.keys(): data[key] = data[key][0] return data select_parts = [] for scene in scene_topic: s = 'scene_str:' + scene if scene_topic[scene] != []: s = '('+ s + ' AND (topic_str:'+ \ ' OR topic_str:'.join(scene_topic[scene]) +'))' select_parts.append(s) select = '(' + ' OR '.join(select_parts) + ') AND ' + select data = [ pro_data(x) for x in self.solr.query_solr( self.solr_core, select, fields, max_num).docs ] return data except: traceback.print_exc() return None def sale_id2description(self, _id, scene): try: select = 'scene_str:' + scene + ' AND _id_str:' + _id fields = ['description'] data = [ x for x in self.solr.query_solr(self.solr_core, select, fields, 1).docs ] data = data[0] return data['description'] except: traceback.print_exc() return None def sale_type2answers(self, scene, t=''): try: def pro_data(data): for key in data.keys(): data[key] = data[key][0] return data if t: select = 'scene_str:' + scene + ' AND type_str:' + t else: select = 'scene_str' + scene + ' AND type_str:*' fields = [ '_id', 'answers', 'type', 'emotion_url', 'media', 'timeout' ] max_num = 20 data = [ pro_data(x) for x in self.solr.query_solr( self.solr_core, select, fields, max_num).docs ] return data except: traceback.print_exc() return None
class SearchSolr(): def __init__(self, ip='127.0.0.1', solr_core=SOLR_CORE_NAME): self.solr_url = 'http://' + ip + ':8999/solr' self.solr_core = solr_core self.solr = SOLR(self.solr_url) def load_data(self, select='*:*', fields=[], max_num=10, flag=False): try: def pro_x(x): y = {} y['store_id'] = x['store_id'][0] y['category'] = x['category'][0] y['instruction'] = x['instruction'][0] if 'entities' in x: y['entities'] = x['entities'] else: y['entities'] = [''] y['answers'] = x['answer'] y['emotion_name'] = 'null' y['emotion_url'] = 'null' if 'media' in x: y['media'] = x['media'][0] y['timeout'] = '15' else: y['media'] = 'null' y['timeout'] = '0' return y Data = {} def pro_y(x): y = {} y['store_id'] = x['store_id'][0] y['category'] = x['category'][0] y['intent'] = x['intent'] y['questions'] = x['question'] if 'entities' in x: y['entities'] = x['entities'] else: y['entities'] = '' if y['intent'] + '|' + y['entities'] in Data: Data[y['intent'] + '|' + y['entities']]['questions'].append(x['question'][0]) else: Data[y['intent'] + '|' + y['entities']] = y return y if flag == True: data = [ pro_x(x) for x in self.solr.query_solr( self.solr_core, select, fields, max_num).docs ] else: data = [ pro_y(x) for x in self.solr.query_solr( self.solr_core, select, fields, max_num).docs ] data = [] for key in Data.keys(): data.append(Data[key]) return data except: traceback.print_exc() return None