Tornado – Whoosh – MoreLike and MoreLikeThis

Like other search engines, Whoosh too provides more_like() and more_like_this() methods to find similar documents in the index, Typically, morelikethis doesnt execute any special query to get the list of similar documents to the one specified, but in fact it searches all other documents in the index relative to the document content that is specified. Here’s a example of more_like() method of Whoosh integrated with Tornado

User enters the document path and submits it to the index which then presents the similar morelike documents. User form code here

<title> More Like </title>
<FORM action="/morelikethis" method="POST">
Document:<input type=text name=path>
<input type="submit" name="submit">

In the code below:

  • document_number(path=path) gets the document number of the specified document path in the index
  • more_like(docnum, ‘content’) method then find documents *like* the specified document based on content
  • more_like_this(“content”, top=1) method searches the top 1 sub-hits

import whoosh,os
from whoosh import index
import whoosh.index
import whoosh.fields
import whoosh.qparser
import tornado.ioloop
import tornado.web
class Search(object):
def __init__(self, indexdir, searchstr=None):
self.indexdir = indexdir
self.searchstr = searchstr
def searcher(self):
schema = whoosh.fields.Schema(
path = whoosh.fields.ID(unique=True, stored=True),
title = whoosh.fields.TEXT(stored=True, phrase=False),
content = whoosh.fields.TEXT(stored=True),
tag = whoosh.fields.TEXT(stored=True),
category = whoosh.fields.TEXT(stored=True))
if not os.path.exists(self.indexdir):
ix = index.create_in(self.indexdir, schema)
writer = ix.writer()
writer.add_document(title=u"Welcome", content=u"This is welcome blog!",
path=u"/welcome", tag=u"Welcome", category=u"Welcome")
writer.add_document(title=u"Python Whoosh", content=u"Whoosh search library in pure Python",
path=u"/whoosh", tag=u"whoosh", category=u"Search")
writer.add_document(title=u"Python Tornado", content=u"Tornado Web Server for real-time web apps",
path=u"/tornado", tag=u"tornado", category=u"Web Server")
writer.add_document(title=u"Python Tornado Async", content=u"Tornado Web Server provides async web requests",
path=u"/tornadoasync", tag=u"async", category=u"Web Server")
writer.add_document(title=u"Python Tornado Templates", content=u"Tornado Web Server has template feature",
path=u"/tornadotemplates", tag=u"templates", category=u"Web Server")
writer.add_document(title=u"Python Tornado", content=u"Tornado Web Server is awesome",
path=u"/tornado", tag=u"great", category=u"Web Server")
_queryparser = whoosh.qparser.QueryParser('content', schema=schema)
s = ix.searcher()
class Home(tornado.web.RequestHandler):
def get(self):
self.write('It Works!')
class MoreLikeThis(tornado.web.RequestHandler):
def get(self):
def post(self):
path = self.get_argument('path')
print path
srch = Search('./indexer')
s = srch.searcher()
docnum = s.document_number(path=path)
print docnum
r = s.more_like(docnum, 'content')
head = "<h3>Documents like more like %s</h3><br />" %path
head1 = "<h3>Documents like more like this %s</h3><br />" %path
hits = ''; subhits = ''
for hit in r:
hits += "Title: " + hit["title"] + " Tag: " + hit["tag"] + " Category: " + hit["category"] + "<br />"
for subhit in hit.more_like_this("content", top=1):
subhits += "Title: " + subhit["title"] + " Tag: " + subhit["tag"] + "<br />"
application = tornado.web.Application([
(r"/",Home ),
(r"/morelikethis",MoreLikeThis ),
if __name__ == "__main__":

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.