Search in sources :

Example 1 with PlainAnalyzerFactory

use of org.opengrok.indexer.analysis.plain.PlainAnalyzerFactory in project OpenGrok by OpenGrok.

the class Context method getContext2.

/**
 * 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 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) {
    if (isEmpty()) {
        return false;
    }
    Document doc;
    try {
        doc = searcher.doc(docId);
    } catch (IOException e) {
        LOGGER.log(Level.WARNING, "ERROR getting searcher doc(int)", e);
        return false;
    }
    Definitions tags = null;
    try {
        IndexableField tagsField = doc.getField(QueryBuilder.TAGS);
        if (tagsField != null) {
            tags = Definitions.deserialize(tagsField.binaryValue().bytes);
        }
    } catch (ClassNotFoundException | IOException e) {
        LOGGER.log(Level.WARNING, "ERROR Definitions.deserialize(...)", e);
        return false;
    }
    Scopes scopes;
    try {
        IndexableField scopesField = doc.getField(QueryBuilder.SCOPES);
        if (scopesField != null) {
            scopes = Scopes.deserialize(scopesField.binaryValue().bytes);
        } else {
            scopes = new Scopes();
        }
    } catch (ClassNotFoundException | IOException e) {
        LOGGER.log(Level.WARNING, "ERROR Scopes.deserialize(...)", e);
        return false;
    }
    /*
         * UnifiedHighlighter demands an analyzer "even if in some
         * circumstances it isn't used"; here it is not meant to be used.
         */
    PlainAnalyzerFactory fac = PlainAnalyzerFactory.DEFAULT_INSTANCE;
    AbstractAnalyzer anz = fac.getAnalyzer();
    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;
    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);
    formatter.setUrl(urlPrefixE + pathE);
    formatter.setDefs(tags);
    formatter.setScopes(scopes);
    formatter.setMoreUrl(moreURL);
    formatter.setMoreLimit(linelimit);
    OGKUnifiedHighlighter uhi = new OGKUnifiedHighlighter(env, searcher, anz);
    uhi.setBreakIterator(StrictLineBreakIterator::new);
    uhi.setFormatter(formatter);
    uhi.setTabSize(tabSize);
    try {
        List<String> fieldList = qbuilder.getContextFields();
        String[] fields = fieldList.toArray(new String[0]);
        String res = uhi.highlightFieldsUnion(fields, query, docId, linelimit);
        if (res != null) {
            dest.append(res);
            return true;
        }
    } catch (IOException e) {
        LOGGER.log(Level.WARNING, "ERROR highlightFieldsUnion(...)", e);
    // Continue below.
    } catch (Throwable e) {
        LOGGER.log(Level.SEVERE, "ERROR highlightFieldsUnion(...)", e);
        throw e;
    }
    return false;
}
Also used : Definitions(org.opengrok.indexer.analysis.Definitions) PlainAnalyzerFactory(org.opengrok.indexer.analysis.plain.PlainAnalyzerFactory) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) IndexableField(org.apache.lucene.index.IndexableField) Scopes(org.opengrok.indexer.analysis.Scopes) AbstractAnalyzer(org.opengrok.indexer.analysis.AbstractAnalyzer)

Example 2 with PlainAnalyzerFactory

use of org.opengrok.indexer.analysis.plain.PlainAnalyzerFactory in project OpenGrok by OpenGrok.

the class SearchAndContextFormatterTest method getFirstFragments.

private String[] getFirstFragments(SearchEngine instance) throws IOException {
    ContextArgs args = new ContextArgs((short) 1, (short) 10);
    /*
         * The following `anz' should go unused, but UnifiedHighlighter demands
         * an analyzer "even if in some circumstances it isn't used."
         */
    PlainAnalyzerFactory fac = PlainAnalyzerFactory.DEFAULT_INSTANCE;
    AbstractAnalyzer anz = fac.getAnalyzer();
    ContextFormatter formatter = new ContextFormatter(args);
    OGKUnifiedHighlighter uhi = new OGKUnifiedHighlighter(env, instance.getSearcher(), anz);
    uhi.setBreakIterator(StrictLineBreakIterator::new);
    uhi.setFormatter(formatter);
    ScoreDoc[] docs = instance.scoreDocs();
    for (ScoreDoc scoreDoc : docs) {
        int docid = scoreDoc.doc;
        Document doc = instance.doc(docid);
        String path = doc.get(QueryBuilder.PATH);
        System.out.println(path);
        formatter.setUrl("/source" + path);
        for (String contextField : instance.getQueryBuilder().getContextFields()) {
            Map<String, String[]> res = uhi.highlightFields(new String[] { contextField }, instance.getQueryObject(), new int[] { docid }, new int[] { 10 });
            String[] frags = res.getOrDefault(contextField, null);
            if (frags != null) {
                return frags;
            }
        }
    }
    return null;
}
Also used : PlainAnalyzerFactory(org.opengrok.indexer.analysis.plain.PlainAnalyzerFactory) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) AbstractAnalyzer(org.opengrok.indexer.analysis.AbstractAnalyzer)

Example 3 with PlainAnalyzerFactory

use of org.opengrok.indexer.analysis.plain.PlainAnalyzerFactory in project OpenGrok by OpenGrok.

the class SearchAndContextFormatterTest2 method getFirstFragments.

private String[] getFirstFragments(SearchEngine instance) throws IOException, InvalidTokenOffsetsException {
    ContextArgs args = new ContextArgs((short) 1, (short) 10);
    /*
         * The following `anz' should go unused, but UnifiedHighlighter demands
         * an analyzer "even if in some circumstances it isn't used."
         */
    PlainAnalyzerFactory fac = PlainAnalyzerFactory.DEFAULT_INSTANCE;
    AbstractAnalyzer anz = fac.getAnalyzer();
    ContextFormatter formatter = new ContextFormatter(args);
    OGKUnifiedHighlighter uhi = new OGKUnifiedHighlighter(env, instance.getSearcher(), anz);
    uhi.setBreakIterator(StrictLineBreakIterator::new);
    uhi.setFormatter(formatter);
    uhi.setTabSize(TABSIZE);
    ScoreDoc[] docs = instance.scoreDocs();
    for (ScoreDoc scoreDoc : docs) {
        int docid = scoreDoc.doc;
        Document doc = instance.doc(docid);
        String path = doc.get(QueryBuilder.PATH);
        System.out.println(path);
        formatter.setUrl("/source" + path);
        for (String contextField : instance.getQueryBuilder().getContextFields()) {
            Map<String, String[]> res = uhi.highlightFields(new String[] { contextField }, instance.getQueryObject(), new int[] { docid }, new int[] { 10 });
            String[] frags = res.getOrDefault(contextField, null);
            if (frags != null) {
                return frags;
            }
        }
    }
    return null;
}
Also used : PlainAnalyzerFactory(org.opengrok.indexer.analysis.plain.PlainAnalyzerFactory) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) AbstractAnalyzer(org.opengrok.indexer.analysis.AbstractAnalyzer)

Aggregations

Document (org.apache.lucene.document.Document)3 AbstractAnalyzer (org.opengrok.indexer.analysis.AbstractAnalyzer)3 PlainAnalyzerFactory (org.opengrok.indexer.analysis.plain.PlainAnalyzerFactory)3 ScoreDoc (org.apache.lucene.search.ScoreDoc)2 IOException (java.io.IOException)1 IndexableField (org.apache.lucene.index.IndexableField)1 Definitions (org.opengrok.indexer.analysis.Definitions)1 Scopes (org.opengrok.indexer.analysis.Scopes)1