Skip to content

Commit cd1fe74

Browse files
committed
Added a new field “fileType” to the Lucene index.
The field should contain the file type. By default the normalized name of the analyzer is used. For example: “CAnalyzer” becomes “c” and “SQLAnalyzer” becomes “sql”.
1 parent 871b35b commit cd1fe74

File tree

5 files changed

+71
-4
lines changed

5 files changed

+71
-4
lines changed

src/org/opensolaris/opengrok/analysis/AnalyzerGuru.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ public void populateDocument(Document doc, File file, String path,
278278
));
279279
}
280280
fa.analyze(doc, StreamSource.fromFile(file), xrefOut);
281+
282+
String fileType = fa.getFileTypeName();
283+
doc.add(new StringField(QueryBuilder.FILETYPE, fileType, Store.YES));
281284
}
282285
}
283286

src/org/opensolaris/opengrok/analysis/FileAnalyzer.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ public FileAnalyzer(FileAnalyzerFactory factory) {
129129
this.factory = factory;
130130

131131
}
132+
133+
/**
134+
* Returns the normalized name of the analyzer,
135+
* which should corresponds to the file type.
136+
* Example: The analyzer for the C language (CAnalyzer) would return “c”.
137+
* @return Normalized name of the analyzer.
138+
*/
139+
public String getFileTypeName() {
140+
String name = this.getClass().getSimpleName().toLowerCase();
141+
String suffix = "analyzer";
142+
143+
if (name.endsWith(suffix)) {
144+
return name.substring(0, name.length() - suffix.length());
145+
}
146+
147+
return name.toLowerCase();
148+
}
132149

133150
/**
134151
* Analyze the contents of a source file. This includes populating the

src/org/opensolaris/opengrok/search/QueryBuilder.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class QueryBuilder {
4949
public static final String REFS = "refs";
5050
public static final String PATH = "path";
5151
public static final String HIST = "hist";
52+
public static final String FILETYPE = "fileType";
5253
/**
5354
* Fields we use in lucene internal ones
5455
*/
@@ -160,6 +161,25 @@ public QueryBuilder setHist(String hist) {
160161
public String getHist() {
161162
return getQueryText(HIST);
162163
}
164+
165+
/**
166+
* Set search string for the "fileType" field.
167+
*
168+
* @param fileType query string to set
169+
* @return this instance
170+
*/
171+
public QueryBuilder setFileType(String fileType) {
172+
return addQueryText(FILETYPE, fileType);
173+
}
174+
175+
/**
176+
* Get search string for the "fileType" field.
177+
*
178+
* @return {@code null} if not set, the query string otherwise.
179+
*/
180+
public String getFileType() {
181+
return getQueryText(FILETYPE);
182+
}
163183

164184
/**
165185
* Get a map containing the query text for each of the fields that have been

src/org/opensolaris/opengrok/search/Search.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@
3636
@SuppressWarnings({"PMD.AvoidPrintStackTrace", "PMD.SystemPrintln"})
3737
final class Search {
3838

39-
private static final String usage = "USAGE: Search -R <configuration.xml> [-d | -r | -p | -h | -f] 'query string' ..\n" +
39+
private static final String usage = "USAGE: Search -R <configuration.xml> [-d | -r | -p | -h | -f | -t] 'query string' ..\n" +
4040
"\t -R <configuration.xml> Read configuration from the specified file\n" +
4141
"\t -d Symbol Definitions\n" +
4242
"\t -r Symbol References\n" +
4343
"\t -p Path\n" +
4444
"\t -h History\n" +
45-
"\t -f Full text";
45+
"\t -f Full text\n" +
46+
"\t -t File Type";
4647

4748
private SearchEngine engine;
4849
final List<Hit> results = new ArrayList<Hit>();
@@ -52,7 +53,7 @@ final class Search {
5253
@SuppressWarnings({"PMD.SwitchStmtsShouldHaveDefault"})
5354
protected boolean parseCmdLine(String[] argv) {
5455
engine = new SearchEngine();
55-
Getopt getopt = new Getopt(argv, "R:d:r:p:h:f:");
56+
Getopt getopt = new Getopt(argv, "R:d:r:p:h:f:t:");
5657
try {
5758
getopt.parse();
5859
} catch (Exception e) {
@@ -88,6 +89,9 @@ protected boolean parseCmdLine(String[] argv) {
8889
case 'f':
8990
engine.setFreetext(getopt.getOptarg());
9091
break;
92+
case 't':
93+
engine.setFileType(getopt.getOptarg());
94+
break;
9195
}
9296
}
9397

src/org/opensolaris/opengrok/search/SearchEngine.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ public class SearchEngine {
9595
* Holds value of property symbol.
9696
*/
9797
private String symbol;
98+
/**
99+
* Holds value of property fileType
100+
*/
101+
private String fileType;
98102
/**
99103
* Holds value of property indexDatabase.
100104
*/
@@ -136,7 +140,8 @@ private QueryBuilder createQueryBuilder() {
136140
.setDefs(definition)
137141
.setRefs(symbol)
138142
.setPath(file)
139-
.setHist(history);
143+
.setHist(history)
144+
.setFileType(fileType);
140145
}
141146

142147
public boolean isValidQuery() {
@@ -488,4 +493,22 @@ public String getSymbol() {
488493
public void setSymbol(String symbol) {
489494
this.symbol = symbol;
490495
}
496+
497+
/**
498+
* Getter for property fileType.
499+
*
500+
* @return Value of property fileType.
501+
*/
502+
public String getFileType() {
503+
return this.fileType;
504+
}
505+
506+
/**
507+
* Setter for property fileType.
508+
*
509+
* @param fileType New value of property fileType.
510+
*/
511+
public void setFileType(String fileType) {
512+
this.fileType = fileType;
513+
}
491514
}

0 commit comments

Comments
 (0)