Example usage for org.apache.lucene.search.uhighlight Passage addMatch

List of usage examples for org.apache.lucene.search.uhighlight Passage addMatch

Introduction

In this page you can find the example usage for org.apache.lucene.search.uhighlight Passage addMatch.

Prototype

public void addMatch(int startOffset, int endOffset, BytesRef term, int termFreqInDoc) 

Source Link

Usage

From source file:org.opengrok.indexer.search.context.ContextFormatterTest.java

License:Open Source License

@Test
public void testLineMatchFormatted() {
    final String WORD = "gravida";
    int woff = DOC.indexOf(WORD);
    assertTrue(WORD, woff >= 0);/*from  w ww.  ja  v  a  2s .  c om*/

    Passage p = new Passage();
    p.setStartOffset(woff);
    p.setEndOffset(woff + WORD.length());
    p.addMatch(woff, p.getEndOffset(), new BytesRef(WORD), 1);
    assertEquals("getNumMatches()", 1, p.getNumMatches());

    // First, test with contextCount==0
    ContextArgs args = new ContextArgs((short) 0, (short) 10);
    ContextFormatter fmt = new ContextFormatter(args);
    fmt.setUrl("http://example.com");
    Object res = fmt.format(new Passage[] { p }, DOC);
    assertNotNull("format() result", res);

    final String DOCCTX_0 = "<a class=\"s\" href=\"http://example.com#3\"><span class=\"l\">"
            + "3</span> Mauris diam nisl, tincidunt nec <b>gravida</b> sit"
            + " amet, efficitur vitae</a><br/>\n";
    String ctx = res.toString();
    assertLinesEqual("format().toString()", DOCCTX_0, ctx);

    // Second, test with contextCount==1
    args = new ContextArgs((short) 1, (short) 10);
    fmt = new ContextFormatter(args);
    fmt.setUrl("http://example.com");
    res = fmt.format(new Passage[] { p }, DOC);
    assertNotNull("format() result", res);

    final String DOCCTX_1 = "<a class=\"s\" href=\"http://example.com#2\"><span class=\"l\">"
            + "2</span> Mauris vel tortor vel nisl efficitur fermentum nec vel" + " erat.</a><br/>"
            + "<a class=\"s\" href=\"http://example.com#3\"><span class=\"l\">"
            + "3</span> Mauris diam nisl, tincidunt nec <b>gravida</b> sit" + " amet, efficitur vitae</a><br/>"
            + "<a class=\"s\" href=\"http://example.com#4\"><span class=\"l\">"
            + "4</span> est. Sed aliquam non mi vel mattis:</a><br/>";
    ctx = res.toString();
    assertLinesEqual("format().toString()", DOCCTX_1, ctx);
}

From source file:org.opengrok.indexer.search.context.ContextFormatterTest.java

License:Open Source License

@Test
public void testLinesSpanningMatchFormatted() {
    Passage p = new Passage();
    p.setStartOffset(0);//w  ww  .  jav  a 2  s.  c  o  m
    p.setEndOffset(DOC2.length());
    p.addMatch(0, p.getEndOffset(), new BytesRef(DOC2), 1);
    assertEquals("getNumMatches()", 1, p.getNumMatches());

    /**
     * We're using the entire document, but see how it behaves with
     * contextCount==1
     */
    ContextArgs args = new ContextArgs((short) 1, (short) 10);
    ContextFormatter fmt = new ContextFormatter(args);
    fmt.setUrl("http://example.com");
    Object res = fmt.format(new Passage[] { p }, DOC2);
    assertNotNull("format() result", res);

    final String DOC2CTX = "<a class=\"s\" href=\"http://example.com#1\"><span class=\"l\">"
            + "1</span> <b>abc</b></a><br/>" + "<a class=\"s\" href=\"http://example.com#2\"><span class=\"l\">"
            + "2</span> <b>def</b></a><br/>" + "<a class=\"s\" href=\"http://example.com#3\"><span class=\"l\">"
            + "3</span> <b>ghi</b></a><br/>";
    String ctx = res.toString();
    assertLinesEqual("format().toString()", DOC2CTX, ctx);
}

