Package org.opensolaris.opengrok.search

Examples of org.opensolaris.opengrok.search.QueryBuilder


            // 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;
View Full Code Here


    @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());
    }
View Full Code Here

        List<Hit> hits = hitList ? new ArrayList<Hit>() : null;

        RuntimeEnvironment.getInstance().setQuickContextScan(limit);

        // Search freetext for the term "def"
        QueryBuilder qb = new QueryBuilder().setFreetext("def");
        Context c = new Context(qb.build(), qb.getQueries());
        assertTrue(c.getContext(in, out, "", "", "", null, limit, qb.isDefSearch(), hits));

        if (hitList) {
            assertEquals(1, hits.size());
            assertEquals("1", hits.get(0).getLineno());
        }

        String expectedOutput = hitList
                ? "abc <b>def</b> ghi"
                : "<a class=\"s\" href=\"#1\"><span class=\"l\">1</span> "
                + "abc <b>def</b> ghi</a><br/>";

        String actualOutput = hitList ? hits.get(0).getLine() : out.toString();

        assertEquals(expectedOutput, actualOutput);

        // Search with definitions
        Definitions defs = new Definitions();
        defs.addTag(1, "def", "type", "text");
        in = new StringReader("abc def ghi\n");
        out = hitList ? null : new StringWriter();
        hits = hitList ? new ArrayList<Hit>() : null;
        qb = new QueryBuilder().setDefs("def");
        c = new Context(qb.build(), qb.getQueries());
        assertTrue(c.getContext(in, out, "", "", "", defs, limit, qb.isDefSearch(), hits));

        if (hitList) {
            assertEquals(1, hits.size());
            assertEquals("1", hits.get(0).getLineno());
        }

        expectedOutput = hitList
                ? "abc <b>def</b> ghi"
                : "<a class=\"s\" href=\"#1\"><span class=\"l\">1</span> "
                + "abc <b>def</b> ghi</a> <i> type</i> <br/>";
        actualOutput = hitList ? hits.get(0).getLine() : out.toString();
        assertEquals(expectedOutput, actualOutput);
       
        in = new StringReader("abc def ghi\nbah def foobar");
        out = hitList ? null : new StringWriter();
        hits = hitList ? new ArrayList<Hit>() : null;
        assertTrue(c.getContext(in, out, "", "", "", defs, limit, qb.isDefSearch(), hits));

        if (hitList) {
            assertEquals(1, hits.size());
            assertEquals("1", hits.get(0).getLineno());
        }
       
        //test case - if this is def search, don't show false results (defs
        // weren't defined)
        assertEquals(expectedOutput, actualOutput);       
                       
        // Search with no input (will search definitions)
        in = null;
        out = hitList ? null : new StringWriter();
        hits = hitList ? new ArrayList<Hit>() : null;
        qb = new QueryBuilder().setDefs("def");
        c = new Context(qb.build(), qb.getQueries());
        assertTrue(c.getContext(in, out, "", "", "", defs, limit, qb.isDefSearch(), hits));

        if (hitList) {
            assertEquals(1, hits.size());
            assertEquals("1", hits.get(0).getLineno());
        }

        expectedOutput = hitList
                ? "text"
                : "<a class=\"s\" href=\"#1\"><span class=\"l\">1</span> "
                + "text</a> <i>type</i><br/>";
        actualOutput = hitList ? hits.get(0).getLine() : out.toString();
        assertEquals(expectedOutput, actualOutput);
       
        defs = new Definitions();
        defs.addTag(2, "def", "type", "text");
        in = new StringReader("abc1 def ghi\nabc def ghi\nabc3 def ghi\n");
        out = hitList ? null : new StringWriter();
        hits = hitList ? new ArrayList<Hit>() : null;
        qb = new QueryBuilder().setDefs("def");
        c = new Context(qb.build(), qb.getQueries());
        assertTrue(c.getContext(in, out, "", "", "", defs, limit, qb.isDefSearch(), hits));

        if (hitList) {
            assertEquals(1, hits.size());
            assertEquals("2", hits.get(0).getLineno());
        }

        expectedOutput = hitList
                ? "abc <b>def</b> ghi"
                : "<a class=\"s\" href=\"#2\"><span class=\"l\">2</span> "
                + "abc <b>def</b> ghi</a> <i> type</i> <br/>";
        actualOutput = hitList ? hits.get(0).getLine() : out.toString();
        assertEquals(expectedOutput, actualOutput);

        // Search with no results
        in = new StringReader("abc def ghi\n");
        out = hitList ? null : new StringWriter();
        hits = hitList ? new ArrayList<Hit>() : null;
        qb = new QueryBuilder().setFreetext("no_match");
        c = new Context(qb.build(), qb.getQueries());
        assertFalse(c.getContext(in, out, "", "", "", null, limit, qb.isDefSearch(), hits));
        if (hitList) {
            assertEquals(0, hits.size());
        } else {
            assertEquals("", out.toString());
        }

        // History search (should not show source context)
        in = new StringReader("abc def ghi\n");
        out = hitList ? null : new StringWriter();
        hits = hitList ? new ArrayList<Hit>() : null;
        qb = new QueryBuilder().setHist("abc");
        c = new Context(qb.build(), qb.getQueries());
        assertFalse(c.getContext(in, out, "", "", "", null, limit, qb.isDefSearch(), hits));
        if (hitList) {
            assertEquals(0, hits.size());
        } else {
            assertEquals("", out.toString());
        }
