Source

plugin_h_search / plugin_h_search /

Filename Size Date modified Message
..
1.1 KB
1.7 KB
13.2 KB
Search plugin for web2py (alpha)
================================

Author  : Hans Christian von Stockhausen <hc at vst.io>
Date    : 2011-01-13
Home    : http://bitbucket.org/hc/plugin_h_search
License : MIT (see LICENSE)

This module implements a simple search engine for web2py-powered sites. It is 
intended for local site search on or off Google Appengine. Results are ranked 
by keyword frequency, keyword position relative to the top of the page and 
keyword proximity to one another. 
The engine is based on "Programming Collective Intelligence" (Ch. 4).


Dependencies
------------

This module requires the following third party modules:

pyporter2     - https://github.com/mdirolf/pyporter2
BeautifulSoup - www.crummy.com/software/BeautifulSoup/

Note: The w2p plugin package contains these already for your convinience.


Example 
-------

### models/mymodel.py
search = local_import('plugin_h_search')
search.create_tables(globals())

### controllers/mycontroller.py
search = local_import('plugin_h_search')

@search.index(globals(), search.INDEX_DAILY)
def foo():
    " The entire html output of this page is indexed max once a day "
    return dict(message='Hello World')

def bar():
    " Index arbitrary data. Note that no decorator is used "
    url = 'http://www.web2py.com'
    data = urllib2.urlopen(url).read()
    idx = search.Indexer(globals(), search.INDEX_WEEKLY)
    idx.add(data, url)
    return 'Indexed %s' % url

@cache(request.vars.get('q'), time_expire=3600, cache_model=cache_disk)  
def search():
    return dict(results=search.search(globals(), request.vars.get('q'))
  
### views/mycontroller/search.html
{{extend 'layout.html'}}
{{for r in results:}}
    {{=A(XML(r.title), _href=r.url)}}
    {{=XML(r.snippet)}}
{{pass}}