(refs #3)Apply likeEncode to search keyword.

This commit is contained in:
takezoe
2013-07-18 00:47:55 +09:00
parent 69ec4175eb
commit 4fb6005f44
2 changed files with 22 additions and 13 deletions

View File

@@ -47,7 +47,7 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
case "issue" => if(query.isEmpty){ case "issue" => if(query.isEmpty){
search.html.issues(Nil, "", repository) search.html.issues(Nil, "", repository)
} else { } else {
search.html.issues(queryIssues(repository.owner, repository.name, query).map { case (issue, commentCount, content) => search.html.issues(searchIssuesByKeyword(repository.owner, repository.name, query).map { case (issue, commentCount, content) =>
IssueSearchResult( IssueSearchResult(
issue.issueId, issue.issueId,
issue.title, issue.title,
@@ -69,7 +69,7 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
treeWalk.setRecursive(true) treeWalk.setRecursive(true)
treeWalk.addTree(revCommit.getTree) treeWalk.addTree(revCommit.getTree)
val lowerQueries = StringUtil.splitWords(query.toLowerCase) val keywords = StringUtil.splitWords(query.toLowerCase)
val list = new ListBuffer[(String, (String, Int))] val list = new ListBuffer[(String, (String, Int))]
while (treeWalk.next()) { while (treeWalk.next()) {
if(treeWalk.getFileMode(0) != FileMode.TREE){ if(treeWalk.getFileMode(0) != FileMode.TREE){
@@ -77,7 +77,7 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
if(FileUtil.isText(bytes)){ if(FileUtil.isText(bytes)){
val text = new String(bytes, "UTF-8") val text = new String(bytes, "UTF-8")
val lowerText = text.toLowerCase val lowerText = text.toLowerCase
val indices = lowerQueries.map(lowerText.indexOf _) val indices = keywords.map(lowerText.indexOf _)
if(!indices.exists(_ < 0)){ if(!indices.exists(_ < 0)){
list.append((treeWalk.getPathString, getHighlightText(text, query))) list.append((treeWalk.getPathString, getHighlightText(text, query)))
} }
@@ -99,14 +99,14 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
}) })
private def getHighlightText(content: String, query: String): (String, Int) = { private def getHighlightText(content: String, query: String): (String, Int) = {
val lowerQueries = StringUtil.splitWords(query.toLowerCase) val keywords = StringUtil.splitWords(query.toLowerCase)
val lowerText = content.toLowerCase val lowerText = content.toLowerCase
val indices = lowerQueries.map(lowerText.indexOf _) val indices = keywords.map(lowerText.indexOf _)
if(!indices.exists(_ < 0)){ if(!indices.exists(_ < 0)){
val lineNumber = content.substring(0, indices.min).split("\n").size - 1 val lineNumber = content.substring(0, indices.min).split("\n").size - 1
val highlightText = StringUtil.escapeHtml(content.split("\n").drop(lineNumber).take(5).mkString("\n")) val highlightText = StringUtil.escapeHtml(content.split("\n").drop(lineNumber).take(5).mkString("\n"))
.replaceAll("(?i)(" + lowerQueries.map("\\Q" + _ + "\\E").mkString("|") + ")", .replaceAll("(?i)(" + keywords.map("\\Q" + _ + "\\E").mkString("|") + ")",
"<span style=\"background-color: yellow;\">$1</span>") "<span style=\"background-color: yellow;\">$1</span>")
(highlightText, lineNumber + 1) (highlightText, lineNumber + 1)
} else { } else {

View File

@@ -236,13 +236,22 @@ trait IssuesService {
} }
.update (closed, currentDate) .update (closed, currentDate)
def queryIssues(owner: String, repository: String, query: String): List[(Issue, Int, String)] = { /**
val lowerQueries = StringUtil.splitWords(query.toLowerCase) * Search issues by keyword.
*
* @param owner the repository owner
* @param repository the repository name
* @param query the keywords separated by whitespace.
* @return issues with comment count and matched content of issue or comment
*/
def searchIssuesByKeyword(owner: String, repository: String, query: String): List[(Issue, Int, String)] = {
import scala.slick.driver.H2Driver.likeEncode
val keywords = StringUtil.splitWords(query.toLowerCase)
// Search Issue // Search Issue
val issues = Query(Issues).filter { t => val issues = Query(Issues).filter { t =>
lowerQueries.map { query => keywords.map { keyword =>
(t.title.toLowerCase like '%' + query + '%') || (t.content.toLowerCase like '%' + query + '%') // TODO escape!!!! (t.title.toLowerCase like (s"%${likeEncode(keyword)}%", '^')) || (t.content.toLowerCase like (s"%${likeEncode(keyword)}%", '^'))
} .reduceLeft { (a, b) => } .reduceLeft { (a, b) =>
a && b a && b
} }
@@ -252,8 +261,8 @@ trait IssuesService {
val comments = Query(IssueComments).innerJoin(Issues).on { case (t1, t2) => val comments = Query(IssueComments).innerJoin(Issues).on { case (t1, t2) =>
t1.byIssue(t2.userName, t2.repositoryName, t2.issueId) t1.byIssue(t2.userName, t2.repositoryName, t2.issueId)
}.filter { case (t1, t2) => }.filter { case (t1, t2) =>
lowerQueries.map { query => keywords.map { query =>
t1.content.toLowerCase like '%' + query + '%' // TODO escape!!!! t1.content.toLowerCase like (s"%${likeEncode(query)}%", '^')
}.reduceLeft { (a, b) => }.reduceLeft { (a, b) =>
a && b a && b
} }