Permalink
Browse files

improving search

  • Loading branch information...
1 parent 0622a13 commit 68b2085b81c9cc0cf3a50e46774ac6749df412aa @varunpant committed Dec 27, 2012
@@ -93,7 +93,7 @@ def GET(self, page):
count = post_service.count_published()
if count < 0:
return notfound("No archived posts were found.")
- offset = start_index(page, blog_settings.items_per_page) + blog_settings.posts_in_home
+ offset = start_index(page, blog_settings.items_per_page)
posts = post_service.get_published(offset, blog_settings.items_per_page)
page_count = total_page(count - blog_settings.posts_in_home, blog_settings.items_per_page)
nextLink = previousLink = None
@@ -115,7 +115,7 @@ def GET(self, page, year):
count = post_service.count_published()
if count < 0:
return notfound("No archived posts were found.")
- offset = start_index(page, blog_settings.items_per_page) + blog_settings.posts_in_home
+ offset = start_index(page, blog_settings.items_per_page)
posts = post_service.find_published_by_year(year, offset, blog_settings.items_per_page)
page_count = total_page(count - blog_settings.posts_in_home, blog_settings.items_per_page)
nextLink = previousLink = None
@@ -139,7 +139,7 @@ def GET(self, page, year, month):
count = post_service.count_published()
if count < 0:
return notfound("No archived posts were found.")
- offset = start_index(page, blog_settings.items_per_page) + blog_settings.posts_in_home
+ offset = start_index(page, blog_settings.items_per_page)
posts = post_service.find_published_by_year_and_month(safe_year, safe_month, offset, blog_settings.items_per_page)
page_count = total_page(count - blog_settings.posts_in_home, blog_settings.items_per_page)
nextLink = previousLink = None
@@ -166,7 +166,7 @@ def GET(self, page, year, month, day):
count = post_service.count_published()
if count < 0:
return notfound("No archived posts were found.")
- offset = start_index(page, blog_settings.items_per_page) + blog_settings.posts_in_home
+ offset = start_index(page, blog_settings.items_per_page)
posts = post_service.find_published_by_year_month_and_day(safe_year, safe_month, safe_day, offset, blog_settings.items_per_page)
page_count = total_page(count - blog_settings.posts_in_home, blog_settings.items_per_page)
nextLink = previousLink = None
@@ -213,7 +213,7 @@ class Search:
def GET(self):
q = web.input().q
page = safe_number(web.input(page="1").page)
- offset = start_index(page, blog_settings.items_per_page) + blog_settings.posts_in_home
+ offset = start_index(page, blog_settings.items_per_page)
count = safe_number(search_service.getCount(q)[0])
result = []
msg = None
@@ -229,4 +229,4 @@ def GET(self):
msg = "No results were found for query \" " + q + " \""
- return render.search(q, result, nextLink, previousLink, msg)
+ return render.search(q,count, result, nextLink, previousLink, msg)
View
BIN db/app.db
Binary file not shown.
@@ -3,12 +3,12 @@
def search(q, offset, items_per_page):
session = getSession()
connection = session.connection()
- resp = connection.execute("SELECT * from(SELECT * FROM search where title LIKE '%" + str(q) + "%' UNION SELECT * FROM search where content LIKE '%" + str(q) + "%' ) ORDER BY title LIMIT " + str(items_per_page) + " OFFSET " + str(offset))
+ resp = connection.execute("SELECT * from(SELECT * FROM search where title LIKE '%" + str(q) + "%' UNION SELECT * FROM search where content LIKE '%" + str(q) + "%' ) WHERE published_at is not null ORDER BY published_at LIMIT " + str(items_per_page) + " OFFSET " + str(offset))
return resp.fetchall()
def getCount(q):
session = getSession()
connection = session.connection()
- resp = connection.execute("SELECT count(*) from(SELECT * FROM search where title LIKE '%" + q + "%' UNION SELECT * FROM search where content LIKE '%" + q + "%' )")
+ resp = connection.execute("SELECT count(*) from(SELECT * FROM search where title LIKE '%" + q + "%' UNION SELECT * FROM search where content LIKE '%" + q + "%' ) WHERE published_at is not null")
return resp.fetchone()
View
@@ -10,12 +10,12 @@ class SearchServiceQueryingTestCase(unittest.TestCase):
def setUp(self):
self.service = search_service
- db = web.database(dbn='sqlite', db = web.config.dbpath)
- executeRaw("CREATE VIRTUAL TABLE search using FTS4(title, slug, content, isPost);")
- executeRaw("INSERT INTO SEARCH(title,slug, content, isPost) VALUES ('Hello World','Hello World', 'hello world this is my first post', '1')")
- executeRaw("INSERT INTO SEARCH(title,slug, content, isPost) VALUES ('Hi World','Hi World', 'hello world this is my second post', '1')")
- executeRaw("INSERT INTO SEARCH(title,slug, content, isPost) VALUES('Hello World','Hello World', 'Sup world this is another post', '1')")
- executeRaw("INSERT INTO SEARCH(title,slug, content, isPost) VALUES('o World', 'o World', 'namste world this is my first post', '1')")
+ db = web.database(dbn='sqlite', db=web.config.dbpath)
+ executeRaw("CREATE VIRTUAL TABLE search using FTS4(title, slug, content, published_at,ispost);")
+ executeRaw("INSERT INTO SEARCH(title,slug, content, published_at, isPost) VALUES ('Hello World','Hello World', 'hello world this is my first post','2010-10-03 11:15:52.000000', '1')")
+ executeRaw("INSERT INTO SEARCH(title,slug, content, published_at, isPost) VALUES ('Hi World','Hi World', 'hello world this is my second post','2010-10-04 11:15:52.000000','1')")
+ executeRaw("INSERT INTO SEARCH(title,slug, content, published_at, isPost) VALUES('Hello World','Hello World', 'Sup world this is another post','2010-10-05 11:15:52.000000','1')")
+ executeRaw("INSERT INTO SEARCH(title,slug, content, published_at, isPost) VALUES('o World', 'o World', 'namste world this is my first post','2010-10-06 11:15:52.000000','1')")
@@ -24,13 +24,13 @@ def tearDown(self):
def test_search(self):
- results = self.service.search('hello',0,5)
+ results = self.service.search('hello', 0, 5)
self.assertEqual(len(results), 3)
results = self.service.search('sup', 0, 5)
self.assertEqual(len(results), 1)
- results = self.service.search('World',0,5)
+ results = self.service.search('World', 0, 5)
self.assertEqual(len(results), 4)
- results = self.service.search('post',0,5)
+ results = self.service.search('post', 0, 5)
self.assertEqual(len(results), 4)
def test_search_count(self):
@@ -1,28 +1,41 @@
-$def with (query,searchResults,nextLink,previousLink,msg)
+$def with (query,count,searchResults,nextLink,previousLink,msg)
$var title = "Search results for query: " + query
-<h4 style="margin-bottom:10px; ">Search Results for: $:query</h4>
+<h4 style="margin-bottom:10px; ">Search Results for: <em>$:query</em> &nbsp; [$:count]</h4>
$if msg:
<p>
$msg
</p>
$else:
- $for post in searchResults:
- <article class="post">
- <header>
- <h4><a href="/posts/$:post.slug">$:post.slug</a></h4>
- </header>
-
- <section class="content">
- $if len(post.content) <= 500:
- $:post.content
- $else:
- $:post.content[0:499]
- </section>
- <div class="more">
- <a href="/posts/$:post.slug"><b>read on</b> &raquo;</a>
- </div>
-
- </article>
+ $for result in searchResults:
+ $if result.ispost:
+ <article class="post">
+ <header>
+ <h4><a href="/posts/$:result.slug">$:result.title</a></h4>
+ </header>
+
+ <section class="content">
+ $if len(result.content) <= 500:
+ $:result.content
+ $else:
+ $:result.content[0:499]
+ </section>
+ <div class="more">
+ <a href="/posts/$:result.slug"><b>read on</b> &raquo;</a>
+ </div>
+ </article>
+ $else:
+ <article class="post">
+ <header>
+ <h4><a href="/$:result.slug">$:result.title</a></h4>
+ </header>
+ <section class="content">
+
+ </section>
+ <div class="more">
+ <a href="/$:result.slug"><b>read on</b> &raquo;</a>
+ </div>
+ </article>
+
<div id="post-nextprev" class="clearfix">
$if previousLink:
<div id="post-next" > <a class="btn btn-inverse" href="$:previousLink" rel="prev">« More recent articles</a></div>

0 comments on commit 68b2085

Please sign in to comment.