예제 #1
0
파일: views.py 프로젝트: EuanCockburn/ifind
def search(request):
    """
    Accepts GET request containing query terms,
    searches and returns results as JSON.

    """
    # invalid HTTP method
    if request.method != 'GET':
        return HttpResponse(status=405)

    # checks input for validity
    query_terms = check_input(request.GET.get('q', ''))

    # bad request
    if not query_terms:
        return HttpResponse(status=400)

    # get exp ID and load experiment
    exp_id = request.session.get('exp_id', False)
    experiment = get_or_create_experiment(exp_id)

    # execute query
    engine = EngineFactory('bing', api_key="")
    query = Query(query_terms, top=experiment['top'], result_type="image")
    response = engine.search(query)

    return HttpResponse(response.to_json(), content_type='application/json')
예제 #2
0
def run_query(query):
    q = Query(query, top=50)
    e = EngineFactory("Bing", api_key=bing_api_key)

    response = e.search(q)

    return response
예제 #3
0
def run_query(query, condition):
    q = Query(query, top=100)

    # check cache, if query is there, return results
    # else send query to bing, and store the results in the cache
    response = bing_engine.search(q)
    mod = conditions[condition]
    mod_response = mod(response)

    return mod_response
예제 #4
0
파일: utils.py 프로젝트: EuanCockburn/ifind
def run_query(query, condition):
    """
    runs a search query on Bing using the query string passed,
    applies the relevant modifier function and returns the results

    :param query: (str)the query input by the user
    :param condition: (int)the interface condition applied to the user's profile
    :return: (Response)the results of the search after applying the correct modifier function
    """
    q = Query(query, top=100)

    response = e.search(q)

    mod = conditions[condition]
    mod_results = mod(response)

    return mod_results
예제 #5
0
def run_query(request,
              result_dict={},
              query_terms='',
              page=1,
              page_len=10,
              condition=0,
              log_performance=False):
    # Stops an AWFUL lot of problems when people get up to mischief
    if page < 1:
        page = 1

    ec = get_experiment_context(request)

    query = Query(query_terms)
    query.skip = page
    query.top = page_len

    result_dict['query'] = query_terms
    search_engine = experiment_setups[condition].get_engine()

    result_cache = True
    response = None

    if result_cache:
        if cache.cache.get(str(query)):
            response = cache.cache.get(str(query))
        else:
            response = search_engine.search(query)
            cache.cache.set(str(query), response, 500)
    else:
        response = search_engine.search(query)

    num_pages = response.total_pages

    result_dict['trec_results'] = None
    result_dict['trec_no_results_found'] = True
    result_dict['trec_search'] = False
    result_dict['num_pages'] = num_pages

    print "PAGE"
    print num_pages

    if num_pages > 0:
        result_dict['trec_search'] = True
        result_dict['trec_results'] = response.results

        result_dict['curr_page'] = response.actual_page
        if page > 1:
            result_dict['prev_page'] = page - 1
            result_dict['prev_page_show'] = True

            if (page - 1) == 1:
                result_dict[
                    'prev_page_link'] = "?query=" + query_terms.replace(
                        ' ', '+') + '&page=1&noperf=true'
            else:
                result_dict[
                    'prev_page_link'] = "?query=" + query_terms.replace(
                        ' ', '+') + '&page=' + str(page - 1)
        if page < num_pages:
            result_dict['next_page'] = page + 1
            result_dict['next_page_show'] = True
            result_dict['next_page_link'] = "?query=" + query_terms.replace(
                ' ', '+') + '&page=' + str(page + 1)

    # Disable performance logging - it's a hogging the performance!
    # If log_performance is True, we log the performance metrics.
    #if log_performance:
    #    log_event(event="QUERY_PERF",
    #              request=request,
    #              query=query_terms,
    #              metrics=get_query_performance_metrics(result_dict['trec_results'], ec['topicnum']))

    return result_dict
예제 #6
0
__author__ = 'Craig'
from ifind.search import Query, EngineFactory

q = Query("Google", top=5)
e = EngineFactory("Wikipedia")

print q
print e

response = e.search(q)

for r in response.results:
    print r