Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally show surrounding context during search #3857

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ private static Reader getXrefReader(
/**
* Prints out results in html form. The following search helper fields are
* required to be properly initialized: <ul>
* <li>{@link SearchHelper#dataRoot}</li>
* <li>{@link SearchHelper#contextPath}</li>
* <li>{@link SearchHelper#searcher}</li> <li>{@link SearchHelper#hits}</li>
* <li>{@link SearchHelper#historyContext} (ignored if {@code null})</li>
* <li>{@link SearchHelper#sourceContext} (ignored if {@code null})</li>
* <li>{@link SearchHelper#summarizer} (if sourceContext is not
* {@code null})</li> <li>{@link SearchHelper#sourceRoot} (if
* <li>{@link SearchHelper#getDataRoot()}</li>
* <li>{@link SearchHelper#getContextPath()}</li>
* <li>{@link SearchHelper#getSearcher()}</li> <li>{@link SearchHelper#getHits()}</li>
* <li>{@link SearchHelper#getHistoryContext()} (ignored if {@code null})</li>
* <li>{@link SearchHelper#getSourceContext()} (ignored if {@code null})</li>
* <li>{@link SearchHelper#getSummarizer()} (if sourceContext is not
* {@code null})</li> <li>{@link SearchHelper#getSourceRoot()} (if
* sourceContext or historyContext is not {@code null})</li> </ul>
*
* @param out write destination
Expand Down Expand Up @@ -271,9 +271,9 @@ private static void printPlain(PrintPlainFinalArgs fargs, Document doc,

fargs.shelp.getSourceContext().toggleAlt();

boolean didPresentNew = fargs.shelp.getSourceContext().getContext2(fargs.env,
boolean didPresentNew = fargs.shelp.getSourceContext().getContext2(
fargs.shelp.getSearcher(), docId, fargs.out, fargs.xrefPrefix,
fargs.morePrefix, true, fargs.tabSize);
fargs.morePrefix, fargs.shelp.getLimitedContextArgs(), fargs.tabSize);

if (!didPresentNew) {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.opengrok.indexer.search.Hit;
import org.opengrok.indexer.search.QueryBuilder;
import org.opengrok.indexer.util.IOUtils;
import org.opengrok.indexer.web.QueryParameters;
import org.opengrok.indexer.web.Util;

/**
Expand Down Expand Up @@ -112,26 +113,19 @@ public boolean isEmpty() {
/**
* Look for context for this instance's initialized query in a search result
* {@link Document}, and output according to the parameters.
* @param env required environment
* @param searcher required search that produced the document
* @param docId document ID for producing context
* @param dest required target to write
* @param urlPrefix prefix for links
* @param morePrefix optional link to more... page
* @param limit a value indicating if the number of matching lines should be
* limited. N.b. unlike
* {@link #getContext(java.io.Reader, java.io.Writer, java.lang.String, java.lang.String, java.lang.String,
* org.opengrok.indexer.analysis.Definitions, boolean, boolean, java.util.List, org.opengrok.indexer.analysis.Scopes)},
* the {@code limit} argument will not be interpreted w.r.t.
* {@link RuntimeEnvironment#isQuickContextScan()}.
* @param contextArgs a value specifying how much context to show
* @param tabSize optional positive tab size that must accord with the value
* used when indexing or else postings may be wrongly shifted until
* re-indexing
* @return Did it get any matching context?
*/
public boolean getContext2(RuntimeEnvironment env, IndexSearcher searcher,
int docId, Appendable dest, String urlPrefix, String morePrefix,
boolean limit, int tabSize) {
public boolean getContext2(IndexSearcher searcher, int docId, Appendable dest,
String urlPrefix, String morePrefix, ContextArgs contextArgs, int tabSize) {

if (isEmpty()) {
return false;
Expand Down Expand Up @@ -178,35 +172,29 @@ public boolean getContext2(RuntimeEnvironment env, IndexSearcher searcher,

String path = doc.get(QueryBuilder.PATH);
String pathE = Util.uriEncodePath(path);
String urlPrefixE = urlPrefix == null ? "" : Util.uriEncodePath(urlPrefix);
String moreURL = morePrefix == null ? null : Util.uriEncodePath(morePrefix) + pathE + "?" + queryAsURI;
String urlPrefixE = urlPrefix == null ? "" : Util.uriEncodePath(
urlPrefix);
String moreURL = morePrefix == null ? null : Util.uriEncodePath(morePrefix) + pathE + "?" +
QueryParameters.CONTEXT_SURROUND_PARAM_EQ + contextArgs.getContextSurround() +
"&" + queryAsURI;

ContextArgs args = new ContextArgs(env.getContextSurround(), env.getContextLimit());
/*
* Lucene adds to the following value in FieldHighlighter, so avoid
* integer overflow by not using Integer.MAX_VALUE -- Short is good
* enough.
*/
int linelimit = limit ? args.getContextLimit() : Short.MAX_VALUE;

ContextFormatter formatter = new ContextFormatter(args);
ContextFormatter formatter = new ContextFormatter(contextArgs);
formatter.setUrl(urlPrefixE + pathE);
formatter.setDefs(tags);
formatter.setScopes(scopes);
formatter.setMoreUrl(moreURL);
formatter.setMoreLimit(linelimit);

OGKUnifiedHighlighter uhi = new OGKUnifiedHighlighter(env, searcher, anz);
OGKUnifiedHighlighter uhi = new OGKUnifiedHighlighter(searcher, anz);
uhi.setBreakIterator(StrictLineBreakIterator::new);
uhi.setFormatter(formatter);
uhi.setTabSize(tabSize);
uhi.setContextArgs(contextArgs);

try {
List<String> fieldList = qbuilder.getContextFields();
String[] fields = fieldList.toArray(new String[0]);

String res = uhi.highlightFieldsUnion(fields, query, docId,
linelimit);
String res = uhi.highlightFieldsUnion(fields, query, docId);
if (res != null) {
dest.append(res);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
* to producing context presentations.
*/
public class ContextArgs {
/**
* The maximum number of lines of surrounding context in a single direction
* to allow during a search. If there is sufficient content both preceding
* and following a hit, then the total context might be twice this value.
*/
public static final short MAX_CONTEXT_SURROUND = 4;

/** Not Lucene-related, so {@code int} does fine. */
private static final int CONTEXT_WIDTH = 100;

Expand Down
Loading