예제 #1
0
 def get(oid, request):
     """Returns document as OrderedDict of fields:values
     
     @param oid: str Object ID
     @param request: Django HttpRequest
     @returns: OrderedDict
     """
     document = docstore.Docstore().es.get(index=NAME_RECORD_DOCTYPE,
                                           id=oid)
     if not document:
         raise NotFound()
     data = format_name_record(document, request)
     HIDDEN_FIELDS = []
     for field in HIDDEN_FIELDS:
         pop_field(data, field)
     return data
예제 #2
0
def facets_list():
    """Returns a list of facets in alphabetical order, with URLs
    """
    key = FACETS_LIST_CACHE_KEY
    cached = cache.get(key)
    if not cached:
        facets_list = []
        ds = docstore.Docstore()
        facets = ds.get_facets()
        for name in facets:
            data = ds.get(model='facet', document_id=name)
            if not data:
                raise Exception(
                    '"%s" facet data missing--were facets indexed?' % name)
            f = data['_source']
            f['name'] = name
            f['url'] = reverse('ui-browse-facet', args=[name])
            facets_list.append(f)
        cached = facets_list
        cache.set(key, cached, settings.ELASTICSEARCH_FACETS_TIMEOUT)
    return cached
예제 #3
0
def facet_terms(facet):
    """
    If term is precoordinate all the terms are listed, with count of number of occurances (if any).
    If term is postcoordinate, all the terms come from the index, but there is not title/description.
    """
    facetterms = []
    ds = docstore.Docstore()
    results = ds.facet_terms(facet['name'], order='term')
    if facet.get('terms', []):
        # precoordinate
        # IMPORTANT: topics and facility term IDs are int. All others are str.
        term_counts = {}
        for t in results['terms']:
            term_id = extract_term_id(t['term'])
            term_count = t['count']
            if term_id and term_count:
                term_counts[term_id] = term_count
        # make URLs for terms
        for term in facet['terms']:
            term['url'] = reverse('ui-search-term-query',
                                  args=(facet['id'], term['id']))
        # add counts to terms
        for term in facet['terms']:
            term_id = term['id']
            if isinstance(term_id, int):
                term_id = str(term_id)
            term['count'] = term_counts.get(term_id, 0)
            facetterms.append(term)
    else:
        # postcoordinate
        for t in results['terms']:
            t['title'] = t['term']
            t['description'] = ''
            t['url'] = '/search/%s:%s/' % (facet['id'], t['term'])
            facetterms.append(t)
    return facetterms
예제 #4
0
from rest_framework.request import Request as RestRequest
from rest_framework.reverse import reverse

from django.conf import settings
from django.core.paginator import Paginator
from django.http.request import HttpRequest

#from DDR import vocab
from ui import docstore
#from ui import models

#SEARCH_LIST_FIELDS = models.all_list_fields()
DEFAULT_LIMIT = 1000

# set default hosts and index
DOCSTORE = docstore.Docstore()

# whitelist of params recognized in URL query
# TODO move to ddr-defs/repo_models/elastic.py?
SEARCH_PARAM_WHITELIST = [
    'fulltext',
    'sort',
    'topics',
    'facility',
    'model',
    'models',
    'parent',
    'status',
    'public',
    'topics',
    'facility',
예제 #5
0
from ui import docstore
from ui import search

# sorted version of facility and topics tree as choice fields
# {
#     'topics-choices': [
#         [u'topics-1', u'Immigration and citizenship'],
#         ...
#     ],
#     'facility-choices: [...],
# }

try:
    FORMS_CHOICES = docstore.Docstore().es.get(
        index='forms',
        id='forms-choices'
    )['_source']
except docstore.NotFoundError as err:
    t = type(err)
    raise Exception(f'{t} {err} (You probably need to run "ddrindex vocabs").')

# Pretty labels for multiple choice fields
# (After initial search the choice lists come from search aggs lists
# which only include IDs and doc counts.)
# {
#     'topics': {
#         '1': u'Immigration and citizenship',
#         ...
#     },
#     'facility: {...},
# }