Commits

Ivan Melnikov  committed 6de7bde Draft

Use page id as search doc id

  • Participants
  • Parent commits e8c5456

Comments (0)

Files changed (3)

 from creole.html_emitter import HtmlEmitter
 
 from user import User, get_user, auth
-from search import add_page
+# from search import update_page_search, delete_page_search
 
 ##################
 ###   Models   ###
 			deferred.defer(add_page_index, page.key().id(), path)
 
 		# add to search index
-		deferred.defer(add_page, page.key().id())
+		from search import update_page_search
+		deferred.defer(update_page_search, page.key().id())
 
 		# notify subscribers
 		deferred.defer(notify, page.key().id(), user.email)
 	def post(self, path):
 		if not path: path = '/'
 		page = get_page(path)
-		index = PageIndex.get_by_id(page.key().id())
+
+		page_id = page.key().id()
+		index = PageIndex.get_by_id(page_id)
 		page.delete()
 		index.delete()
 
 		memcache.delete(CACHE_PAGE+path)
 
+		from search import delete_page_search
+		deferred.defer(delete_page_search, page_id)
+
 		# TODO: Delete files?
 
 		self.redirect('/')
 from google.appengine.api import search
 
 from user import auth
+from page import Page
 
 
 _INDEX_NAME="page"
 
-def add_page(page_id):
-	from page import Page
+def update_page_search(page_id):
 	page = Page.get_by_id(page_id)
 	# add to search index
-	doc = search.Document(doc_id=page.path, 
+	doc = search.Document(doc_id=str(page_id), 
 						fields=[search.HtmlField(name='html', value=page.html),])
 	try:
 	    search.Index(name=_INDEX_NAME).put(doc)
 	except search.Error:
 	    logging.exception('Put failed')
 
+def delete_page_search(page_id):
+	# remove from index
+	search.Index(name=_INDEX_NAME).delete(page_id)
+
 def search_page(text):
-	docs = []
+	pages = []
 	if not text:
-		return docs
+		return pages
 	try:
 	    results = search.Index(name=_INDEX_NAME).search(text)
 	    for doc in results:
-	    	docs.append(doc.doc_id)
+	    	page = Page.get_by_id(int(doc.doc_id))
+	    	pages.append(page)
 	except search.Error:
 	    logging.exception('Search failed')
-	return docs
+	return pages
 
 
 

File template/search/search.html

 	</form>
 
 	{# search results #}
-	{% for link in docs %}
+	{% for page in docs %}
 		<div class="search-item">
 			{{ loop.index }}
-			<a href="{{ link }}">{{ link }}</a>
+			<a href="{{ page.path }}">{{ page.path }}</a>
 		</div>
 	{% endfor %}