Janto Dreijer avatar Janto Dreijer committed 94a124d

ranked search

Comments (0)

Files changed (1)

 		"""
 		self.print_header()
 		self.print_menu()
-		text = self.request.get("text").strip()
+		query_text = self.request.get("text").strip()
 
 		self.write("<p>")
-		self.write(self.search_box(default=text))
+		self.write(self.search_box(default=query_text))
 		self.write("</p>")
 
-		packages = []
+		field_weight = dict(name=100, description=50)
+		weights_packages = []
 		for package in Package.packages().values():
-			d = package.info()
-			if any(text in field for field in package.info().values()):
-				packages.append(package)
-				continue
+			w = 0
+			for field, text in package.info().items():
+				n = query_text in text
+				w += field_weight.get(field, 1) * n
+			if 0 < w:
+				weights_packages.append((-w, package))
+		weights_packages.sort() # highest scoring first
+		if weights_packages:
+			weights, packages = zip(*weights_packages)
+			self.write("\n<!-- ")
+			self.write(weights)
+			self.write(" -->\n")
+		else:
+			packages = []
 
-		self.write("<p>search results for <strong>%s</strong>:<br />" % (htmlquote(text) or "all"))
+		self.write("<p>search results for <strong>%s</strong>:<br />" % (htmlquote(query_text) or "all"))
 		if packages:
 			self.write(table_of_packages(packages))
 		else:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.