Esempio n. 1
0
 def search_code_snippet(cls,
                         search_value,
                         start=0,
                         size=10,
                         request_type=None):
     if not isinstance(search_value, list):
         search_value = jieba.lcut_for_search(search_value)
     # print(search_value)
     if ElasticSearchSearvice.is_available():
         results = ElasticSearchSearvice.search_code_snippet(
             fields=['code_name', 'code_des', 'code_tags', 'code_source'],
             index='code_snippets',
             search_values=search_value,
             start=start,
             size=size)
         hits = results['hits']['hits']
         # print(hits)
         total = results['hits']['total']
         pre_process = []
         for hit in hits:
             source = hit['_source']
             search_id = hit['_id']
             # source['id'] = search_id
             source['code_from'] = search_id
             pre_process.append(source)
         # total, pre_process = cls.__single_table_search(results, 'code_snippets', search_value)
         return {'total': total, 'hits': pre_process}
     else:
         print('空的')
         return {'total': 0, 'hints': []}
Esempio n. 2
0
 def add_code_snippet(cls, code_snippet_id, code_name, code_des, code_tags,
                      code_source, detail_url, insert_num):
     if ElasticSearchSearvice.is_available():
         ElasticSearchSearvice.add_code_snippet(code_snippet_id, code_name,
                                                code_des, code_tags,
                                                code_source, detail_url,
                                                insert_num)
Esempio n. 3
0
    def add_project(cls, project_id, display_name, description, tags,
                    project_type, img_v, photo_url, username):

        if ElasticSearchSearvice.is_available():
            cls.project = ElasticSearchSearvice.add_project(
                project_id, display_name, description, tags, project_type,
                img_v, photo_url, username)
Esempio n. 4
0
    def search_tags(cls,
                    tags,
                    start=0,
                    size=10,
                    index='all',
                    project_type='app'):
        if not isinstance(tags, list):
            tags = tags.split(',')

        if ElasticSearchSearvice.is_available():
            if index == 'all':
                projects, requests = ElasticSearchSearvice.search_tags(
                    tags,
                    start=start,
                    size=size,
                    index=index,
                    project_type=project_type)

                project_total_num = projects['hits']['total']
                request_total_num = requests['hits']['total']

                projects = cls.__parse_search_tag(projects, 'project')

                requests = cls.__parse_search_tag(requests, 'request')

                back_data = {
                    'projects': {
                        'total': project_total_num,
                        'hits': projects
                    },
                    'requests': {
                        'total': request_total_num,
                        'hits': requests
                    }
                }
            else:
                requests = ElasticSearchSearvice.search_tags(
                    tags,
                    start=start,
                    size=size,
                    index=index,
                    project_type=project_type)
                request_total_num = requests['hits']['total']
                results = cls.__parse_search_tag(requests, index)
                back_data = {
                    'projects': {
                        'total': request_total_num,
                        'hits': {
                            project_type: results
                        }
                    }
                }
        else:
            back_data = {'projects': {'total': 0, 'hints': {}}}

        return back_data
Esempio n. 5
0
    def search_title(cls, values, start=0, size=3):
        selected, copy_values = cls.__split_with_advanced_search(values)
        # print("copy_valuescopy_values:", copy_values)
        copy_values = jieba.lcut_for_search(copy_values)
        # 处理后的结果
        processed_data = []

        if ElasticSearchSearvice.is_available():
            if selected != '':
                if len(copy_values) > 0:
                    results, count_info = ElasticSearchSearvice.search_title(
                        copy_values, index=selected)
                    total, pre_process = cls.__single_table_search(
                        results, selected, copy_values)
                    # total, data = cls.__single_table_search(results, selected, copy_values, is_title=True)
                    # 单表查询
                    processed_data.append(
                        {selected: {
                            'total': total,
                            'hits': pre_process
                        }})
                else:
                    # 没有输入就什么也不查询
                    results = {'message': 'no data'}
                    return results
            else:
                # 全局搜索
                # values.split(' ')
                wait_for_search_values = copy_values
                results, count_info = ElasticSearchSearvice.search_title(
                    search_value=wait_for_search_values)
                # 所有表查询结果
                for result in results:
                    # analysis = cls.__pre_process_results(result, wait_for_search_values, is_title=True)
                    # if analysis:
                    #     key, total, pre_process = analysis
                    selected = list(result.keys())[-1]
                    values = list(result.values())[-1]
                    total, pre_process = cls.__single_table_search(
                        values, selected, copy_values)
                    processed_data.append(
                        {selected: {
                            'total': total,
                            'hits': pre_process
                        }})
        else:
            processed_data = []
            count_info = {'apps': 0, 'modules': 0, 'datasets': 0}
        return processed_data, count_info
