Search in sources :

Example 1 with QueryBuilder

use of org.opensolaris.opengrok.search.QueryBuilder in project OpenGrok by OpenGrok.

the class IndexDatabase method getDefinitions.

/**
     * Get the latest definitions for a file from the index.
     *
     * @param file the file whose definitions to find
     * @return definitions for the file, or {@code null} if they 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
     * @throws ClassNotFoundException if the class for the stored definitions
     * instance cannot be found
     */
public static Definitions getDefinitions(File file) throws IOException, ParseException, ClassNotFoundException {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    String path = env.getPathRelativeToSourceRoot(file, 0);
    //sanitize windows path delimiters
    //in order not to conflict with Lucene escape character
    path = path.replace("\\", "/");
    IndexReader ireader = getIndexReader(path);
    if (ireader == null) {
        // No index, no definitions...
        return null;
    }
    try {
        Query q = new QueryBuilder().setPath(path).build();
        IndexSearcher searcher = new IndexSearcher(ireader);
        TopDocs top = searcher.search(q, 1);
        if (top.totalHits == 0) {
            // No hits, no definitions...
            return null;
        }
        Document doc = searcher.doc(top.scoreDocs[0].doc);
        String foundPath = doc.get(QueryBuilder.PATH);
        // Only use the definitions if we found an exact match.
        if (path.equals(foundPath)) {
            IndexableField tags = doc.getField(QueryBuilder.TAGS);
            if (tags != null) {
                return Definitions.deserialize(tags.binaryValue().bytes);
            }
        }
    } finally {
        ireader.close();
    }
    // Didn't find any definitions.
    return null;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) IndexableField(org.apache.lucene.index.IndexableField) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) Query(org.apache.lucene.search.Query) IndexReader(org.apache.lucene.index.IndexReader) QueryBuilder(org.opensolaris.opengrok.search.QueryBuilder) Document(org.apache.lucene.document.Document)

Example 2 with QueryBuilder

use of org.opensolaris.opengrok.search.QueryBuilder in project OpenGrok by OpenGrok.

the class ContextTest method testMultiLineMatch.

/**
     * Test that valid HTML is generated for a match that spans multiple lines.
     * It used to nest the tags incorrectly. Bug #15632.
     *
     * @throws java.lang.Exception
     */
@Test
public void testMultiLineMatch() throws Exception {
    StringReader in = new StringReader("a\nb\nc\n");
    StringWriter out = new StringWriter();
    // XML boilerplate
    out.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    out.append("<document>\n");
    // Search for a multi-token phrase that spans multiple lines in the
    // input file. The generated HTML fragment is inserted inside a root
    // element so that the StringWriter contains a valid XML document.
    QueryBuilder qb = new QueryBuilder().setFreetext("\"a b c\"");
    Context c = new Context(qb.build(), qb.getQueries());
    assertTrue("No match found", c.getContext(in, out, "", "", "", null, true, qb.isDefSearch(), null));
    // Close the XML document body
    out.append("\n</document>");
    // Check that valid XML was generated. This call used to fail with
    // SAXParseException: [Fatal Error] :3:55: The element type "b" must
    // be terminated by the matching end-tag "</b>".
    assertNotNull(parseXML(out.toString()));
}
Also used : StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) QueryBuilder(org.opensolaris.opengrok.search.QueryBuilder) Test(org.junit.Test)

Example 3 with QueryBuilder

use of org.opensolaris.opengrok.search.QueryBuilder in project OpenGrok by OpenGrok.

the class ContextTest method testLongLineNearBufferBoundary.

/**
     * Test that we don't get an {@code ArrayIndexOutOfBoundsException} when a
     * long (&gt;100 characters) line which contains a match is not terminated
     * with a newline character before the buffer boundary. Bug #383.
     *
     * @throws org.apache.lucene.queryparser.classic.ParseException
     */
@Test
public void testLongLineNearBufferBoundary() throws ParseException {
    char[] chars = new char[Context.MAXFILEREAD];
    Arrays.fill(chars, 'a');
    char[] substring = " this is a test ".toCharArray();
    System.arraycopy(substring, 0, chars, Context.MAXFILEREAD - substring.length, substring.length);
    Reader in = new CharArrayReader(chars);
    QueryBuilder qb = new QueryBuilder().setFreetext("test");
    Context c = new Context(qb.build(), qb.getQueries());
    StringWriter out = new StringWriter();
    boolean match = c.getContext(in, out, "", "", "", null, true, qb.isDefSearch(), null);
    assertTrue("No match found", match);
    String s = out.toString();
    assertTrue("Match not written to Writer", s.contains(" this is a <b>test</b>"));
    assertTrue("No match on line #1", s.contains("href=\"#1\""));
}
Also used : CharArrayReader(java.io.CharArrayReader) StringWriter(java.io.StringWriter) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) StringReader(java.io.StringReader) QueryBuilder(org.opensolaris.opengrok.search.QueryBuilder) Test(org.junit.Test)

