예제 #1
0
def run_solr_query(param = {}, rows=100, page=1, sort=None, spellcheck_count=None):
    # called by do_search
    if spellcheck_count == None:
        spellcheck_count = default_spellcheck_count
    offset = rows * (page - 1)

    (q_list, use_dismax) = build_q_list(param)

    fields = ['key', 'author_name', 'author_key', 'title', 'subtitle', 'edition_count', 'ia', 'has_fulltext', 'first_publish_year', 'cover_edition_key', 'public_scan_b', 'lending_edition_s', 'overdrive_s', 'ia_collection_s']
    fl = ','.join(fields)
    if use_dismax:
        q = web.urlquote(' '.join(q_list))
        solr_select = solr_select_url + "?defType=dismax&q.op=AND&q=%s&qf=text+title^5+author_name^5&bf=sqrt(edition_count)^10&start=%d&rows=%d&fl=%s&wt=standard" % (q, offset, rows, fl)
    else:
        q = web.urlquote(' '.join(q_list + ['_val_:"sqrt(edition_count)"^10']))
        solr_select = solr_select_url + "?q.op=AND&q=%s&start=%d&rows=%d&fl=%s&wt=standard" % (q, offset, rows, fl)
    solr_select += '&spellcheck=true&spellcheck.count=%d' % spellcheck_count
    solr_select += "&facet=true&" + '&'.join("facet.field=" + f for f in facet_fields)

    if 'public_scan' in param:
        v = param.pop('public_scan').lower()
        if v in ('true', 'false'):
            if v == 'false':
                # also constrain on print disabled since the index may not be in sync
                param.setdefault('print_disabled', 'false')
            solr_select += '&fq=public_scan_b:%s' % v

    if 'print_disabled' in param:
        v = param.pop('print_disabled').lower()
        if v in ('true', 'false'):
            solr_select += '&fq=%ssubject_key:protected_daisy' % ('-' if v == 'false' else '')

    k = 'has_fulltext'
    if k in param:
        v = param[k].lower()
        if v not in ('true', 'false'):
            del param[k]
        param[k] == v
        solr_select += '&fq=%s:%s' % (k, v)

    for k in facet_list_fields:
        if k == 'author_facet':
            k = 'author_key'
        if k not in param:
            continue
        v = param[k]
        solr_select += ''.join('&fq=%s:"%s"' % (k, url_quote(l)) for l in v if l)
    if sort:
        solr_select += "&sort=" + url_quote(sort)

    stats.begin("solr", url=solr_select)
    reply = urllib.urlopen(solr_select).read()
    stats.end()
    return (reply, solr_select, q_list)