From source file:org.opengrok.indexer.search.context.ContextFormatterTest.java

License:Open Source License

@Test
public void testDualElidedMatchFormatted() {
    final String WORD = "dignissim";
    int woff = DOC.indexOf(WORD);
    assertTrue(WORD, woff >= 0);//from  w w  w.  j a v  a2s  .c om

    Passage p = new Passage();
    p.setStartOffset(woff);
    p.setEndOffset(woff + WORD.length());
    p.addMatch(woff, p.getEndOffset(), new BytesRef(WORD), 1);
    assertEquals("getNumMatches()", 1, p.getNumMatches());

    // First, test with contextCount==0
    ContextArgs args = new ContextArgs((short) 0, (short) 10);
    ContextFormatter fmt = new ContextFormatter(args);
    fmt.setUrl("http://example.com");
    Object res = fmt.format(new Passage[] { p }, DOC);
    assertNotNull("format() result", res);

    final String DOCCTX_0 = "<a class=\"s\" href=\"http://example.com#6\"><span class=\"l\">"
            + "6</span> &hellip;putate ipsum sed laoreet. Nam maximus libero"
            + " non ornare egestas. Aenean <b>dignissim</b> ipsum eu" + " rhoncus&hellip;</a><br/>\n";
    String ctx = res.toString();
    assertLinesEqual("format().toString()", DOCCTX_0, ctx);

    // Second, test with contextCount==1
    args = new ContextArgs((short) 1, (short) 10);
    fmt = new ContextFormatter(args);
    fmt.setUrl("http://example.com");
    res = fmt.format(new Passage[] { p }, DOC);
    assertNotNull("format() result", res);

    final String DOCCTX_1 = "<a class=\"s\" href=\"http://example.com#5\"><span class=\"l\">"
            + "5</span> </a><br/>" + "<a class=\"s\" href=\"http://example.com#6\"><span class=\"l\">"
            + "6</span> &hellip;putate ipsum sed laoreet. Nam maximus libero"
            + " non ornare egestas. Aenean <b>dignissim</b> ipsum eu" + " rhoncus&hellip;</a><br/>"
            + "<a class=\"s\" href=\"http://example.com#7\"><span class=\"l\">" + "7</span> </a><br/>";
    ctx = res.toString();
    assertLinesEqual("format().toString()", DOCCTX_1, ctx);

    // Third, test with contextCount==1 and a line limit
    args = new ContextArgs((short) 1, (short) 10);
    fmt = new ContextFormatter(args);
    fmt.setUrl("http://example.com");
    fmt.setMoreLimit(2);
    fmt.setMoreUrl("http://example.com/more");
    res = fmt.format(new Passage[] { p }, DOC);
    assertNotNull("format() result", res);

    final String DOCCTX_2M = "<a class=\"s\" href=\"http://example.com#5\"><span class=\"l\">"
            + "5</span> </a><br/>" + "<a class=\"s\" href=\"http://example.com#6\"><span class=\"l\">"
            + "6</span> &hellip;putate ipsum sed laoreet. Nam maximus libero"
            + " non ornare egestas. Aenean <b>dignissim</b> ipsum eu" + " rhoncus&hellip;</a><br/>"
            + "<a href=\"http://example.com/more\">[all &hellip;]</a><br/>";
    ctx = res.toString();
    assertLinesEqual("format().toString()", DOCCTX_2M, ctx);
}

From source file:org.opengrok.indexer.search.context.ContextFormatterTest.java

License:Open Source License

