use of org.opengrok.indexer.search.QueryBuilder in project OpenGrok by OpenGrok.
the class SearchHelper method prepareExec.
/**
* Create the searcher to use w.r.t. currently set parameters and the given
* projects. Does not produce any {@link #redirect} link. It also does
* nothing if {@link #redirect} or {@link #errorMsg} have a
* none-{@code null} value.
* <p>
* Parameters which should be populated/set at this time:
* <ul>
* <li>{@link #builder}</li> <li>{@link #dataRoot}</li>
* <li>{@link #order} (falls back to relevance if unset)</li>
* </ul>
* Populates/sets:
* <ul>
* <li>{@link #query}</li> <li>{@link #searcher}</li> <li>{@link #sort}</li>
* <li>{@link #projects}</li> <li>{@link #errorMsg} if an error occurs</li>
* </ul>
*
* @param projects project names. If empty, a no-project setup
* is assumed (i.e. DATA_ROOT/index will be used instead of possible
* multiple DATA_ROOT/$project/index). If the set contains projects
* not known in the configuration or projects not yet indexed,
* an error will be returned in {@link #errorMsg}.
* @return this instance
*/
public SearchHelper prepareExec(SortedSet<String> projects) {
if (redirect != null || errorMsg != null) {
return this;
}
settingsHelper = null;
// the Query created by the QueryBuilder
try {
indexDir = new File(dataRoot, IndexDatabase.INDEX_DIR);
query = builder.build();
if (projects == null) {
errorMsg = "No project selected!";
return this;
}
this.projects = projects;
if (projects.isEmpty()) {
// no project setup
FSDirectory dir = FSDirectory.open(indexDir.toPath());
reader = DirectoryReader.open(dir);
searcher = new IndexSearcher(reader);
closeOnDestroy = true;
} else {
// Check list of project names first to make sure all of them
// are valid and indexed.
closeOnDestroy = false;
Set<String> invalidProjects = projects.stream().filter(proj -> (Project.getByName(proj) == null)).collect(Collectors.toSet());
if (!invalidProjects.isEmpty()) {
errorMsg = "Project list contains invalid projects: " + String.join(", ", invalidProjects);
return this;
}
Set<Project> notIndexedProjects = projects.stream().map(Project::getByName).filter(proj -> !proj.isIndexed()).collect(Collectors.toSet());
if (!notIndexedProjects.isEmpty()) {
errorMsg = "Some of the projects to be searched are not indexed yet: " + String.join(", ", notIndexedProjects.stream().map(Project::getName).collect(Collectors.toSet()));
return this;
}
// We use MultiReader even for single project. This should
// not matter given that MultiReader is just a cheap wrapper
// around set of IndexReader objects.
reader = RuntimeEnvironment.getInstance().getMultiReader(projects, searcherList);
if (reader != null) {
searcher = new IndexSearcher(reader);
} else {
errorMsg = "Failed to initialize search. Check the index";
if (!projects.isEmpty()) {
errorMsg += " for projects: " + String.join(", ", projects);
}
return this;
}
}
// Most probably they are not reused. SearcherLifetimeManager might help here.
switch(order) {
case LASTMODIFIED:
sort = new Sort(new SortField(QueryBuilder.DATE, SortField.Type.STRING, true));
break;
case BY_PATH:
sort = new Sort(new SortField(QueryBuilder.FULLPATH, SortField.Type.STRING));
break;
default:
sort = Sort.RELEVANCE;
break;
}
checker = new DirectSpellChecker();
} catch (ParseException e) {
errorMsg = PARSE_ERROR_MSG + e.getMessage();
} catch (FileNotFoundException e) {
errorMsg = "Index database not found. Check the index";
if (!projects.isEmpty()) {
errorMsg += " for projects: " + String.join(", ", projects);
}
errorMsg += "; " + e.getMessage();
} catch (IOException e) {
errorMsg = e.getMessage();
}
return this;
}
use of org.opengrok.indexer.search.QueryBuilder in project OpenGrok by OpenGrok.
the class SearchHelper method searchSingle.
/**
* Searches for a document for a single file from the index.
* @param file the file whose definitions to find
* @return {@link ScoreDoc#doc} or -1 if it could not be found
* @throws IOException if an error happens when accessing the index
* @throws ParseException if an error happens when building the Lucene query
*/
public int searchSingle(File file) throws IOException, ParseException {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
String path;
try {
path = env.getPathRelativeToSourceRoot(file);
} catch (ForbiddenSymlinkException e) {
LOGGER.log(Level.FINER, e.getMessage());
return -1;
}
// sanitize windows path delimiters
// in order not to conflict with Lucene escape character
path = path.replace("\\", "/");
QueryBuilder singleBuilder = new QueryBuilder();
if (builder != null) {
singleBuilder.reset(builder);
}
query = singleBuilder.setPath(path).build();
TopDocs top = searcher.search(query, 1);
if (top.totalHits.value == 0) {
return -1;
}
int docID = top.scoreDocs[0].doc;
Document doc = searcher.doc(docID);
String foundPath = doc.get(QueryBuilder.PATH);
// Only use the result if PATH matches exactly.
if (!path.equals(foundPath)) {
return -1;
}
return docID;
}
use of org.opengrok.indexer.search.QueryBuilder in project OpenGrok by OpenGrok.
the class IndexDatabase method getDocument.
/**
* @param file File object of a file under source root
* @return Document object for the file or {@code null}
* @throws IOException on I/O error
* @throws ParseException on problem with building Query
*/
public static Document getDocument(File file) throws IOException, ParseException {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
String path;
try {
path = env.getPathRelativeToSourceRoot(file);
} catch (ForbiddenSymlinkException e) {
LOGGER.log(Level.FINER, e.getMessage());
return null;
}
// Sanitize Windows path delimiters in order not to conflict with Lucene escape character.
path = path.replace("\\", "/");
try (IndexReader ireader = getIndexReader(path)) {
if (ireader == null) {
// No index, no document..
return null;
}
Document doc;
Query q = new QueryBuilder().setPath(path).build();
IndexSearcher searcher = new IndexSearcher(ireader);
Statistics stat = new Statistics();
TopDocs top = searcher.search(q, 1);
stat.report(LOGGER, Level.FINEST, "search via getDocument done", "search.latency", new String[] { "category", "getdocument", "outcome", top.totalHits.value == 0 ? "empty" : "success" });
if (top.totalHits.value == 0) {
// No hits, no document...
return null;
}
doc = searcher.doc(top.scoreDocs[0].doc);
String foundPath = doc.get(QueryBuilder.PATH);
// Only use the document if we found an exact match.
if (!path.equals(foundPath)) {
return null;
}
return doc;
}
}
use of org.opengrok.indexer.search.QueryBuilder in project OpenGrok by OpenGrok.
the class ContextTest method testAllLinkWithLongLines.
/**
* Test that we get the [all...] link if a very long line crosses the buffer
* boundary. Bug 383.
* @throws org.apache.lucene.queryparser.classic.ParseException parse exception
*/
@Test
public void testAllLinkWithLongLines() throws ParseException {
// Create input which consists of one single line longer than
// Context.MAXFILEREAD.
StringBuilder sb = new StringBuilder();
sb.append("search_for_me");
while (sb.length() <= Context.MAXFILEREAD) {
sb.append(" more words");
}
Reader in = new StringReader(sb.toString());
StringWriter out = new StringWriter();
QueryBuilder qb = new QueryBuilder().setFreetext("search_for_me");
Context c = new Context(qb.build(), qb);
boolean match = c.getContext(in, out, "", "", "", null, true, qb.isDefSearch(), null);
assertTrue(match, "No match found");
String s = out.toString();
assertTrue(s.contains(">[all...]</a>"), "No [all...] link");
}
use of org.opengrok.indexer.search.QueryBuilder in project OpenGrok by OpenGrok.
the class ContextTest method searchContextTestHelper.
/**
* Helper method for testing presence of expected words in search context.
* @param searchInText Context of document we are searching in.
* @param queryString Definition of search query.
* @param expectWordInContext Word expected to be found by 'queryString' in
* 'searchInText' and to be included in context. Set null if context is
* expected to be empty.
* @throws ParseException parse exception
*/
private void searchContextTestHelper(String searchInText, String queryString, String expectWordInContext) throws ParseException {
Reader in = new StringReader(searchInText);
StringWriter out = new StringWriter();
QueryBuilder qb = new QueryBuilder().setFreetext(queryString);
Context c = new Context(qb.build(), qb);
boolean match = c.getContext(in, out, "", "", "", null, true, qb.isDefSearch(), null);
if (expectWordInContext == null) {
assertFalse(match, "Match found");
} else {
assertTrue(match, "No match found");
String s = out.toString();
assertTrue(s.contains("<b>" + expectWordInContext + "</b>"), "Searched word '" + expectWordInContext + "' not in context");
}
}
Aggregations