Search in sources :

Example 6 with Scopes

use of org.opensolaris.opengrok.analysis.Scopes in project OpenGrok by OpenGrok.

the class SearchEngine method results.

/**
     * get results , if no search was started before, no results are returned
     * this method will requery if end is more than first query from search,
     * hence performance hit applies, if you want results in later pages than
     * number of cachePages also end has to be bigger than start !
     *
     * @param start start of the hit list
     * @param end end of the hit list
     * @param ret list of results from start to end or null/empty if no search
     * was started
     */
public void results(int start, int end, List<Hit> ret) {
    //return if no start search() was done
    if (hits == null || (end < start)) {
        ret.clear();
        return;
    }
    ret.clear();
    //TODO check if below fits for if end=old hits.length, or it should include it
    if (end > hits.length & !allCollected) {
        //do the requery, we want more than 5 pages
        collector = TopScoreDocCollector.create(totalHits);
        try {
            searcher.search(query, collector);
        } catch (Exception e) {
            // this exception should never be hit, since search() will hit this before
            LOGGER.log(Level.WARNING, SEARCH_EXCEPTION_MSG, e);
        }
        hits = collector.topDocs().scoreDocs;
        Document d = null;
        for (int i = start; i < hits.length; i++) {
            int docId = hits[i].doc;
            try {
                d = searcher.doc(docId);
            } catch (Exception e) {
                LOGGER.log(Level.SEVERE, SEARCH_EXCEPTION_MSG, e);
            }
            docs.add(d);
        }
        allCollected = true;
    }
    // the only problem is that count of docs is usually smaller than number of results
    for (int ii = start; ii < end; ++ii) {
        boolean alt = (ii % 2 == 0);
        boolean hasContext = false;
        try {
            Document doc = docs.get(ii);
            String filename = doc.get(QueryBuilder.PATH);
            Genre genre = Genre.get(doc.get(QueryBuilder.T));
            Definitions tags = null;
            IndexableField tagsField = doc.getField(QueryBuilder.TAGS);
            if (tagsField != null) {
                tags = Definitions.deserialize(tagsField.binaryValue().bytes);
            }
            Scopes scopes = null;
            IndexableField scopesField = doc.getField(QueryBuilder.SCOPES);
            if (scopesField != null) {
                scopes = Scopes.deserialize(scopesField.binaryValue().bytes);
            }
            int nhits = docs.size();
            if (sourceContext != null) {
                try {
                    if (Genre.PLAIN == genre && (source != null)) {
                        hasContext = sourceContext.getContext(new InputStreamReader(new FileInputStream(source + filename)), null, null, null, filename, tags, nhits > 100, false, ret, scopes);
                    } else if (Genre.XREFABLE == genre && data != null && summarizer != null) {
                        int l;
                        try (Reader r = RuntimeEnvironment.getInstance().isCompressXref() ? new HTMLStripCharFilter(new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(data + Prefix.XREF_P + filename + ".gz"))))) : new HTMLStripCharFilter(new BufferedReader(new FileReader(data + Prefix.XREF_P + filename)))) {
                            l = r.read(content);
                        }
                        //TODO FIX below fragmenter according to either summarizer or context (to get line numbers, might be hard, since xref writers will need to be fixed too, they generate just one line of html code now :( )
                        Summary sum = summarizer.getSummary(new String(content, 0, l));
                        Fragment[] fragments = sum.getFragments();
                        for (int jj = 0; jj < fragments.length; ++jj) {
                            String match = fragments[jj].toString();
                            if (match.length() > 0) {
                                if (!fragments[jj].isEllipsis()) {
                                    Hit hit = new Hit(filename, fragments[jj].toString(), "", true, alt);
                                    ret.add(hit);
                                }
                                hasContext = true;
                            }
                        }
                    } else {
                        LOGGER.log(Level.WARNING, "Unknown genre: {0} for {1}", new Object[] { genre, filename });
                        hasContext |= sourceContext.getContext(null, null, null, null, filename, tags, false, false, ret, scopes);
                    }
                } catch (FileNotFoundException exp) {
                    LOGGER.log(Level.WARNING, "Couldn''t read summary from {0} ({1})", new Object[] { filename, exp.getMessage() });
                    hasContext |= sourceContext.getContext(null, null, null, null, filename, tags, false, false, ret, scopes);
                }
            }
            if (historyContext != null) {
                hasContext |= historyContext.getContext(source + filename, filename, ret);
            }
            if (!hasContext) {
                ret.add(new Hit(filename, "...", "", false, alt));
            }
        } catch (IOException | ClassNotFoundException | HistoryException e) {
            LOGGER.log(Level.WARNING, SEARCH_EXCEPTION_MSG, e);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Definitions(org.opensolaris.opengrok.analysis.Definitions) HistoryException(org.opensolaris.opengrok.history.HistoryException) FileNotFoundException(java.io.FileNotFoundException) MultiReader(org.apache.lucene.index.MultiReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IndexReader(org.apache.lucene.index.IndexReader) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) ParseException(org.apache.lucene.queryparser.classic.ParseException) HistoryException(org.opensolaris.opengrok.history.HistoryException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) IndexableField(org.apache.lucene.index.IndexableField) GZIPInputStream(java.util.zip.GZIPInputStream) Scopes(org.opensolaris.opengrok.analysis.Scopes) BufferedReader(java.io.BufferedReader) HTMLStripCharFilter(org.apache.lucene.analysis.charfilter.HTMLStripCharFilter) FileReader(java.io.FileReader) Genre(org.opensolaris.opengrok.analysis.FileAnalyzer.Genre)

Example 7 with Scopes

use of org.opensolaris.opengrok.analysis.Scopes in project OpenGrok by OpenGrok.

the class CAnalyzerFactoryTest method testScopeAnalyzer.

/**
     * Test of writeXref method, of class CAnalyzerFactory.
     *
     * @throws java.lang.Exception
     */
@Test
public void testScopeAnalyzer() throws Exception {
    String path = repository.getSourceRoot() + "/c/sample.c";
    File f = new File(path);
    if (!(f.canRead() && f.isFile())) {
        fail("c testfile " + f + " not found");
    }
    Document doc = new Document();
    doc.add(new Field(QueryBuilder.FULLPATH, path, string_ft_nstored_nanalyzed_norms));
    StringWriter xrefOut = new StringWriter();
    analyzer.setCtags(ctags);
    analyzer.setScopesEnabled(true);
    analyzer.analyze(doc, getStreamSource(path), xrefOut);
    IndexableField scopesField = doc.getField(QueryBuilder.SCOPES);
    assertNotNull(scopesField);
    Scopes scopes = Scopes.deserialize(scopesField.binaryValue().bytes);
    Scope globalScope = scopes.getScope(-1);
    // foo, bar, main
    assertEquals(3, scopes.size());
    for (int i = 0; i < 50; ++i) {
        if (i >= 8 && i <= 22) {
            assertEquals("foo", scopes.getScope(i).getName());
            assertNull(scopes.getScope(i).getNamespace());
        } else if (i >= 24 && i <= 38) {
            assertEquals("bar", scopes.getScope(i).getName());
            assertNull(scopes.getScope(i).getNamespace());
        } else if (i >= 41 && i <= 48) {
            assertEquals("main", scopes.getScope(i).getName());
            assertNull(scopes.getScope(i).getNamespace());
        } else {
            assertEquals(scopes.getScope(i), globalScope);
            assertNull(scopes.getScope(i).getNamespace());
        }
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) IndexableField(org.apache.lucene.index.IndexableField) Field(org.apache.lucene.document.Field) StringWriter(java.io.StringWriter) Scope(org.opensolaris.opengrok.analysis.Scopes.Scope) Scopes(org.opensolaris.opengrok.analysis.Scopes) Document(org.apache.lucene.document.Document) File(java.io.File) Test(org.junit.Test)

Aggregations

Scopes (org.opensolaris.opengrok.analysis.Scopes)7 Document (org.apache.lucene.document.Document)6 IndexableField (org.apache.lucene.index.IndexableField)6 File (java.io.File)5 StringWriter (java.io.StringWriter)4 Field (org.apache.lucene.document.Field)4 Test (org.junit.Test)4 Scope (org.opensolaris.opengrok.analysis.Scopes.Scope)4 FileReader (java.io.FileReader)2 Reader (java.io.Reader)2 Definitions (org.opensolaris.opengrok.analysis.Definitions)2 Genre (org.opensolaris.opengrok.analysis.FileAnalyzer.Genre)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 DateFormat (java.text.DateFormat)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1