@Test
public void testLeftElidedMatchFormatted() {
    final String WORD = "ultricies";
    int woff = DOC.indexOf(WORD);
    assertTrue(WORD, woff >= 0);/*from  ww  w.  j a v a2  s. com*/

    Passage p = new Passage();
    p.setStartOffset(woff);
    p.setEndOffset(woff + WORD.length());
    p.addMatch(woff, p.getEndOffset(), new BytesRef(WORD), 1);
    assertEquals("getNumMatches()", 1, p.getNumMatches());

    // First, test with contextCount==0
    ContextArgs args = new ContextArgs((short) 0, (short) 10);
    ContextFormatter fmt = new ContextFormatter(args);
    fmt.setUrl("http://example.com");
    Object res = fmt.format(new Passage[] { p }, DOC);
    assertNotNull("format() result", res);

    final String DOCCTX_0 = "<a class=\"s\" href=\"http://example.com#6\"><span "
            + "class=\"l\">6</span> &hellip;um sed laoreet. Nam " + "maximus libero non ornare egestas. Aenean "
            + "dignissim ipsum eu rhoncus <b>ultricies</b>.</a>" + "<br/>";
    String ctx = res.toString();
    assertLinesEqual("format().toString()", DOCCTX_0, ctx);

    // Second, test with contextCount==1
    args = new ContextArgs((short) 1, (short) 10);
    fmt = new ContextFormatter(args);
    fmt.setUrl("http://example.com");
    res = fmt.format(new Passage[] { p }, DOC);
    assertNotNull("format() result", res);

    final String DOCCTX_1 = "<a class=\"s\" href=\"http://example.com#5\"><span "
            + "class=\"l\">5</span> </a><br/>" + "<a class=\"s\" href=\"http://example.com#6\"><span "
            + "class=\"l\">6</span> &hellip;um sed laoreet. Nam " + "maximus libero non ornare egestas. Aenean "
            + "dignissim ipsum eu rhoncus <b>ultricies</b>.</a>" + "<br/>"
            + "<a class=\"s\" href=\"http://example.com#7\"><span " + "class=\"l\">7</span> </a><br/>";
    ctx = res.toString();
    assertLinesEqual("format().toString()", DOCCTX_1, ctx);

    // Third, test with contextCount==1 and a line limit
    args = new ContextArgs((short) 1, (short) 10);
    fmt = new ContextFormatter(args);
    fmt.setUrl("http://example.com");
    fmt.setMoreLimit(2);
    fmt.setMoreUrl("http://example.com/more");
    res = fmt.format(new Passage[] { p }, DOC);
    assertNotNull("format() result", res);

    final String DOCCTX_2M = "<a class=\"s\" href=\"http://example.com#5\">"
            + "<span class=\"l\">5</span> </a><br/>" + "<a class=\"s\" href=\"http://example.com#6\"><span "
            + "class=\"l\">6</span> &hellip;um sed laoreet. Nam " + "maximus libero non ornare egestas. Aenean "
            + "dignissim ipsum eu rhoncus <b>ultricies</b>.</a>"
            + "<br/><a href=\"http://example.com/more\">[all " + "&hellip;]</a><br/>";
    ctx = res.toString();
    assertLinesEqual("format().toString()", DOCCTX_2M, ctx);
}

From source file:org.opengrok.indexer.search.context.ContextFormatterTest.java

License:Open Source License