예제 #2
0
def run_solr_query(param={},
                   rows=100,
                   page=1,
                   sort=None,
                   spellcheck_count=None,
                   offset=None,
                   fields=None):
    # called by do_search
    if spellcheck_count == None:
        spellcheck_count = default_spellcheck_count

    # use page when offset is not specified
    if offset is None:
        offset = rows * (page - 1)

    (q_list, use_dismax) = build_q_list(param)

    if fields is None:
        fields = [
            'key', 'author_name', 'author_key', 'title', 'subtitle',
            'edition_count', 'ia', 'has_fulltext', 'first_publish_year',
            'cover_i', 'cover_edition_key', 'public_scan_b',
            'lending_edition_s', 'ia_collection_s'
        ]
    fl = ','.join(fields)
    solr_select = solr_select_url + "?q.op=AND&start=%d&rows=%d&fl=%s" % (
        offset, rows, fl)
    if q_list:
        if use_dismax:
            q = web.urlquote(' '.join(q_list))
            solr_select += "&defType=dismax&qf=text+title^5+author_name^5&bf=sqrt(edition_count)^10"
        else:
            q = web.urlquote(' '.join(q_list +
                                      ['_val_:"sqrt(edition_count)"^10']))
        solr_select += "&q=%s" % q
    solr_select += '&spellcheck=true&spellcheck.count=%d' % spellcheck_count
    solr_select += "&facet=true&" + '&'.join("facet.field=" + f
                                             for f in facet_fields)

    if 'public_scan' in param:
        v = param.pop('public_scan').lower()
        if v in ('true', 'false'):
            if v == 'false':
                # also constrain on print disabled since the index may not be in sync
                param.setdefault('print_disabled', 'false')
            solr_select += '&fq=public_scan_b:%s' % v

    if 'print_disabled' in param:
        v = param.pop('print_disabled').lower()
        if v in ('true', 'false'):
            solr_select += '&fq=%ssubject_key:protected_daisy' % (
                '-' if v == 'false' else '')

    k = 'has_fulltext'
    if k in param:
        v = param[k].lower()
        if v not in ('true', 'false'):
            del param[k]
        param[k] == v
        solr_select += '&fq=%s:%s' % (k, v)

    for k in facet_list_fields:
        if k == 'author_facet':
            k = 'author_key'
        if k not in param:
            continue
        v = param[k]
        solr_select += ''.join('&fq=%s:"%s"' % (k, url_quote(l)) for l in v
                               if l)
    if sort:
        solr_select += "&sort=" + url_quote(sort)

    solr_select += '&wt=' + url_quote(param.get('wt', 'standard'))

    # For single-core solr, filter the results by type:work
    if config.get("single_core_solr"):
        solr_select += "&fq=type:work"

    solr_result = execute_solr_query(solr_select)
    if solr_result is None:
        return (None, None, None)
    reply = solr_result.read()
    return (reply, solr_select, q_list)
예제 #3
0
def test_url_quote():
    assert url_quote('x') == 'x'
    result = url_quote(u'£20')
    assert result == '%C2%A320'
    assert url_quote('test string') == 'test+string'
예제 #4
0
def test_url_quote():
    assert url_quote("x") == "x"
    result = url_quote(u"£20")
    assert result == "%C2%A320"
    assert url_quote("test string") == "test+string"
예제 #5
0
def test_url_quote():
    assert url_quote('x') == 'x'
    result = url_quote(u'£20') 
    assert result == '%C2%A320'
    assert url_quote('test string') == 'test+string'
예제 #6
0
def run_solr_query(param={},
                   rows=100,
                   page=1,
                   sort=None,
                   spellcheck_count=None):
    # called by do_search
    if spellcheck_count == None:
        spellcheck_count = default_spellcheck_count
    offset = rows * (page - 1)

    (q_list, use_dismax) = build_q_list(param)

    fields = [
        'key', 'author_name', 'author_key', 'title', 'subtitle',
        'edition_count', 'ia', 'has_fulltext', 'first_publish_year',
        'cover_edition_key', 'public_scan_b', 'lending_edition_s',
        'overdrive_s', 'ia_collection_s'
    ]
    fl = ','.join(fields)
    if use_dismax:
        q = web.urlquote(' '.join(q_list))
        solr_select = solr_select_url + "?defType=dismax&q.op=AND&q=%s&qf=text+title^5+author_name^5&bf=sqrt(edition_count)^10&start=%d&rows=%d&fl=%s&wt=standard" % (
            q, offset, rows, fl)
    else:
        q = web.urlquote(' '.join(q_list + ['_val_:"sqrt(edition_count)"^10']))
        solr_select = solr_select_url + "?q.op=AND&q=%s&start=%d&rows=%d&fl=%s&wt=standard" % (
            q, offset, rows, fl)
    solr_select += '&spellcheck=true&spellcheck.count=%d' % spellcheck_count
    solr_select += "&facet=true&" + '&'.join("facet.field=" + f
                                             for f in facet_fields)

    if 'public_scan' in param:
        v = param.pop('public_scan').lower()
        if v in ('true', 'false'):
            if v == 'false':
                # also constrain on print disabled since the index may not be in sync
                param.setdefault('print_disabled', 'false')
            solr_select += '&fq=public_scan_b:%s' % v

    if 'print_disabled' in param:
        v = param.pop('print_disabled').lower()
        if v in ('true', 'false'):
            solr_select += '&fq=%ssubject_key:protected_daisy' % (
                '-' if v == 'false' else '')

    k = 'has_fulltext'
    if k in param:
        v = param[k].lower()
        if v not in ('true', 'false'):
            del param[k]
        param[k] == v
        solr_select += '&fq=%s:%s' % (k, v)

    for k in facet_list_fields:
        if k == 'author_facet':
            k = 'author_key'
        if k not in param:
            continue
        v = param[k]
        solr_select += ''.join('&fq=%s:"%s"' % (k, url_quote(l)) for l in v
                               if l)
    if sort:
        solr_select += "&sort=" + url_quote(sort)

    stats.begin("solr", url=solr_select)
    reply = urllib.urlopen(solr_select).read()
    stats.end()
    return (reply, solr_select, q_list)
