zoukankan      html  css  js  c++  java
  • solrpy pagnator batch

    i use solr1.4.1 version,and the return xml is changed,
    the original return xml is like this


    0
    12

    test
    ...

    ...



    document_id
    some test text
    ...





    2
    ...






    some test text
    ...


    ...



    in responseHeader node has params sub node,but the new version(1.4.1),cancle it,so we need hack some code to fix this isuue.

    so when use the default paginator has one issue,
    when i invoke it will thrown errors as follows:



    class SolrPaginator:

    """

    Create a Django-like Paginator for a solr response object. Can be handy

    when you want to hand off a Paginator and/or Page to a template to

    display results, and provide links to next page, etc.



    For example:

    >>> from solr import SolrConnection, SolrPaginator

    >>>

    >>> conn = SolrConnection('http://localhost:8083/solr')

    >>> response = conn.query('title:huckleberry')

    >>> paginator = SolrPaginator(response)

    >>> print paginator.num_pages

    >>> page = paginator.get_page(5)



    For more details see the Django Paginator documentation and solrpy

    unittests.



    http://docs.djangoproject.com/en/dev/topics/pagination/



    """



    def __init__(self, result, default_page_size='10'):

    self.params = {"rows":default_page_size}

    # lexus modified at 201007

    #self.params = result.header['params']

    self.result = result

    self.conn = result._connection



    if 'rows' in self.params:

    self.page_size = int(self.params['rows'])

    elif default_page_size:

    try:

    self.page_size = int(default_page_size)

    except ValueError:

    raise ValueError('default_page_size must be an integer')



    if self.page_size < len(self.result.results):

    raise ValueError('Invalid default_page_size specified, lower '

    'than number of results')



    else:

    self.page_size = len(self.result.results)

    ....



    实际调用的例子
    #!/usr/bin/env python
    #encoding=utf8

    import solr

    # create a connection to a solr server
    s = solr.SolrConnection('http://localhost:8080/solr/core0')

    # add a document to the index
    #s.add(id=1, title='Lucene in Action', author=['Erik Hatcher', 'Otis Gospodnetić'])
    #s.commit()

    # do a search
    response = s.query('正品')
    for hit in response.results:
    print hit['name'].encode("utf8")
    print hit['price']

    response = s.query('*', facet='true', facet_field='site')
    for hit in response.results:
    print hit["name"].encode("utf8")
    if "brand" in hit:
    print hit["brand"]
    else:
    print "no brand"

    response = s.query('*')
    paginator = solr.SolrPaginator(response,default_page_size='1')
    print type(paginator)
    print paginator.num_pages

  • 相关阅读:
    SpringBoot集成Mybatis
    springboot通过slf4j配置日志
    SpringBoot导入jsp依赖始终报错
    shiro小记
    阿里开发手册华山版——(编程规约篇)记录目前自己不合理的地方
    [网络流24题] 2. 太空飞行计划问题 解题报告
    LOJ 6089 小 Y 的背包计数问题 解题报告 (动态规划)
    UVA 10599 Blocks 解题报告 (动态规划)
    Comet OJ#12E Ternary String Counting 解题报告
    [WC2016]挑战NPC 解题报告
  • 原文地址:https://www.cnblogs.com/lexus/p/1784427.html
Copyright © 2011-2022 走看看