Example 4 with QueryBuilder

use of org.opensolaris.opengrok.search.QueryBuilder in project OpenGrok by OpenGrok.

the class ContextTest method bug17582.

/**
     * The results from mixed-case symbol search should contain tags.
     *
     * @throws java.lang.Exception
     */
@Test
public void bug17582() throws Exception {
    // Freetext search should match regardless of case
    bug17582(new QueryBuilder().setFreetext("Bug17582"), new int[] { 2, 3 }, new String[] { "type1", "type2" });
    // Defs search should only match if case matches
    bug17582(new QueryBuilder().setDefs("Bug17582"), new int[] { 3 }, new String[] { "type2" });
    // Refs search should only match if case matches
    bug17582(new QueryBuilder().setRefs("Bug17582"), new int[] { 3 }, new String[] { "type2" });
    // Path search shouldn't match anything in source
    bug17582(new QueryBuilder().setPath("Bug17582"), new int[0], new String[0]);
    // Refs should only match if case matches, but freetext will match
    // regardless of case
    bug17582(new QueryBuilder().setRefs("Bug17582").setFreetext("Bug17582"), new int[] { 2, 3 }, new String[] { "type1", "type2" });
    // Refs should only match if case matches, hist shouldn't match
    // anything in source
    bug17582(new QueryBuilder().setRefs("Bug17582").setHist("bug17582"), new int[] { 3 }, new String[] { "type2" });
}
Also used : QueryBuilder(org.opensolaris.opengrok.search.QueryBuilder) Test(org.junit.Test)

Example 5 with QueryBuilder

use of org.opensolaris.opengrok.search.QueryBuilder in project OpenGrok by OpenGrok.

the class ContextTest method testIsEmpty.

/**
     * Tests for the isEmpty() method.
     *
     * @throws org.apache.lucene.queryparser.classic.ParseException
     */
@Test
public void testIsEmpty() throws ParseException {
    String term = "qwerty";
    // Definition search should be used
    QueryBuilder qb = new QueryBuilder().setDefs(term);
    Context c = new Context(qb.build(), qb.getQueries());
    assertFalse(c.isEmpty());
    // Symbol search should be used
    qb = new QueryBuilder().setRefs(term);
    c = new Context(qb.build(), qb.getQueries());
    assertFalse(c.isEmpty());
    // Full search should be used
    qb = new QueryBuilder().setFreetext(term);
    c = new Context(qb.build(), qb.getQueries());
    assertFalse(c.isEmpty());
    // History search should not be used
    qb = new QueryBuilder().setHist(term);
    c = new Context(qb.build(), qb.getQueries());
    assertTrue(c.isEmpty());
    // Path search should not be used
    qb = new QueryBuilder().setPath(term);
    c = new Context(qb.build(), qb.getQueries());
    assertTrue(c.isEmpty());
    // Combined search should be fine
    qb = new QueryBuilder().setHist(term).setFreetext(term);
    c = new Context(qb.build(), qb.getQueries());
    assertFalse(c.isEmpty());
}
Also used : QueryBuilder(org.opensolaris.opengrok.search.QueryBuilder) Test(org.junit.Test)

Aggregations

QueryBuilder (org.opensolaris.opengrok.search.QueryBuilder)10 StringReader (java.io.StringReader)7 StringWriter (java.io.StringWriter)7 Test (org.junit.Test)7 CharArrayReader (java.io.CharArrayReader)4 Reader (java.io.Reader)4 ArrayList (java.util.ArrayList)1 Document (org.apache.lucene.document.Document)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexableField (org.apache.lucene.index.IndexableField)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 Query (org.apache.lucene.search.Query)1 TopDocs (org.apache.lucene.search.TopDocs)1 Definitions (org.opensolaris.opengrok.analysis.Definitions)1 RuntimeEnvironment (org.opensolaris.opengrok.configuration.RuntimeEnvironment)1 Hit (org.opensolaris.opengrok.search.Hit)1