Exemple #1
0
def autocomplete(request):
  preferred_language = translation.get_language()
  if request.GET.get('q'):
    results = []
    q = request.GET['q']
    search = ep.ElasticSearch()
    search.size(10)
    labels_q = ep.ElasticQuery().wildcard('labels_orig',"*" + q + "*")
    labels_results = search.search_advanced('thesaurus','terms',labels_q)
    matching_uris = []
    for res in labels_results["hits"]["hits"]:
      matching_uris.append(res["_source"]["uri"])
          
    if len(matching_uris) < 2:
      alt_labels_q = ep.ElasticQuery().wildcard('alt_labels_orig',"*" + q + "*")
      alt_labels_results = search.search_advanced('thesaurus','terms',alt_labels_q)
      #matching_uris = []
      for res in alt_labels_results["hits"]["hits"]:
        matching_uris.append(res["_source"]["uri"])
        
    uris = set(matching_uris)
    for u in uris:
      results.append({'url':u, 'value':get_preferred_label(URIRef(u),preferred_language)})
      
  return HttpResponse(json.dumps(results), content_type='application/json')
 def es_map_query(self, query): 
     '''
     Maps an query request to an ElasticSearch query
     '''
     if not self.use_es:
         raise BadRequest('Can not make queries without ElasticSearch, enable in res/config/pyon.yml')
     if self.query_is_term_search(query):
         return ep.ElasticQuery().wildcard(field=query['field'],value=query['value'])
     if self.query_is_range_search(query):
         return ep.ElasticQuery().range(
             field      = query['field'],
             from_value = query['range']['from'],
             to_value   = query['range']['to']
         )