@Test
public void testRightElidedMatchFormatted() {
    final String WORD = "Maecenas";
    int woff = DOC.indexOf(WORD);
    assertTrue(WORD, woff >= 0);//from  w  w  w .j a  va  2  s. c om

    Passage p = new Passage();
    p.setStartOffset(woff);
    p.setEndOffset(woff + WORD.length());
    p.addMatch(woff, p.getEndOffset(), new BytesRef(WORD), 1);
    assertEquals("getNumMatches()", 1, p.getNumMatches());

    // First, test with contextCount==0
    ContextArgs args = new ContextArgs((short) 0, (short) 10);
    ContextFormatter fmt = new ContextFormatter(args);
    fmt.setUrl("http://example.com");
    Object res = fmt.format(new Passage[] { p }, DOC);
    assertNotNull("format() result", res);

    final String DOCCTX_0 = "<a class=\"s\" href=\"http://example.com#6\"><span "
            + "class=\"l\">6</span> ----<b>Maecenas</b> vitae "
            + "lacus velit varius vulputate ipsum sed laoreet. "
            + "Nam maximus libero non ornare eg&hellip;</a><br/>";
    String ctx = res.toString();
    assertLinesEqual("format().toString()", DOCCTX_0, ctx);

    // Second, test with contextCount==1
    args = new ContextArgs((short) 1, (short) 10);
    fmt = new ContextFormatter(args);
    fmt.setUrl("http://example.com");
    res = fmt.format(new Passage[] { p }, DOC);
    assertNotNull("format() result", res);

    final String DOCCTX_1 = "<a class=\"s\" href=\"http://example.com#5\"><span "
            + "class=\"l\">5</span> </a><br/>" + "<a class=\"s\" href=\"http://example.com#6\"><span "
            + "class=\"l\">6</span> ----<b>Maecenas</b> vitae "
            + "lacus velit varius vulputate ipsum sed laoreet. "
            + "Nam maximus libero non ornare eg&hellip;</a><br/>"
            + "<a class=\"s\" href=\"http://example.com#7\"><span " + "class=\"l\">7</span> </a><br/>";
    ctx = res.toString();
    assertLinesEqual("format().toString()", DOCCTX_1, ctx);

    // Third, test with contextCount==1 and a line limit
    args = new ContextArgs((short) 1, (short) 10);
    fmt = new ContextFormatter(args);
    fmt.setUrl("http://example.com");
    fmt.setMoreLimit(2);
    fmt.setMoreUrl("http://example.com/more");
    res = fmt.format(new Passage[] { p }, DOC);
    assertNotNull("format() result", res);

    final String DOCCTX_2M = "<a class=\"s\" href=\"http://example.com#5\"><span "
            + "class=\"l\">5</span> </a><br/>" + "<a class=\"s\" href=\"http://example.com#6\"><span "
            + "class=\"l\">6</span> ----<b>Maecenas</b> vitae "
            + "lacus velit varius vulputate ipsum sed laoreet. "
            + "Nam maximus libero non ornare eg&hellip;</a><br/>" + "<a href=\"http://example.com/more\">[all "
            + "&hellip;]</a><br/>\n";
    ctx = res.toString();
    assertLinesEqual("format().toString()", DOCCTX_2M, ctx);
}

From source file:org.opengrok.indexer.search.context.ContextFormatterTest.java

License:Open Source License

@Test
public void testBoundsProblemFormatted() {
    final String PHRASE = "efficitur vitae";
    int phOff = DOC.indexOf(PHRASE);
    assertTrue(PHRASE, phOff >= 0);//from   ww w. j av  a 2 s .c o m

    // Create a slightly-longer word of all '*'.
    final int LF_CHAR_COUNT = 1;
    final String STARS = String.join("", Collections.nCopies(PHRASE.length() + LF_CHAR_COUNT, "*"));

    Passage p = new Passage();
    p.setStartOffset(phOff);
    p.setEndOffset(phOff + STARS.length());
    p.addMatch(phOff, p.getEndOffset(), new BytesRef(STARS), 1);
    assertEquals("getNumMatches()", 1, p.getNumMatches());

    // Test with contextCount==0
    ContextArgs args = new ContextArgs((short) 0, (short) 10);
    ContextFormatter fmt = new ContextFormatter(args);
    fmt.setUrl("http://example.com");
    Object res = fmt.format(new Passage[] { p }, DOC);
    assertNotNull("format() result", res);

    final String DOC_CTX_0 = "<a class=\"s\" href=\"http://example.com#3\"><span class=\"l\">"
            + "3</span> Mauris diam nisl, tincidunt nec gravida sit"
            + " amet, <b>efficitur vitae</b></a><br/>\n";
    String ctx = res.toString();
    assertLinesEqual("format().toString()", DOC_CTX_0, ctx);
}