View Full Code Here

        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\""));
View Full Code Here

            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.getQueries());

        boolean match =
                c.getContext(in, out, "", "", "", null, true, qb.isDefSearch(), null);
        assertTrue("No match found", match);
        String s = out.toString();
        assertTrue("No [all...] link", s.contains(">[all...]</a>"));
    }
View Full Code Here

        sb.append("should not be found");

        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.getQueries());

        boolean match =
                c.getContext(in, out, "", "", "", null, true, qb.isDefSearch(), null);
        assertTrue("No match found", match);
        String s = out.toString();
        assertTrue("Match not listed", s.contains("<b>search_for_me</b>"));
        assertFalse("Line not truncated", s.contains("should not be found"));
        assertTrue("Ellipsis not found", s.contains("&hellip;"));
View Full Code Here

        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
View Full Code Here

     */
    @Test
    public void bug16848() throws Exception {
        StringReader in = new StringReader("Mixed case: abc AbC dEf\n");
        StringWriter out = new StringWriter();
        QueryBuilder qb = new QueryBuilder().setFreetext("mixed");
        Context c = new Context(qb.build(), qb.getQueries());
        assertTrue(c.getContext(in, out, "", "", "", null, false, qb.isDefSearch(), null));
        assertEquals("<a class=\"s\" href=\"#0\"><span class=\"l\">0</span> "
                + "<b>Mixed</b> case: abc AbC dEf</a><br/>",
                out.toString());
    }
View Full Code Here

     * The results from mixed-case symbol search should contain tags.
     */
    @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"});
    }
View Full Code Here

     *
     * @return a query builder with all relevant fields populated.
     */
    public QueryBuilder getQueryBuilder() {
        if (queryBuilder == null) {
            queryBuilder = new QueryBuilder().setFreetext(req.getParameter("q"))
                    .setDefs(req.getParameter(QueryBuilder.DEFS))
                    .setRefs(req.getParameter(QueryBuilder.REFS))
                    .setPath(req.getParameter(QueryBuilder.PATH))
                    .setHist(req.getParameter(QueryBuilder.HIST))
                    .setType(req.getParameter(QueryBuilder.TYPE));
View Full Code Here

TOP

Related Classes of org.opensolaris.opengrok.search.QueryBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.