Exemple #3
0
def search(request):
  preferred_language = translation.get_language()
  if request.GET.get('q'):
    results = []
    q = request.GET['q']
    search = ep.ElasticSearch()
    search.size(8000)
    labels_q = ep.ElasticQuery().match('labels',q)
    labels_results = search.search_advanced('thesaurus','terms',labels_q)
    for res in labels_results["hits"]["hits"]:
      results.append({'pref_label': get_preferred_label(URIRef(res["_source"]["uri"]), preferred_language), 'uri':res["_source"]["uri"]})
      
    alt_labels_q = ep.ElasticQuery().match('alt_labels',q)
    alt_labels_results = search.search_advanced('thesaurus','terms',alt_labels_q)
    for res in alt_labels_results["hits"]["hits"]:
      pref_label = get_preferred_label(URIRef(res["_source"]["uri"]), preferred_language)
      to_append = {'pref_label': pref_label, 'uri':res["_source"]["uri"]}
      if not to_append in results:
        results.append(to_append)
    
    query = ep.ElasticQuery().match('_all',q)
    this_results = search.search_advanced('thesaurus','terms',query)
    #print(this_results["hits"]["hits"])

    for res in this_results["hits"]["hits"]:
      pref_label = get_preferred_label(URIRef(res["_source"]["uri"]), preferred_language)
      to_append = {'pref_label': pref_label, 'uri':res["_source"]["uri"]}
      if not to_append in results:
        results.append(to_append)
    try:
      page = request.GET.get('page',1)
    except PageNotAnInteger:
      page = 1

    p = Paginator(results, 20, request=request)
    paginated_results = p.page(page)
    return render(request, 'thesaurus/search.html', {'results': paginated_results })
    def query_geo_distance(self, source_id='', field='', origin=None, distance='', units='mi',order=None, limit=0, offset=0, id_only=False):
        validate_true(isinstance(origin,(tuple,list)) , 'Origin is not a list or tuple.')
        validate_true(len(origin)==2, 'Origin is not of the right size: (2)')

        if not self.use_es:
            raise BadRequest('Can not make queries without ElasticSearch, enable in res/config/pyon.yml')

        es = ep.ElasticSearch(host=self.elasticsearch_host, port=self.elasticsearch_port)
        source = self.clients.resource_registry.read(source_id)

        iterate = self._multi(self.query_range, source=source, field=field, origin=origin, distance=distance) 
        if iterate is not None:
            return iterate

        index = source
        validate_is_instance(index,ElasticSearchIndex, '%s does not refer to a valid index.' % index)

        sorts = ep.ElasticSort()
        if order is not None and isinstance(order,dict):
            field = order.keys()[0]
            value = order[field]
            sorts.sort(field,value)

        if limit:
            es.size(limit)

        if offset:
            es.from_offset(offset)

        if field == '*':
            field = '_all'


        sorts.geo_distance(field, origin, units)

        es.sorted(sorts)

        filter = ep.ElasticFilter()
        filter.geo_distance(field,origin, '%s%s' %(distance,units))

        es.filtered(filter)

        query = ep.ElasticQuery().match_all()

        response = IndexManagementService._es_call(es.search_index_advanced,index.index_name,query)
        IndexManagementService._check_response(response)

        return self._results_from_response(response,id_only)
    def test_query_range(self, mock_es):
        mock_index = ElasticSearchIndex(name='index', index_name='index')
        self.discovery.elasticsearch_host = 'fakehost'
        self.discovery.elasticsearch_port = 'fakeport'
        self.rr_read.return_value = mock_index
        hits = [{'_id':'a'},{'_id':'b'}]
        mock_es().search_index_advanced.return_value = {'hits':{'hits':hits}}

        retval = self.discovery.query_range('index_id','field',0,100,id_only=False)

        mock_es().search_index_advanced.assert_called_once_with('index',ep.ElasticQuery().range(field='field', from_value=0, to_value=100))
        retval.sort()
        self.assertTrue(retval==hits, '%s' % retval)
        
        retval = self.discovery.query_range('index_id','field',0,100,id_only=True)
        retval.sort()
        self.assertTrue(retval==['a','b'])
    def query_geo_bbox(self, source_id='', field='', top_left=None, bottom_right=None, order=None, limit=0, offset=0, id_only=False):
        validate_true(isinstance(top_left, (list,tuple)), 'Top Left is not a list or a tuple')
        validate_true(len(top_left)==2, 'Top Left is not of the right size: (2)')
        validate_true(isinstance(bottom_right, (list,tuple)), 'Bottom Right is not a list or a tuple')
        validate_true(len(bottom_right)==2, 'Bottom Right is not of the right size: (2)')

        if not self.use_es:
            raise BadRequest('Can not make queries without ElasticSearch, enable in res/config/pyon.yml')

        es = ep.ElasticSearch(host=self.elasticsearch_host, port=self.elasticsearch_port)
        source = self.clients.resource_registry.read(source_id)

        iterate = self._multi(self.query_range, source=source, field=field, top_left=top_left, bottom_right=bottom_right, order=order, limit=limit, offset=offset, id_only=id_only)
        if iterate is not None:
            return iterate

        index = source
        validate_is_instance(index,ElasticSearchIndex, '%s does not refer to a valid index.' % index)

        sorts = ep.ElasticSort()
        if order is not None and isinstance(order,dict):
            field = order.keys()[0]
            value = order[field]
            sorts.sort(field,value)

        if limit:
            es.size(limit)

        if offset:
            es.from_offset(offset)

        if field == '*':
            field = '_all'


        filter = ep.ElasticFilter()
        filter.geo_bounding_box(field, top_left, bottom_right)

        es.filtered(filter)

        query = ep.ElasticQuery().match_all()

        response = IndexManagementService._es_call(es.search_index_advanced,index.index_name,query)
        IndexManagementService._check_response(response)

        return self._results_from_response(response,id_only)
    def query_range(self, source_id='', field='', from_value=None, to_value=None, order=None, limit=0, offset=0, id_only=False):
        
        if not self.use_es:
            raise BadRequest('Can not make queries without ElasticSearch, enable in res/config/pyon.yml')

        validate_true(not from_value is None, 'from_value not specified')
        validate_true(isinstance(from_value,int) or isinstance(from_value,float), 'from_value is not a valid number')
        validate_true(not to_value is None, 'to_value not specified')
        validate_true(isinstance(to_value,int) or isinstance(to_value,float), 'to_value is not a valid number')
        validate_true(source_id, 'source_id not specified')

        es = ep.ElasticSearch(host=self.elasticsearch_host, port=self.elasticsearch_port)


        source = self.clients.resource_registry.read(source_id)

        #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
        # If source is a view, catalog or collection go through it and recursively call query_range on all the results in the indexes
        #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
        iterate = self._multi(self.query_range, source, field=field, from_value=from_value, to_value=to_value, order=order, limit=limit, offset=offset, id_only=id_only)
        if iterate is not None:
            return iterate

        index = source
        validate_is_instance(index,ElasticSearchIndex,'%s does not refer to a valid index.' % source_id)
        if order:
            validate_is_instance(order,dict,'Order is incorrect.')
            es.sort(**order)

        if limit:
            es.size(limit)

        if field == '*':
            field = '_all'

        query = ep.ElasticQuery().range(
            field      = field,
            from_value = from_value,
            to_value   = to_value
        )

        response = IndexManagementService._es_call(es.search_index_advanced,index.index_name,query)

        IndexManagementService._check_response(response)

        return self._results_from_response(response, id_only)
    def query_term(self, source_id='', field='', value='', order=None, limit=0, offset=0, id_only=False):
        '''
        Elasticsearch Query against an index
        > discovery.query_index('indexID', 'name', '*', order={'name':'asc'}, limit=20, id_only=False)
        '''
        if not self.use_es:
            raise BadRequest('Can not make queries without ElasticSearch, enable system.elasticsearch to make queries.')

        validate_true(source_id, 'Unspecified source_id')
        validate_true(field, 'Unspecified field')
        validate_true(value, 'Unspecified value')


        es = ep.ElasticSearch(host=self.elasticsearch_host, port=self.elasticsearch_port)

        source = self.clients.resource_registry.read(source_id)

        #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
        # If source is a view, catalog or collection go through it and recursively call query_range on all the results in the indexes
        #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
        iterate = self._multi(self.query_term, source, field=field, value=value, order=order, limit=limit, offset=offset, id_only=id_only)
        if iterate is not None:
            return iterate


        index = source
        validate_is_instance(index, ElasticSearchIndex, '%s does not refer to a valid index.' % index)
        if order: 
            validate_is_instance(order,dict, 'Order is incorrect.')
            es.sort(**order)

        if limit:
            es.size(limit)

        if offset:
            es.from_offset(offset)

        if field == '*':
            field = '_all'

        query = ep.ElasticQuery().wildcard(field=field, value=value)
        response = IndexManagementService._es_call(es.search_index_advanced,index.index_name,query)

        IndexManagementService._check_response(response)

        return self._results_from_response(response, id_only)
Exemple #9
0
def search(request):
    context = {}
    query = ep.ElasticQuery().query_string(query=request.POST['text'])

    sr = ep.ElasticSearch()
    p = sr.search_advanced('student', 'info', query)

    if len(p['hits']['hits']) > 0:
        stu_list = []
        p = sr.return_records('student', 'info', query)

        num = len(p)
        for obj in p:
            stu_list.append(obj['_source'])
            context = {'posts': stu_list, 'count': num}

    return render(request, 'search.html', context)
Exemple #10
0
 def test_query(self):
     query = ep.ElasticQuery().query_string(query='anm')
     search = ep.ElasticSearch()
     self.assertTrue(search.search_advanced('student', 'info', query))