From source file:org.opengrok.indexer.search.context.PassageConverterTest.java

License:Open Source License

@Test
public void testOneWord() {
    final String WORD = "gravida";
    int woff = DOC.indexOf(WORD);
    assertTrue(WORD, woff >= 0);//from www . ja  va 2 s  .  c o  m

    Passage p = new Passage();
    p.setStartOffset(woff);
    p.setEndOffset(woff + WORD.length());
    p.addMatch(woff, p.getEndOffset(), new BytesRef(WORD), 1);
    assertEquals("getNumMatches()", 1, p.getNumMatches());

    PassageConverter cvt = getConverter((short) 0);
    SortedMap<Integer, LineHighlight> linemap = cvt.convert(new Passage[] { p }, splitter);

    assertEquals("linemap size()", 1, linemap.size());
    int lineno = linemap.firstKey();
    assertEquals("lineno", 2, lineno);

    LineHighlight lhi = linemap.get(lineno);
    assertNotNull("get LineHighlight", lhi);
    assertEquals("getLelide()", 0, lhi.getLelide());
    assertEquals("getRelide()", 0, lhi.getRelide());
    assertEquals("countMarkups()", 1, lhi.countMarkups());

    PhraseHighlight phi = lhi.getMarkup(0);
    assertNotNull("get PhraseHighlight", phi);
    assertEquals("getLineStart()", 32, phi.getLineStart());
    assertEquals("getLineEnd()", 32 + WORD.length(), phi.getLineEnd());
}

From source file:org.opengrok.indexer.search.context.PassageConverterTest.java

License:Open Source License

@Test
public void testOneWordElided() {
    final String WORD = "dignissim";
    int woff = DOC.indexOf(WORD);
    assertTrue(WORD, woff >= 0);// w  w  w .j ava2s. c  o m

    Passage p = new Passage();
    p.setStartOffset(woff);
    p.setEndOffset(woff + WORD.length());
    p.addMatch(woff, p.getEndOffset(), new BytesRef(WORD), 1);
    assertEquals("getNumMatches()", 1, p.getNumMatches());

    PassageConverter cvt = getConverter((short) 0);
    SortedMap<Integer, LineHighlight> linemap = cvt.convert(new Passage[] { p }, splitter);

    assertEquals("linemap size()", 1, linemap.size());
    int lineno = linemap.firstKey();
    assertEquals("lineno", 5, lineno);

    LineHighlight lhi = linemap.get(lineno);
    assertNotNull("get LineHighlight", lhi);
    assertEquals("getLelide()", 41, lhi.getLelide());
    assertEquals("getRelide()", 139, lhi.getRelide());
    assertEquals("context width minus 2", 139 - 41, cvt.getArgs().getContextWidth() - 2);
    assertEquals("countMarkups()", 1, lhi.countMarkups());

    PhraseHighlight phi = lhi.getMarkup(0);
    assertNotNull("get PhraseHighlight", phi);
    assertEquals("getLineStart()", 113, phi.getLineStart());
    assertEquals("getLineEnd()", 113 + WORD.length(), phi.getLineEnd());
}

From source file:org.opengrok.indexer.search.context.PassageConverterTest.java

License:Open Source License

