Exemple #1
0
def search_entities(request):
  """
  For displaying results.
  """
  interface = request.POST.get('interface', 'navigator')
  query_s = json.loads(request.POST.get('query_s', ''))
  query_s = smart_str(query_s)

  offset = request.POST.get('offset', 0)
  limit = int(request.POST.get('limit', 100))
  raw_query = request.POST.get('raw_query', False)
  sources = json.loads(request.POST.get('sources') or '[]')
  if sources and not has_navigator_file_search(request.user):
    sources = ['sql']

  query_s = query_s.strip() or '*'

  api = get_api(request=request, interface=interface)

  entities = api.search_entities(query_s, limit=limit, offset=offset, raw_query=raw_query, sources=sources)

  if not raw_query:
    _augment_highlighting(query_s, entities)

  response = {
    'entities': entities,
    'count': len(entities),
    'offset': offset,
    'limit': limit,
    'query_s': query_s,
    'status': 0
  }

  return JsonResponse(response)
Exemple #2
0
def search_entities_interactive(request):
  """
  For search autocomplete.
  """
  interface = request.POST.get('interface', 'navigator')
  query_s = json.loads(request.POST.get('query_s', ''))
  prefix = request.POST.get('prefix')
  offset = request.POST.get('offset', 0)
  limit = int(request.POST.get('limit', 25))
  field_facets = json.loads(request.POST.get('field_facets') or '[]')
  sources = json.loads(request.POST.get('sources') or '[]')

  api = get_api(request=request, interface=interface)

  if sources and not has_navigator_file_search(request.user):
    sources = ['sql']

  response = api.search_entities_interactive(
      query_s=query_s,
      limit=limit,
      offset=offset,
      facetFields=field_facets,
      facetPrefix=prefix,
      facetRanges=None,
      firstClassEntitiesOnly=None,
      sources=sources
  )

  if response.get('facets'): # Remove empty facets
    for fname, fvalues in response['facets'].items():
      if NAVIGATOR.APPLY_SENTRY_PERMISSIONS.get():
        fvalues = []
      else:
        fvalues = sorted([(k, v) for k, v in fvalues.items() if v > 0], key=lambda n: n[1], reverse=True)
      response['facets'][fname] = OrderedDict(fvalues)
      if ':' in query_s and not response['facets'][fname]:
        del response['facets'][fname]


  _augment_highlighting(query_s, response.get('results'))

  response['status'] = 0

  return JsonResponse(response)
Exemple #3
0
def search_entities_interactive(request):
    """
  For search autocomplete.
  """
    api = NavigatorApi(request.user)

    query_s = json.loads(request.POST.get('query_s', ''))
    prefix = request.POST.get('prefix')
    offset = request.POST.get('offset', 0)
    limit = int(request.POST.get('limit', 25))
    field_facets = json.loads(request.POST.get('field_facets') or '[]')
    sources = json.loads(request.POST.get('sources') or '[]')

    if sources and not has_navigator_file_search(request.user):
        sources = ['sql']

    f = {
        "outputFormat": {
            "type": "dynamic"
        },
        "name": {
            "type": "dynamic"
        },
        "lastModified": {
            "type": "date"
        },
        "sourceType": {
            "type": "dynamic"
        },
        "parentPath": {
            "type": "dynamic"
        },
        "lastAccessed": {
            "type": "date"
        },
        "type": {
            "type": "dynamic"
        },
        "sourceId": {
            "type": "dynamic"
        },
        "partitionColNames": {
            "type": "dynamic"
        },
        "serDeName": {
            "type": "dynamic"
        },
        "created": {
            "type": "date"
        },
        "fileSystemPath": {
            "type": "dynamic"
        },
        "compressed": {
            "type": "bool"
        },
        "clusteredByColNames": {
            "type": "dynamic"
        },
        "originalName": {
            "type": "dynamic"
        },
        "owner": {
            "type": "dynamic"
        },
        "extractorRunId": {
            "type": "dynamic"
        },
        "userEntity": {
            "type": "bool"
        },
        "sortByColNames": {
            "type": "dynamic"
        },
        "inputFormat": {
            "type": "dynamic"
        },
        "serDeLibName": {
            "type": "dynamic"
        },
        "originalDescription": {
            "type": "dynamic"
        },
        "lastModifiedBy": {
            "type": "dynamic"
        }
    }

    auto_field_facets = ["tags", "type"] + f.keys()
    query_s = query_s.strip() + '*'

    last_query_term = [term for term in query_s.split()][-1]

    if last_query_term and last_query_term != '*':
        last_query_term = last_query_term.rstrip('*')
        (fname,
         fval) = last_query_term.split(':') if ':' in last_query_term else (
             last_query_term, '')
        auto_field_facets = [
            f for f in auto_field_facets if f.startswith(fname)
        ]

    response = api.search_entities_interactive(query_s=query_s,
                                               limit=limit,
                                               offset=offset,
                                               facetFields=field_facets
                                               or auto_field_facets[:5],
                                               facetPrefix=prefix,
                                               facetRanges=None,
                                               firstClassEntitiesOnly=None,
                                               sources=sources)

    if response.get('facets'):  # Remove empty facets
        for fname, fvalues in response['facets'].items():
            if NAVIGATOR.APPLY_SENTRY_PERMISSIONS.get():
                fvalues = []
            else:
                fvalues = sorted([(k, v) for k, v in fvalues.items() if v > 0],
                                 key=lambda n: n[1],
                                 reverse=True)
            response['facets'][fname] = OrderedDict(fvalues)
            if ':' in query_s and not response['facets'][fname]:
                del response['facets'][fname]

    _augment_highlighting(query_s, response.get('results'))

    response['status'] = 0

    return JsonResponse(response)