def get_query_from_more_like_this(request, doc_id, type, maxx=100):
    try:
        eprofile = EmifProfile.objects.get(user=request.user)
    except EmifProfile.DoesNotExist:
        print "-- ERROR: Couldn't get emif profile for user"
    if eprofile.restricted == True:
        query = restriction(request.user)

    c = CoreEngine()
    #results = c.search_fingerprint(query, sort=sort, rows=rows, start=start)
    results = c.more_like_this(doc_id, type, maxx=maxx)


    if len(results)>0:
        queryString = "id:("
        for r in results:
            if "id" in r:
                queryString = queryString + r["id"]+"^"+str(r["score"])+ " "

        queryString = queryString + ")"
    else:
        queryString = None

    ## PY SOLR IS STUPID, OTHERWISE THIS WOULD BE AVOIDED
    database_name = ""
    results = c.search_fingerprint("id:"+doc_id , start=0, rows=1, fl="database_name_t")
    for r in results:
        if "database_name_t" in r:
            database_name = r["database_name_t"]

    return (queryString, database_name)
示例#2
0
    def get(self, request, *args, **kw):
        self.__subscribed = []
        self.__mlt_fused = {}

        if request.user.is_authenticated():

            ordered = cache.get('recommendations_' + str(request.user.id))

            if ordered == None:
                maxx = 100
                subscriptions = FingerprintSubscription.active().filter(
                    user=request.user)

                # first we generate the list of already subscribed databases, since they wont appear on suggestions
                for subscription in subscriptions:
                    self.__subscribed.append(
                        subscription.fingerprint.fingerprint_hash)

                c = CoreEngine()
                for subscription in subscriptions:
                    fingerprint = subscription.fingerprint
                    this_mlt = c.more_like_this(fingerprint.fingerprint_hash,
                                                fingerprint.questionnaire.slug,
                                                maxx=maxx)

                    self.__merge(this_mlt)

                ordered = sorted(self.__mlt_fused.values(),
                                 reverse=True,
                                 key=lambda x: x['score'])[:10]

                for entry in ordered:
                    try:
                        fingerprint = Fingerprint.valid().get(
                            fingerprint_hash=entry['id'])
                    except Fingerprint.DoesNotExist:
                        continue

                    entry['name'] = fingerprint.findName()

                    entry[
                        'href'] = 'fingerprint/' + fingerprint.fingerprint_hash + '/1/'

                cache.set('recommendations_' + str(request.user.id), ordered,
                          720)

            response = Response({'mlt': ordered}, status=status.HTTP_200_OK)

        else:
            response = Response({}, status=status.HTTP_403_FORBIDDEN)

        return response
示例#3
0
文件: api.py 项目: bastiao/catalogue
    def get(self, request, *args, **kw):
        self.__subscribed = []
        self.__mlt_fused = {}

        if request.user.is_authenticated():

            ordered = cache.get('recommendations_'+str(request.user.id))

            if ordered == None:
                maxx = 100
                subscriptions = FingerprintSubscription.active().filter(user=request.user)

                # first we generate the list of already subscribed databases, since they wont appear on suggestions
                for subscription in subscriptions:
                    self.__subscribed.append(subscription.fingerprint.fingerprint_hash)

                c = CoreEngine()
                for subscription in subscriptions:
                    fingerprint = subscription.fingerprint
                    this_mlt = c.more_like_this(fingerprint.fingerprint_hash, fingerprint.questionnaire.slug, maxx=maxx)

                    self.__merge(this_mlt)

                ordered = sorted(self.__mlt_fused.values(), reverse=True, key=lambda x:x['score'])[:10]

                for entry in ordered:
                    try:
                        fingerprint = Fingerprint.valid().get(fingerprint_hash=entry['id'])
                    except Fingerprint.DoesNotExist:
                        continue

                    entry['name'] = fingerprint.findName()

                    entry['href'] = 'fingerprint/'+fingerprint.fingerprint_hash+'/1/'

                cache.set('recommendations_'+str(request.user.id), ordered, 720)


            response = Response({'mlt': ordered}, status=status.HTTP_200_OK)

        else:
            response = Response({}, status=status.HTTP_403_FORBIDDEN)

        return response