@Test
public void testTwoWordsElided() {
    final String WORD1 = "Maecenas";
    int woff1 = DOC.indexOf(WORD1);
    assertTrue(WORD1, woff1 >= 0);/*from   w ww  .j a v  a 2s .c o m*/

    final String WORD2 = "rhoncus";
    int woff2 = DOC.indexOf(WORD2);
    assertTrue(WORD2, woff2 >= 0);

    Passage p = new Passage();
    p.setStartOffset(woff1);
    p.setEndOffset(woff2 + WORD2.length());
    p.addMatch(woff1, woff1 + WORD1.length(), new BytesRef(WORD1), 1);
    p.addMatch(woff2, woff2 + WORD2.length(), new BytesRef(WORD2), 1);
    assertEquals("getNumMatches()", 2, p.getNumMatches());

    PassageConverter cvt = getConverter((short) 0);
    SortedMap<Integer, LineHighlight> linemap = cvt.convert(new Passage[] { p }, splitter);

    assertEquals("linemap size()", 1, linemap.size());
    int lineno = linemap.firstKey();
    assertEquals("lineno", 5, lineno);

    LineHighlight lhi = linemap.get(lineno);
    assertNotNull("get LineHighlight", lhi);
    assertEquals("getLelide()", 4, lhi.getLelide());
    assertEquals("getRelide()", 102, lhi.getRelide());
    assertEquals("context width minus 2", 139 - 41, cvt.getArgs().getContextWidth() - 2);
    assertEquals("countMarkups()", 2, lhi.countMarkups());

    PhraseHighlight phi = lhi.getMarkup(0);
    assertNotNull("get PhraseHighlight", phi);
    assertEquals("0:getLineStart()", 4, phi.getLineStart());
    assertEquals("0:getLineEnd()", 4 + WORD1.length(), phi.getLineEnd());

    phi = lhi.getMarkup(1);
    assertNotNull("get PhraseHighlight", phi);
    assertEquals("1:getLineStart()", 132, phi.getLineStart());
    assertEquals("1:getLineEnd()", 132 + WORD2.length(), phi.getLineEnd());
}

From source file:org.opengrok.indexer.search.context.PassageConverterTest.java

License:Open Source License

@Test
public void testLineSpanningMatch() {
    final String PHRASE = "elit.\nMauris";
    int poff = DOC.indexOf(PHRASE);
    assertTrue(PHRASE, poff >= 0);//from w ww  .j  a  v a  2 s  . co  m

    Passage p = new Passage();
    p.setStartOffset(poff);
    p.setEndOffset(poff + PHRASE.length());
    p.addMatch(poff, p.getEndOffset(), new BytesRef(PHRASE), 1);
    assertEquals("getNumMatches()", 1, p.getNumMatches());

    PassageConverter cvt = getConverter((short) 0);
    SortedMap<Integer, LineHighlight> linemap = cvt.convert(new Passage[] { p }, splitter);

    assertEquals("linemap size()", 2, linemap.size());
    int lineno = linemap.firstKey();
    assertEquals("first lineno", 0, lineno);
    assertTrue("linemap[1] exists", linemap.containsKey(1));

    LineHighlight lhi = linemap.get(lineno);
    assertNotNull("get LineHighlight", lhi);
    assertEquals("getLelide()", 0, lhi.getLelide());
    assertEquals("getRelide()", 0, lhi.getRelide());
    assertEquals("countMarkups()", 1, lhi.countMarkups());

    PhraseHighlight phi = lhi.getMarkup(0);
    assertNotNull("get PhraseHighlight", phi);
    assertEquals("getLineStart()", 55, phi.getLineStart());
    assertEquals("getLineEnd()", Integer.MAX_VALUE, phi.getLineEnd());

    lhi = linemap.get(lineno + 1);
    assertNotNull("get LineHighlight", lhi);
    assertEquals("getLelide()", 0, lhi.getLelide());
    assertEquals("getRelide()", 0, lhi.getRelide());
    assertEquals("countMarkups()", 1, lhi.countMarkups());

    phi = lhi.getMarkup(0);
    assertNotNull("get PhraseHighlight", phi);
    assertEquals("getLineStart()", -1, phi.getLineStart());
    assertEquals("getLineEnd()", 6, phi.getLineEnd());
}