Esempio n. 6
0
    def search(cls,
               search_value,
               index='app',
               start=0,
               size=3,
               request_type=None):
        if not isinstance(search_value, list):
            search_value = jieba.lcut_for_search(search_value)
        if index in MAPPING:
            selected = index + 's'
        else:
            selected = index

        if ElasticSearchSearvice.is_available():
            results, count_info = ElasticSearchSearvice.search_fields(
                fields=FIELDS[index],
                index=selected,
                search_values=search_value,
                start=start,
                size=size,
                request_type=request_type)
            print(results, count_info)
            # print(results)
            # 数据处理
            total, pre_process = cls.__single_table_search(
                results, selected, search_value)
            # if 'request' in selected:
            #     apps, modules, datasets = cls.__split_request(pre_process)
            #     return {selected: {'total': total, 'hits': {'apps': apps, 'modules': modules, 'datasets': datasets}},
            #             'count': count_info}
            # if not count_info['project_nums'] and 'dataset' in selected:
            #     pre_process = []
            return {
                selected: {
                    'total': total,
                    'hits': pre_process
                },
                'count': count_info
            }
        return {selected: {'total': 0, 'hints': []}, 'count': {}}
Esempio n. 7
0
 def related_projects(cls, **args):
     if ElasticSearchSearvice.is_available():
         page_no = args.get('page_no')
         page_size = args.get('page_size')
         project_id = args.get('project_id')
         project_type = args.get('project_type')
         project = ProjectBusiness.get_by_id(project_id)
         tags = [tag.id for tag in project.tags]
         projects = []
         for tag in tags:
             ps = bus_type_mapper[project_type].repo.search(
                 tag, {'tags': 'icontains'})
             ps = ps(status='active')
             for p in ps:
                 if 'tutorial' not in p.display_name.lower():
                     projects.append(p)
             # projects.extend(ps)
         np.random.shuffle(projects)
         if not isinstance(projects, list):
             projects = projects.tolist()
         return projects[(page_no - 1) * page_size:page_no *
                         page_size], len(projects)
     return [], 0
Esempio n. 8
0
 def delete_code_snippet(cls, code_snippet_id):
     if ElasticSearchSearvice.is_available():
         ElasticSearchSearvice.delete_project(code_snippet_id)
Esempio n. 9
0
 def delete_project(cls, project_id):
     if ElasticSearchSearvice.is_available():
         ElasticSearchSearvice.delete_project(project_id)
Esempio n. 10
0
 def add_request(cls, request_title, description, request_type, username,
                 request_id):
     if ElasticSearchSearvice.is_available():
         ElasticSearchSearvice.add_request(request_title, description,
                                           request_type, username,
                                           request_id)
Esempio n. 11
0
 def add_user(cls, user_ID, username, bio, avatarV, avatar_url):
     if ElasticSearchSearvice.is_available():
         ElasticSearchSearvice.add_user(user_ID, username, bio, avatarV,
                                        avatar_url)
Esempio n. 12
0
 def delete_all(cls):
     ElasticSearchSearvice.delete_all()
Esempio n. 13
0
 def add_all(cls):
     ElasticSearchSearvice.add_all()
Esempio n. 14
0
 def delete_users(cls, user_ID):
     if ElasticSearchSearvice.is_available():
         ElasticSearchSearvice.delete_project(user_ID)
Esempio n. 15
0
 def refresh_all(cls):
     ElasticSearchSearvice.refresh_index()
Esempio n. 16
0
 def delete_request(cls, request_id):
     if ElasticSearchSearvice.is_available():
         ElasticSearchSearvice.delete_request(request_id)
Esempio n. 17
0
        if ElasticSearchSearvice.is_available():
            ElasticSearchSearvice.delete_project(user_ID)

    @classmethod
    def delete_request(cls, request_id):
        if ElasticSearchSearvice.is_available():
            ElasticSearchSearvice.delete_request(request_id)


# MARK: 单元测试
if __name__ == '__main__':
    # test_titles1 = 'chentiyun'
    # test_titles2 = 'module: g'
    # test_tags = ['video']
    # # 提取部分
    # import time
    # from pprint import pprint
    # start = time.time()
    # print(SearchService.search_title(test_titles1))
    # # SearchService.search_title(test_titles2)
    # print(SearchService.search(test_titles1))
    from pprint import pprint
    # pprint(SearchService.search_code_snippet('print'))
    # pprint(SearchService.search_tags(tags=test_tags, index='all'))
    # print('end time:', time.time() - start)
    ElasticSearchSearvice.delete_all()
    ElasticSearchSearvice.add_all()

    # 测试删除
    # SearchService.delete_project('5b83fc930c11f3302f4d07c6')