Skip to content

The Unified and Autonomous Development Platform

Fix word match issue of text query.

robin shine8631c13b0c9b
diff --git a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/TextQuery.java b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/TextQuery.java
index 95c7fd77a2..c6c4fe8c9f 100644
--- a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/TextQuery.java
+++ b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/TextQuery.java
@@ -10,6 +10,7 @@ import java.util.regex.Pattern;
import javax.annotation.Nullable;
+import org.apache.commons.lang3.CharUtils;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.treewalk.TreeWalk;
@@ -71,7 +72,24 @@ public class TextQuery extends BlobQuery {
int start = casedLine.indexOf(searchFor, 0);
while (start != -1) {
int end = start + searchFor.length();
- matches.add(new TextHit.Range(start, end));
+ if (isWordMatch()) {
+ char beforeChar;
+ if (start == 0)
+ beforeChar = ' ';
+ else
+ beforeChar = line.charAt(start-1);
+
+ char afterChar;
+ if (end == line.length())
+ afterChar = ' ';
+ else
+ afterChar = line.charAt(end);
+
+ if (!isWordChar(beforeChar) && !isWordChar(afterChar))
+ matches.add(new TextHit.Range(start, end));
+ } else {
+ matches.add(new TextHit.Range(start, end));
+ }
start = casedLine.indexOf(searchFor, end);
}
if (!matches.isEmpty()) {
@@ -91,4 +109,8 @@ public class TextQuery extends BlobQuery {
}
}
+ private boolean isWordChar(char ch) {
+ return CharUtils.isAsciiAlphanumeric(ch) || ch == '_';
+ }
+
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/TestPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/TestPage.java
index 992108a086..a6f79ab99e 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/TestPage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/TestPage.java
@@ -1,5 +1,8 @@
package com.pmease.gitplex.web.page;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
import java.util.List;
import org.apache.wicket.ajax.AjaxRequestTarget;
@@ -8,6 +11,7 @@ import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;
+import com.pmease.commons.git.Commit;
import com.pmease.commons.hibernate.dao.Dao;
import com.pmease.gitplex.core.GitPlex;
import com.pmease.gitplex.core.manager.IndexManager;
@@ -30,12 +34,11 @@ public class TestPage extends BasePage {
@Override
public void onClick() {
- Repository repo = GitPlex.getInstance(Dao.class).load(Repository.class, 2L);
+ Repository repo = GitPlex.getInstance(Dao.class).load(Repository.class, 1L);
IndexManager indexManager = GitPlex.getInstance(IndexManager.class);
- /*
Date since;
try {
- since = new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-01");
+ since = new SimpleDateFormat("yyyy-MM-dd").parse("2010-01-01");
} catch (ParseException e) {
throw new RuntimeException(e);
}
@@ -49,8 +52,7 @@ public class TestPage extends BasePage {
indexManager.index(repo, commit.getHash());
}
System.out.println("Total Minutes: " + (System.currentTimeMillis()-time)/1000/60);
- */
- indexManager.index(repo, repo.git().parseRevision("master", true));
+// indexManager.index(repo, repo.git().parseRevision("master", true));
}
});