Skip to content

The Unified and Autonomous Development Platform

Search symbols with wildcard instead of BiGram.

robin shinef49f5d34b757
diff --git a/gitplex.search/src/main/java/com/pmease/gitplex/search/DefaultIndexManager.java b/gitplex.search/src/main/java/com/pmease/gitplex/search/DefaultIndexManager.java
index 3e4edf0ff6..a6ad8ca6c7 100644
--- a/gitplex.search/src/main/java/com/pmease/gitplex/search/DefaultIndexManager.java
+++ b/gitplex.search/src/main/java/com/pmease/gitplex/search/DefaultIndexManager.java
@@ -240,9 +240,9 @@ public class DefaultIndexManager implements IndexManager {
document.add(new StringField(BLOB_PATH.name(), blobPath, Store.YES));
if (blobPath.indexOf('/') != -1)
- document.add(new TextField(BLOB_SYMBOLS.name(), StringUtils.substringAfterLast(blobPath, "/"), Store.NO));
+ document.add(new StringField(BLOB_SYMBOLS.name(), StringUtils.substringAfterLast(blobPath, "/"), Store.NO));
else
- document.add(new TextField(BLOB_SYMBOLS.name(), blobPath, Store.NO));
+ document.add(new StringField(BLOB_SYMBOLS.name(), blobPath, Store.NO));
ObjectLoader objectLoader = repo.open(blobId);
if (objectLoader.getSize() <= MAX_INDEXABLE_SIZE) {
@@ -257,7 +257,7 @@ public class DefaultIndexManager implements IndexManager {
for (Symbol symbol: extractor.extract(content)) {
String name = symbol.getName();
if (name != null)
- document.add(new TextField(BLOB_SYMBOLS.name(), name, Store.NO));
+ document.add(new StringField(BLOB_SYMBOLS.name(), name, Store.NO));
}
} catch (ExtractException e) {
logger.debug("Error extracting symbols from blob (hash:" + blobId.name() + ", path:" + blobPath + ")", e);
diff --git a/gitplex.search/src/main/java/com/pmease/gitplex/search/hit/file.png b/gitplex.search/src/main/java/com/pmease/gitplex/search/hit/file.png
deleted file mode 100644
index 663cbaddde..0000000000
Binary files a/gitplex.search/src/main/java/com/pmease/gitplex/search/hit/file.png and /dev/null differ
diff --git a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/BlobQuery.java b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/BlobQuery.java
index 72c0b64ae7..3bc5d72b90 100644
--- a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/BlobQuery.java
+++ b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/BlobQuery.java
@@ -1,7 +1,6 @@
package com.pmease.gitplex.search.query;
-import static com.pmease.gitplex.search.FieldConstants.*;
-import static com.pmease.gitplex.search.IndexConstants.NGRAM_SIZE;
+import static com.pmease.gitplex.search.FieldConstants.BLOB_PATH;
import java.util.Collection;
import java.util.List;
@@ -16,7 +15,6 @@ import org.apache.lucene.search.WildcardQuery;
import org.eclipse.jgit.treewalk.TreeWalk;
import com.pmease.gitplex.search.hit.QueryHit;
-import com.pmease.gitplex.search.query.regex.RegexLiterals;
public abstract class BlobQuery {
@@ -88,13 +86,9 @@ public abstract class BlobQuery {
public Query asLuceneQuery() {
BooleanQuery query = new BooleanQuery(true);
- if (regex) {
- Query literalsQuery = new RegexLiterals(searchFor).asLuceneQuery(fieldName);
- if (literalsQuery != null)
- query.add(literalsQuery, Occur.MUST);
- } else if (searchFor.length() >= NGRAM_SIZE) {
- query.add(new NGramLuceneQuery(fieldName, searchFor), Occur.MUST);
- }
+ Query searchForQuery = getLuceneQueryOfSearchFor();
+ if (searchForQuery != null)
+ query.add(searchForQuery, Occur.MUST);
if (pathPrefix != null)
query.add(new WildcardQuery(BLOB_PATH.term(pathPrefix + "*")), Occur.MUST);
@@ -111,6 +105,9 @@ public abstract class BlobQuery {
return new WildcardQuery(BLOB_PATH.term("*"));
}
+ @Nullable
+ protected abstract Query getLuceneQueryOfSearchFor();
+
protected Pattern getPattern() {
if (regex) {
if (pattern == null) {
diff --git a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/NGramLuceneQuery.java b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/NGramLuceneQuery.java
index ed64d25b7d..1f384a379a 100644
--- a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/NGramLuceneQuery.java
+++ b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/NGramLuceneQuery.java
@@ -1,7 +1,5 @@
package com.pmease.gitplex.search.query;
-import static com.pmease.gitplex.search.IndexConstants.NGRAM_SIZE;
-
import java.io.IOException;
import java.io.StringReader;
@@ -14,12 +12,12 @@ import com.google.common.base.Preconditions;
public class NGramLuceneQuery extends NGramPhraseQuery {
- public NGramLuceneQuery(String fieldName, String fieldValue) {
- super(NGRAM_SIZE);
+ public NGramLuceneQuery(String fieldName, String fieldValue, int gramSize) {
+ super(gramSize);
- Preconditions.checkArgument(fieldValue.length()>=NGRAM_SIZE);
+ Preconditions.checkArgument(fieldValue.length()>=gramSize);
- try (NGramTokenizer tokenizer = new NGramTokenizer(new StringReader(fieldValue.toLowerCase()), NGRAM_SIZE, NGRAM_SIZE)) {
+ try (NGramTokenizer tokenizer = new NGramTokenizer(new StringReader(fieldValue.toLowerCase()), gramSize, gramSize)) {
tokenizer.reset();
while (tokenizer.incrementToken()) {
add(new Term(fieldName,
diff --git a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/SymbolQuery.java b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/SymbolQuery.java
index 96658a0bac..8a13ff6e0d 100644
--- a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/SymbolQuery.java
+++ b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/SymbolQuery.java
@@ -8,6 +8,9 @@ import java.util.regex.Pattern;
import javax.annotation.Nullable;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.WildcardQuery;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.slf4j.Logger;
@@ -25,6 +28,7 @@ import com.pmease.gitplex.search.IndexConstants;
import com.pmease.gitplex.search.hit.FileHit;
import com.pmease.gitplex.search.hit.QueryHit;
import com.pmease.gitplex.search.hit.SymbolHit;
+import com.pmease.gitplex.search.query.regex.RegexLiterals;
public class SymbolQuery extends BlobQuery {
@@ -103,4 +107,15 @@ public class SymbolQuery extends BlobQuery {
}
}
+
+ @Override
+ protected Query getLuceneQueryOfSearchFor() {
+ if (isRegex())
+ return new RegexLiterals(getSearchFor()).asWildcardQuery(getFieldName());
+ else if (getSearchFor().length() != 0)
+ return new WildcardQuery(new Term(getFieldName(), getSearchFor() + "*"));
+ else
+ return null;
+ }
+
}
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 c6c4fe8c9f..f631a855c8 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
@@ -1,5 +1,7 @@
package com.pmease.gitplex.search.query;
+import static com.pmease.gitplex.search.IndexConstants.NGRAM_SIZE;
+
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
@@ -11,6 +13,7 @@ import java.util.regex.Pattern;
import javax.annotation.Nullable;
import org.apache.commons.lang3.CharUtils;
+import org.apache.lucene.search.Query;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.treewalk.TreeWalk;
@@ -18,8 +21,9 @@ import com.google.common.base.Splitter;
import com.pmease.commons.util.Charsets;
import com.pmease.gitplex.search.FieldConstants;
import com.pmease.gitplex.search.IndexConstants;
-import com.pmease.gitplex.search.hit.TextHit;
import com.pmease.gitplex.search.hit.QueryHit;
+import com.pmease.gitplex.search.hit.TextHit;
+import com.pmease.gitplex.search.query.regex.RegexLiterals;
public class TextQuery extends BlobQuery {
@@ -112,5 +116,15 @@ public class TextQuery extends BlobQuery {
private boolean isWordChar(char ch) {
return CharUtils.isAsciiAlphanumeric(ch) || ch == '_';
}
+
+ @Override
+ protected Query getLuceneQueryOfSearchFor() {
+ if (isRegex())
+ return new RegexLiterals(getSearchFor()).asNGramQuery(getFieldName(), NGRAM_SIZE);
+ else if (getSearchFor().length() >= NGRAM_SIZE)
+ return new NGramLuceneQuery(getFieldName(), getSearchFor(), NGRAM_SIZE);
+ else
+ return null;
+ }
}
diff --git a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/regex/RegexLiterals.java b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/regex/RegexLiterals.java
index d136accb83..1b6d3c8e24 100644
--- a/gitplex.search/src/main/java/com/pmease/gitplex/search/query/regex/RegexLiterals.java
+++ b/gitplex.search/src/main/java/com/pmease/gitplex/search/query/regex/RegexLiterals.java
@@ -8,9 +8,11 @@ import javax.annotation.Nullable;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
+import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
+import org.apache.lucene.search.WildcardQuery;
import com.google.common.base.Strings;
import com.pmease.gitplex.search.query.NGramLuceneQuery;
@@ -29,13 +31,13 @@ public class RegexLiterals {
}
@Nullable
- public Query asLuceneQuery(String fieldName) {
+ public Query asNGramQuery(String fieldName, int gramSize) {
BooleanQuery orQuery = new BooleanQuery();
for (List<LeafLiterals> row: rows) {
BooleanQuery andQuery = new BooleanQuery();
for (LeafLiterals literals: row) {
if (literals.getLiteral() != null && literals.getLiteral().length()>=NGRAM_SIZE)
- andQuery.add(new NGramLuceneQuery(fieldName, literals.getLiteral()), Occur.MUST);
+ andQuery.add(new NGramLuceneQuery(fieldName, literals.getLiteral(), gramSize), Occur.MUST);
}
if (andQuery.getClauses().length != 0)
orQuery.add(andQuery, Occur.SHOULD);
@@ -46,6 +48,24 @@ public class RegexLiterals {
return null;
}
+ @Nullable
+ public Query asWildcardQuery(String fieldName) {
+ BooleanQuery orQuery = new BooleanQuery();
+ for (List<LeafLiterals> row: rows) {
+ BooleanQuery andQuery = new BooleanQuery();
+ for (LeafLiterals literals: row) {
+ if (literals.getLiteral() != null && literals.getLiteral().length() != 0)
+ andQuery.add(new WildcardQuery(new Term(fieldName, literals.getLiteral() + "*")), Occur.MUST);
+ }
+ if (andQuery.getClauses().length != 0)
+ orQuery.add(andQuery, Occur.SHOULD);
+ }
+ if (orQuery.getClauses().length != 0)
+ return orQuery;
+ else
+ return null;
+ }
+
@Override
public String toString() {
StringBuilder orBuilder = new StringBuilder();
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/search/BlobSearchPanel.html b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/search/BlobSearchPanel.html
index 9583354a78..3144f381ec 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/search/BlobSearchPanel.html
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/search/BlobSearchPanel.html
@@ -26,7 +26,7 @@
</tr>
<wicket:enclosure child="moreSymbolHits">
<tr class="hit more-matches">
- <td class="icon"><i class="fa fa-arrow-circle-right fa-fw"></i></td>
+ <td class="icon"><i class="fa fa-arrow-circle-right"></i></td>
<td><a wicket:id="moreSymbolHits">Show more</a></td>
</tr>
</wicket:enclosure>
@@ -34,7 +34,7 @@
</wicket:enclosure>
<div wicket:id="noSymbolHits" class="no-matches"><i class="fa fa-info-circle"></i> No matches</div>
</div>
- <div class="texts section">
+ <div wicket:id="texts" class="texts section">
<div class="title">Text occurrences</div>
<wicket:enclosure child="textHits">
<table>
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/search/BlobSearchPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/search/BlobSearchPanel.java
index 7c1b095143..bd73ca8343 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/search/BlobSearchPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/search/BlobSearchPanel.java
@@ -43,6 +43,7 @@ import org.apache.wicket.request.resource.ResourceReference;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
import com.pmease.commons.wicket.AjaxEvent;
import com.pmease.commons.wicket.assets.hotkeys.HotkeysResourceReference;
import com.pmease.commons.wicket.behavior.RunTaskBehavior;
@@ -194,7 +195,17 @@ public abstract class BlobSearchPanel extends Panel {
});
- instantSearchResultContainer.add(new ListView<QueryHit>("textHits", new AbstractReadOnlyModel<List<QueryHit>>() {
+ WebMarkupContainer textSection = new WebMarkupContainer("texts") {
+
+ @Override
+ protected void onConfigure() {
+ super.onConfigure();
+ setVisible(instantSearchFor != null && instantSearchFor.length() >= IndexConstants.NGRAM_SIZE);
+ }
+
+ };
+ instantSearchResultContainer.add(textSection);
+ textSection.add(new ListView<QueryHit>("textHits", new AbstractReadOnlyModel<List<QueryHit>>() {
@Override
public List<QueryHit> getObject() {
@@ -237,7 +248,7 @@ public abstract class BlobSearchPanel extends Panel {
}
});
- instantSearchResultContainer.add(new AjaxLink<Void>("moreTextHits") {
+ textSection.add(new AjaxLink<Void>("moreTextHits") {
@Override
protected void onInitialize() {
@@ -273,7 +284,7 @@ public abstract class BlobSearchPanel extends Panel {
}
});
- instantSearchResultContainer.add(new WebMarkupContainer("noTextHits") {
+ textSection.add(new WebMarkupContainer("noTextHits") {
@Override
protected void onConfigure() {
@@ -298,21 +309,27 @@ public abstract class BlobSearchPanel extends Panel {
target.add(instantSearchResultContainer);
instantSearchFor = instantSearchInput.getInput();
- if (instantSearchFor != null && instantSearchFor.length() >= IndexConstants.NGRAM_SIZE) {
+ if (!Strings.isNullOrEmpty(instantSearchFor)) {
SearchManager searchManager = GitPlex.getInstance(SearchManager.class);
- BlobQuery blobQuery = new SymbolQuery(instantSearchInput.getInput(), false, false, false, MAX_INSTANT_QUERY_ENTRIES);
+ BlobQuery blobQuery = new SymbolQuery(instantSearchInput.getInput(),
+ false, false, false, MAX_INSTANT_QUERY_ENTRIES);
try {
symbolHits = searchManager.search(repoModel.getObject(), getCurrentCommit(), blobQuery);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
-
- TextQuery textQuery = new TextQuery(instantSearchInput.getInput(), false, false, false, MAX_INSTANT_QUERY_ENTRIES);
- try {
- textHits = searchManager.search(repoModel.getObject(), getCurrentCommit(), textQuery);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
+
+ if (instantSearchFor.length() >= IndexConstants.NGRAM_SIZE) {
+ TextQuery textQuery = new TextQuery(instantSearchInput.getInput(),
+ false, false, false, MAX_INSTANT_QUERY_ENTRIES);
+ try {
+ textHits = searchManager.search(repoModel.getObject(), getCurrentCommit(), textQuery);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ } else {
+ textHits = new ArrayList<>();
}
instantSearchResultDropdown.show(target);
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/search/blob-search.css b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/search/blob-search.css
index 354eb26b00..34f1ed5165 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/search/blob-search.css
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/search/blob-search.css
@@ -11,8 +11,8 @@
.blob-instant-search-result tr.hit>td {vertical-align: top; padding: 1px 0;}
.blob-instant-search-result tr.hit>td.icon {width: 24px;}
.blob-instant-search-result tr.hit.active>td {background: #E8E8E8;}
-.blob-instant-search-result .more-matches .fa {font-size: 16px;}
-.blob-instant-search-result .more-matches {margin: 8px 0; font-style: italic;}
+.blob-instant-search-result tr.more-matches .fa {font-size: 16px;}
+.blob-instant-search-result tr.more-matches>td {padding: 8px 0; font-style: italic;}
.blob-instant-search-result .no-matches {font-style: italic;}
.blob-instant-search-result td.content .scope {color: #666; font-size: 12px;}
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 8b4f4a482a..755c19c14b 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,8 +1,5 @@
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;
@@ -11,7 +8,6 @@ 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;
@@ -36,6 +32,7 @@ public class TestPage extends BasePage {
public void onClick() {
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("2010-01-01");
@@ -52,7 +49,8 @@ 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));
}
});