예제 #7
0
def run_solr_query(param = {}, rows=100, page=1, sort=None, spellcheck_count=None, offset=None, fields=None):
    # called by do_search
    if spellcheck_count == None:
        spellcheck_count = default_spellcheck_count

    # use page when offset is not specified
    if offset is None:
        offset = rows * (page - 1)

    (q_list, use_dismax) = build_q_list(param)

    if fields is None:
        fields = [
            'key', 'author_name', 'author_key',
            'title', 'subtitle', 'edition_count',
            'ia', 'has_fulltext', 'first_publish_year',
            'cover_i','cover_edition_key', 'public_scan_b',
            'lending_edition_s', 'ia_collection_s']
    fl = ','.join(fields)
    solr_select = solr_select_url + "?q.op=AND&start=%d&rows=%d&fl=%s" % (offset, rows, fl)
    if q_list:
        if use_dismax:
            q = web.urlquote(' '.join(q_list))
            solr_select += "&defType=dismax&qf=text+title^5+author_name^5&bf=sqrt(edition_count)^10"
        else:
            q = web.urlquote(' '.join(q_list + ['_val_:"sqrt(edition_count)"^10']))
        solr_select += "&q=%s" % q
    solr_select += '&spellcheck=true&spellcheck.count=%d' % spellcheck_count
    solr_select += "&facet=true&" + '&'.join("facet.field=" + f for f in facet_fields)

    if 'public_scan' in param:
        v = param.pop('public_scan').lower()
        if v in ('true', 'false'):
            if v == 'false':
                # also constrain on print disabled since the index may not be in sync
                param.setdefault('print_disabled', 'false')
            solr_select += '&fq=public_scan_b:%s' % v

    if 'print_disabled' in param:
        v = param.pop('print_disabled').lower()
        if v in ('true', 'false'):
            solr_select += '&fq=%ssubject_key:protected_daisy' % ('-' if v == 'false' else '')

    k = 'has_fulltext'
    if k in param:
        v = param[k].lower()
        if v not in ('true', 'false'):
            del param[k]
        param[k] == v
        solr_select += '&fq=%s:%s' % (k, v)

    for k in facet_list_fields:
        if k == 'author_facet':
            k = 'author_key'
        if k not in param:
            continue
        v = param[k]
        solr_select += ''.join('&fq=%s:"%s"' % (k, url_quote(l)) for l in v if l)
    if sort:
        solr_select += "&sort=" + url_quote(sort)

    solr_select += '&wt=' + url_quote(param.get('wt', 'standard'))

    # For single-core solr, filter the results by type:work
    if config.get("single_core_solr"):
        solr_select += "&fq=type:work"

    solr_result = execute_solr_query(solr_select)
    if solr_result is None:
        return (None, None, None)
    reply = solr_result.read()
    return (reply, solr_select, q_list)