def search(request): project_slug = request.GET.get('project', None) version_slug = request.GET.get('version', LATEST) query = request.GET.get('q', None) if project_slug is None or query is None: return Response({'error': 'Need project and q'}, status=status.HTTP_400_BAD_REQUEST) log.debug("(API Search) %s", query) results = search_file(request=request, project_slug=project_slug, version_slug=version_slug, query=query) return Response({'results': results})
def search(request): """Perform search, supplement links by resolving project domains.""" project_slug = request.GET.get('project', None) version_slug = request.GET.get('version', LATEST) query = request.GET.get('q', None) if project_slug is None or query is None: return Response( {'error': 'Need project and q'}, status=status.HTTP_400_BAD_REQUEST, ) try: project = Project.objects.get(slug=project_slug) except Project.DoesNotExist: return Response( {'error': 'Project not found'}, status=status.HTTP_404_NOT_FOUND, ) log.debug('(API Search) %s', query) results = search_file( request=request, project_slug=project_slug, version_slug=version_slug, query=query, ) if results is None: return Response( {'error': 'Project not found'}, status=status.HTTP_404_NOT_FOUND, ) # Supplement result paths with domain information on project hits = results.get('hits', {}).get('hits', []) for (n, hit) in enumerate(hits): fields = hit.get('fields', {}) search_project = fields.get('project')[0] search_version = fields.get('version')[0] path = fields.get('path')[0] canonical_url = project.get_docs_url(version_slug=version_slug) if search_project != project_slug: try: subproject = project.subprojects.get( child__slug=search_project) canonical_url = subproject.child.get_docs_url( version_slug=search_version, ) except ProjectRelationship.DoesNotExist: pass results['hits']['hits'][n]['fields']['link'] = (canonical_url + path) return Response({'results': results})
def search(request): """Perform search, supplement links by resolving project domains.""" project_slug = request.GET.get('project', None) version_slug = request.GET.get('version', LATEST) query = request.GET.get('q', None) if project_slug is None or query is None: return Response({'error': 'Need project and q'}, status=status.HTTP_400_BAD_REQUEST) try: project = Project.objects.get(slug=project_slug) except Project.DoesNotExist: return Response({'error': 'Project not found'}, status=status.HTTP_404_NOT_FOUND) log.debug("(API Search) %s", query) results = search_file(request=request, project_slug=project_slug, version_slug=version_slug, query=query) if results is None: return Response({'error': 'Project not found'}, status=status.HTTP_404_NOT_FOUND) # Supplement result paths with domain information on project hits = results.get('hits', {}).get('hits', []) for (n, hit) in enumerate(hits): fields = hit.get('fields', {}) search_project = fields.get('project')[0] search_version = fields.get('version')[0] path = fields.get('path')[0] canonical_url = project.get_docs_url(version_slug=version_slug) if search_project != project_slug: try: subproject = project.subprojects.get(child__slug=search_project) canonical_url = subproject.child.get_docs_url( version_slug=search_version ) except ProjectRelationship.DoesNotExist: pass results['hits']['hits'][n]['fields']['link'] = ( canonical_url + path ) return Response({'results': results})
def elastic_search(request): """Use Elasticsearch for global search.""" user_input = UserInput( query=request.GET.get('q'), type=request.GET.get('type', 'project'), project=request.GET.get('project'), version=request.GET.get('version', LATEST), taxonomy=request.GET.get('taxonomy'), language=request.GET.get('language'), ) results = '' facets = {} if user_input.query: if user_input.type == 'project': results = search_lib.search_project( request, user_input.query, language=user_input.language) elif user_input.type == 'file': results = search_lib.search_file( request, user_input.query, project_slug=user_input.project, version_slug=user_input.version, taxonomy=user_input.taxonomy) if results: # pre and post 1.0 compat for num, hit in enumerate(results['hits']['hits']): for key, val in list(hit['fields'].items()): if isinstance(val, list): results['hits']['hits'][num]['fields'][key] = val[0] if 'facets' in results: for facet_type in ['project', 'version', 'taxonomy', 'language']: if facet_type in results['facets']: facets[facet_type] = collections.OrderedDict() for term in results['facets'][facet_type]['terms']: facets[facet_type][term['term']] = term['count'] if settings.DEBUG: print(pprint(results)) print(pprint(facets)) if user_input.query: user = '' if request.user.is_authenticated(): user = request.user log.info( LOG_TEMPLATE.format( user=user, project=user_input.project or '', type=user_input.type or '', version=user_input.version or '', language=user_input.language or '', msg=user_input.query or '', )) template_vars = user_input._asdict() template_vars.update({ 'results': results, 'facets': facets, }) return render( request, 'search/elastic_search.html', template_vars, )
def elastic_search(request): """Use Elasticsearch for global search.""" user_input = UserInput( query=request.GET.get('q'), type=request.GET.get('type', 'project'), project=request.GET.get('project'), version=request.GET.get('version', LATEST), taxonomy=request.GET.get('taxonomy'), language=request.GET.get('language'), ) results = '' facets = {} if user_input.query: if user_input.type == 'project': results = search_lib.search_project(request, user_input.query, language=user_input.language) elif user_input.type == 'file': results = search_lib.search_file(request, user_input.query, project_slug=user_input.project, version_slug=user_input.version, taxonomy=user_input.taxonomy) if results: # pre and post 1.0 compat for num, hit in enumerate(results['hits']['hits']): for key, val in list(hit['fields'].items()): if isinstance(val, list): results['hits']['hits'][num]['fields'][key] = val[0] if 'facets' in results: for facet_type in ['project', 'version', 'taxonomy', 'language']: if facet_type in results['facets']: facets[facet_type] = collections.OrderedDict() for term in results['facets'][facet_type]['terms']: facets[facet_type][term['term']] = term['count'] if settings.DEBUG: print(pprint(results)) print(pprint(facets)) if user_input.query: user = '' if request.user.is_authenticated(): user = request.user log.info( LOG_TEMPLATE.format( user=user, project=user_input.project or '', type=user_input.type or '', version=user_input.version or '', language=user_input.language or '', msg=user_input.query or '', )) template_vars = user_input._asdict() template_vars.update({ 'results': results, 'facets': facets, }) return render( request, 'search/elastic_search.html', template_vars, )
def elastic_search(request): """ Use elastic search for global search """ query = request.GET.get('q') type = request.GET.get('type', 'project') # File Facets project = request.GET.get('project') version = request.GET.get('version', LATEST) taxonomy = request.GET.get('taxonomy') language = request.GET.get('language') results = "" facets = {} if query: if type == 'project': results = search_lib.search_project(request, query, language=language) elif type == 'file': results = search_lib.search_file(request, query, project_slug=project, version_slug=version, taxonomy=taxonomy) if results: # pre and post 1.0 compat for num, hit in enumerate(results['hits']['hits']): for key, val in hit['fields'].items(): if isinstance(val, list): results['hits']['hits'][num]['fields'][key] = val[0] if 'facets' in results: for facet_type in ['project', 'version', 'taxonomy', 'language']: if facet_type in results['facets']: facets[facet_type] = collections.OrderedDict() for term in results['facets'][facet_type]['terms']: facets[facet_type][term['term']] = term['count'] if settings.DEBUG: print pprint(results) print pprint(facets) if query: user = '' if request.user.is_authenticated(): user = request.user log.info(LOG_TEMPLATE.format( user=user, project=project or '', type=type or '', version=version or '', language=language or '', msg=query or '', )) return render_to_response( 'search/elastic_search.html', { # Input 'query': query, 'type': type, 'project': project, 'version': version, 'taxonomy': taxonomy, 'language': language, # Results 'results': results, 'facets': facets, }, context_instance=RequestContext(request), )
def elastic_search(request): """ Use elastic search for global search """ query = request.GET.get('q') type = request.GET.get('type', 'project') # File Facets project = request.GET.get('project') version = request.GET.get('version', LATEST) taxonomy = request.GET.get('taxonomy') language = request.GET.get('language') results = "" facets = {} if query: if type == 'project': results = search_lib.search_project(request, query, language=language) elif type == 'file': results = search_lib.search_file(request, query, project_slug=project, version_slug=version, taxonomy=taxonomy) if results: # pre and post 1.0 compat for num, hit in enumerate(results['hits']['hits']): for key, val in hit['fields'].items(): if isinstance(val, list): results['hits']['hits'][num]['fields'][key] = val[0] if 'facets' in results: for facet_type in ['project', 'version', 'taxonomy', 'language']: if facet_type in results['facets']: facets[facet_type] = collections.OrderedDict() for term in results['facets'][facet_type]['terms']: facets[facet_type][term['term']] = term['count'] if settings.DEBUG: print pprint(results) print pprint(facets) if query: user = '' if request.user.is_authenticated(): user = request.user log.info( LOG_TEMPLATE.format( user=user, project=project or '', type=type or '', version=version or '', language=language or '', msg=query or '', )) return render_to_response( 'search/elastic_search.html', { # Input 'query': query, 'type': type, 'project': project, 'version': version, 'taxonomy': taxonomy, 'language': language, # Results 'results': results, 'facets': facets, }, context_instance=RequestContext(request), )