Search in sources :

Example 1 with SpanWithinQuery

use of de.ids_mannheim.korap.query.SpanWithinQuery in project Krill by KorAP.

the class TestMatchIndex method indexExampleFocusWithSpan.

@Test
public void indexExampleFocusWithSpan() throws IOException {
    KrillIndex ki = new KrillIndex();
    // abcabcabac
    FieldDocument fd = new FieldDocument();
    fd.addTV("base", "abcabcabac", "[(0-1)s:a|i:a|_0$<i>0<i>1|-:t$<i>10]" + "[(1-2)s:b|i:b|_1$<i>1<i>2|<>:s$<b>64<i>1<i>5<i>5]" + "[(2-3)s:c|i:c|_2$<i>2<i>3|<>:s$<b>64<i>2<i>7<i>7]" + "[(3-4)s:a|i:a|_3$<i>3<i>4]" + "[(4-5)s:b|i:b|_4$<i>4<i>5]" + "[(5-6)s:c|i:c|_5$<i>5<i>6]" + "[(6-7)s:a|i:a|_6$<i>6<i>7]" + "[(7-8)s:b|i:b|_7$<i>7<i>8]" + "[(8-9)s:a|i:a|_8$<i>8<i>9]" + "[(9-10)s:c|i:c|_9$<i>9<i>10]");
    ki.addDoc(fd);
    ki.commit();
    SpanQuery sq;
    Result kr;
    // sq = new SpanWithinQuery(new SpanClassQuery(new SpanElementQuery(
    // "base", "s"), (byte) 2), new SpanClassQuery(new SpanTermQuery(
    // new Term("base", "s:b")), (byte) 3));
    // 
    // kr = ki.search(sq, (short) 10);
    // assertEquals(kr.getSerialQuery(),
    // "spanContain({2: <base:s />}, {3: base:s:b})");
    // assertEquals(kr.getMatch(0).getSnippetBrackets(),
    // "a[{2:{3:b}cab}]cabac");
    sq = new SpanFocusQuery(new SpanWithinQuery(new SpanClassQuery(new SpanElementQuery("base", "s"), (byte) 2), new SpanClassQuery(new SpanTermQuery(new Term("base", "s:b")), (byte) 3)), (byte) 3);
    kr = ki.search(sq, (short) 10);
    assertEquals(kr.getSerialQuery(), "focus(3: spanContain({2: <base:s />}, {3: base:s:b}))");
    assertEquals(kr.getMatch(0).getSnippetBrackets(), "a[[{3:b}]]cabcab ...");
}
Also used : SpanClassQuery(de.ids_mannheim.korap.query.SpanClassQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanWithinQuery(de.ids_mannheim.korap.query.SpanWithinQuery) Term(org.apache.lucene.index.Term) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanFocusQuery(de.ids_mannheim.korap.query.SpanFocusQuery) SpanElementQuery(de.ids_mannheim.korap.query.SpanElementQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Result(de.ids_mannheim.korap.response.Result) Test(org.junit.Test)

Example 2 with SpanWithinQuery

use of de.ids_mannheim.korap.query.SpanWithinQuery in project Krill by KorAP.

the class TestMatchIndex method indexExampleFocusWithSkip.

@Ignore
public void indexExampleFocusWithSkip() throws IOException {
    KrillIndex ki = new KrillIndex();
    // abcabcabac
    FieldDocument fd = new FieldDocument();
    fd.addTV("base", "abcabcabac", // The payload should be ignored
    // |<>:p#0-10<i>9]" +
    "[(0-1)s:a|i:a|_0$<i>0<i>1|-:t$<i>10]" + "[(1-2)s:b|i:b|_1$<i>1<i>2|<>:s$<b>64<i>1<i>5<i>5]" + "[(2-3)s:c|i:c|_2$<i>2<i>3|<>:s$<b>64<i>2<i>7<i>7]" + "[(3-4)s:a|i:a|_3$<i>3<i>4]" + "[(4-5)s:b|i:b|_4$<i>4<i>5]" + "[(5-6)s:c|i:c|_5$<i>5<i>6]" + "[(6-7)s:a|i:a|_6$<i>6<i>7]" + "[(7-8)s:b|i:b|_7$<i>7<i>8]" + "[(8-9)s:a|i:a|_8$<i>8<i>9]" + "[(9-10)s:c|i:c|_9$<i>9<i>10]");
    ki.addDoc(fd);
    fd = new FieldDocument();
    fd.addTV("base", "gbcgbcgbgc", "[(0-1)s:g|i:g|_0$<i>0<i>1|-:t$<i>10|<>:p$<b>64<i>0<i>10<i>9]" + "[(1-2)s:b|i:b|_1$<i>1<i>2|<>:s$<b>64<i>1<i>5<i>5]" + "[(2-3)s:c|i:c|_2$<i>2<i>3|<>:s$<b>64<i>2<i>7<i>7]" + "[(3-4)s:g|i:g|_3$<i>3<i>4]" + "[(4-5)s:b|i:b|_4$<i>4<i>5]" + "[(5-6)s:c|i:c|_5$<i>5<i>6]" + "[(6-7)s:g|i:g|_6$<i>6<i>7]" + "[(7-8)s:b|i:b|_7$<i>7<i>8]" + "[(8-9)s:g|i:g|_8$<i>8<i>9]" + "[(9-10)s:c|i:c|_9$<i>9<i>10]");
    ki.addDoc(fd);
    fd = new FieldDocument();
    fd.addTV("base", "gbcgbcgbgc", "[(0-1)s:g|i:g|_0$<i>0<i>1|-:t$<i>10]" + "[(1-2)s:b|i:b|_1$<i>1<i>2]" + "[(2-3)s:c|i:c|_2$<i>2<i>3]" + "[(3-4)s:g|i:g|_3$<i>3<i>4]" + "[(4-5)s:b|i:b|_4$<i>4<i>5]" + "[(5-6)s:c|i:c|_5$<i>5<i>6]" + "[(6-7)s:g|i:g|_6$<i>6<i>7]" + "[(7-8)s:b|i:b|_7$<i>7<i>8]" + "[(8-9)s:g|i:g|_8$<i>8<i>9]" + "[(9-10)s:c|i:c|_9$<i>9<i>10]");
    ki.addDoc(fd);
    fd = new FieldDocument();
    // contains(<p>, focus(3: contains({2:<s>}, {3:a})))
    fd.addTV("base", "acabcabac", "[(0-1)s:a|i:a|_0$<i>0<i>1|-:t$<i>10|<>:p$<b>64<i>0<i>9<i>8]" + "[(1-2)s:b|i:b|_1$<i>1<i>2|<>:s$<b>64<i>1<i>5<i>5]" + "[(2-3)s:a|i:a|_2$<i>2<i>3|<>:s$<b>64<i>2<i>7<i>7]" + "[(3-4)s:b|i:b|_3$<i>3<i>4]" + "[(4-5)s:c|i:c|_4$<i>4<i>5]" + "[(5-6)s:a|i:a|_5$<i>5<i>6]" + "[(6-7)s:b|i:b|_6$<i>6<i>7]" + "[(7-8)s:a|i:a|_7$<i>7<i>8]" + "[(8-9)s:c|i:c|_8$<i>8<i>9]");
    ki.addDoc(fd);
    ki.commit();
    SpanQuery sq;
    Result kr;
    KrillCollection kc = new KrillCollection(ki);
    assertEquals("Documents", 4, kc.numberOf("documents"));
    // within(<p>, focus(3:within({2:<s>}, {3:a})))
    sq = new SpanWithinQuery(new SpanElementQuery("base", "p"), new SpanFocusQuery(new SpanWithinQuery(new SpanClassQuery(new SpanElementQuery("base", "s"), (byte) 2), new SpanClassQuery(new SpanTermQuery(new Term("base", "s:a")), (byte) 3)), (byte) 3));
    // fail("Skipping may go horribly wrong! (Known issue)");
    Krill ks = new Krill(sq);
    ks.getMeta().setStartIndex(0).setCount((short) 20).setContext(new SearchContext(true, (short) 5, true, (short) 5));
    kr = ks.apply(ki);
    // kr = ki.search(kc, sq, 0, (short) 20, true, (short) 5, true, (short) 5);
    assertEquals(kr.getSerialQuery(), "spanContain(<base:p />, focus(3: spanContain({2: <base:s />}, {3: base:s:a})))");
    assertEquals(12, kr.getTotalResults());
    assertEquals("[a{2:bc{3:a}b}cabac]", kr.getMatch(0).getSnippetBrackets());
    assertEquals("[ab{2:c{3:a}bcab}ac]", kr.getMatch(1).getSnippetBrackets());
    assertEquals("[ab{2:cabc{3:a}}bac]", kr.getMatch(2).getSnippetBrackets());
}
Also used : SearchContext(de.ids_mannheim.korap.response.SearchContext) Term(org.apache.lucene.index.Term) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanElementQuery(de.ids_mannheim.korap.query.SpanElementQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Result(de.ids_mannheim.korap.response.Result) Krill(de.ids_mannheim.korap.Krill) SpanClassQuery(de.ids_mannheim.korap.query.SpanClassQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanWithinQuery(de.ids_mannheim.korap.query.SpanWithinQuery) SpanFocusQuery(de.ids_mannheim.korap.query.SpanFocusQuery) KrillCollection(de.ids_mannheim.korap.KrillCollection) Ignore(org.junit.Ignore)

Example 3 with SpanWithinQuery

use of de.ids_mannheim.korap.query.SpanWithinQuery in project Krill by KorAP.

the class SpanQueryWrapper method toQuery.

/**
 * Serialize the wrapped query and return a SpanQuery.
 * This will be the final query and may be rewritten.
 *
 * @return A {@link SpanQuery} object.
 * @throws QueryException
 */
public SpanQuery toQuery() throws QueryException {
    if (this.isNull() || this.isEmpty()) {
        return null;
    }
    ;
    // Wrap the query in a <base/s=t>, if it's extended to the right
    if (this.isExtendedToTheRight()) {
        return new SpanFocusQuery(new SpanWithinQuery("base/s:t", new SpanClassQuery(this.toFragmentQuery(), (byte) 254)), (byte) 254);
    }
    ;
    SpanQuery sq = this.toFragmentQuery();
    log.info(sq.toString());
    return sq;
}
Also used : SpanClassQuery(de.ids_mannheim.korap.query.SpanClassQuery) SpanWithinQuery(de.ids_mannheim.korap.query.SpanWithinQuery) SpanFocusQuery(de.ids_mannheim.korap.query.SpanFocusQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 4 with SpanWithinQuery

use of de.ids_mannheim.korap.query.SpanWithinQuery in project Krill by KorAP.

the class TestWithinIndex method queryJSONpoly2.

// contains(<s>, (es wird | wird es))
@Test
public void queryJSONpoly2() throws QueryException, IOException {
    String jsonPath = URLDecoder.decode(getClass().getResource("/queries/poly2.json").getFile(), "UTF-8");
    String jsonPQuery = readFile(jsonPath);
    SpanQueryWrapper sqwi = new KrillQuery("tokens").fromKoral(jsonPQuery);
    SpanWithinQuery sq = (SpanWithinQuery) sqwi.toQuery();
    KrillIndex ki = new KrillIndex();
    ki.addDoc(getClass().getResourceAsStream("/wiki/DDD-08370.json.gz"), true);
    ki.addDoc(getClass().getResourceAsStream("/wiki/PPP-02924.json.gz"), true);
    ki.commit();
    Result kr = ki.search(sq, (short) 10);
    assertEquals(2, kr.getTotalResults());
    assertEquals(0, kr.getMatch(0).getLocalDocID());
    assertEquals(76, kr.getMatch(0).getStartPos());
    assertEquals(93, kr.getMatch(0).getEndPos());
    assertEquals(1, kr.getMatch(1).getLocalDocID());
    assertEquals(237, kr.getMatch(1).getStartPos());
    assertEquals(252, kr.getMatch(1).getEndPos());
}
Also used : KrillQuery(de.ids_mannheim.korap.KrillQuery) SpanWithinQuery(de.ids_mannheim.korap.query.SpanWithinQuery) SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper) KrillIndex(de.ids_mannheim.korap.KrillIndex) Result(de.ids_mannheim.korap.response.Result) Test(org.junit.Test)

Example 5 with SpanWithinQuery

use of de.ids_mannheim.korap.query.SpanWithinQuery in project Krill by KorAP.

the class TestWithinIndex method indexExample2e.

@Test
public void indexExample2e() throws IOException {
    KrillIndex ki = new KrillIndex();
    // <a><a><a>h</a>hij</a>hij</a>
    FieldDocument fd = new FieldDocument();
    fd.addTV("base", // <a><a>hhij</a>hijh</a>ij</a>
    "h  h  i  j  h  i  j  h  i  j        ", "[s:h|_0$<i>0<i>3|<>:a$<b>64<i>0<i>12<i>4<b>0|" + "<>:a$<b>64<i>0<i>24<i>8<b>0|" + // 1
    "<>:a$<b>64<i>0<i>30<i>10<b>0]" + // 2
    "[s:h|_1$<i>3<i>6]" + // 3
    "[s:i|_2$<i>6<i>9]" + // 4
    "[s:j|_3$<i>9<i>12]" + // 5
    "[s:h|_4$<i>12<i>15]" + // 6
    "[s:i|_5$<i>15<i>18]" + // 7
    "[s:j|_6$<i>18<i>21]" + // 8
    "[s:h|_7$<i>21<i>24]" + // 9
    "[s:i|_8$<i>24<i>27]" + // 10
    "[s:j|_9$<i>27<i>30]");
    ki.addDoc(fd);
    // Save documents
    ki.commit();
    assertEquals(1, ki.numberOf("documents"));
    SpanQuery sq;
    Result kr;
    sq = new SpanWithinQuery(new SpanElementQuery("base", "a"), new SpanTermQuery(new Term("base", "s:h")));
    kr = ki.search(sq, (short) 10);
    // assertEquals("totalResults", 10, kr.getTotalResults());
    assertEquals("StartPos (0)", 0, kr.getMatch(0).startPos);
    assertEquals("EndPos (0)", 4, kr.getMatch(0).endPos);
    assertEquals("Snippet (0)", "[[h  h  i  j  ]]h  i  j  h  i  j   ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("StartPos (1)", 0, kr.getMatch(1).startPos);
    assertEquals("EndPos (1)", 4, kr.getMatch(1).endPos);
    assertEquals("Snippet (1)", "[[h  h  i  j  ]]h  i  j  h  i  j   ...", kr.getMatch(1).getSnippetBrackets());
    assertEquals("StartPos (2)", 0, kr.getMatch(2).startPos);
    assertEquals("EndPos (2)", 8, kr.getMatch(2).endPos);
    assertEquals("Snippet (2)", "[[h  h  i  j  h  i  j  h  ]]i  j        ", kr.getMatch(2).getSnippetBrackets());
    assertEquals("StartPos (3)", 0, kr.getMatch(3).startPos);
    assertEquals("EndPos (3)", 8, kr.getMatch(3).endPos);
    assertEquals("Snippet (3)", "[[h  h  i  j  h  i  j  h  ]]i  j        ", kr.getMatch(3).getSnippetBrackets());
    assertEquals("StartPos (4)", 0, kr.getMatch(4).startPos);
    assertEquals("EndPos (4)", 8, kr.getMatch(4).endPos);
    assertEquals("Snippet (4)", "[[h  h  i  j  h  i  j  h  ]]i  j        ", kr.getMatch(4).getSnippetBrackets());
    assertEquals("StartPos (5)", 0, kr.getMatch(5).startPos);
    assertEquals("EndPos (5)", 8, kr.getMatch(5).endPos);
    assertEquals("Snippet (5)", "[[h  h  i  j  h  i  j  h  ]]i  j        ", kr.getMatch(5).getSnippetBrackets());
    assertEquals("StartPos (6)", 0, kr.getMatch(6).startPos);
    assertEquals("EndPos (6)", 10, kr.getMatch(6).endPos);
    assertEquals("StartPos (7)", 0, kr.getMatch(7).startPos);
    assertEquals("EndPos (7)", 10, kr.getMatch(7).endPos);
    assertEquals("StartPos (8)", 0, kr.getMatch(8).startPos);
    assertEquals("EndPos (8)", 10, kr.getMatch(8).endPos);
    assertEquals("StartPos (9)", 0, kr.getMatch(9).startPos);
    assertEquals("EndPos (9)", 10, kr.getMatch(9).endPos);
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanWithinQuery(de.ids_mannheim.korap.query.SpanWithinQuery) Term(org.apache.lucene.index.Term) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanElementQuery(de.ids_mannheim.korap.query.SpanElementQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Result(de.ids_mannheim.korap.response.Result) Test(org.junit.Test)

Aggregations

SpanWithinQuery (de.ids_mannheim.korap.query.SpanWithinQuery)20 Test (org.junit.Test)18 KrillIndex (de.ids_mannheim.korap.KrillIndex)17 Result (de.ids_mannheim.korap.response.Result)17 SpanElementQuery (de.ids_mannheim.korap.query.SpanElementQuery)16 SpanQuery (org.apache.lucene.search.spans.SpanQuery)16 Term (org.apache.lucene.index.Term)15 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)15 SpanClassQuery (de.ids_mannheim.korap.query.SpanClassQuery)4 SpanFocusQuery (de.ids_mannheim.korap.query.SpanFocusQuery)3 SpanNextQuery (de.ids_mannheim.korap.query.SpanNextQuery)3 KrillQuery (de.ids_mannheim.korap.KrillQuery)2 SpanQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanQueryWrapper)2 SpanSequenceQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper)2 Krill (de.ids_mannheim.korap.Krill)1 KrillCollection (de.ids_mannheim.korap.KrillCollection)1 SearchContext (de.ids_mannheim.korap.response.SearchContext)1 Ignore (org.junit.Ignore)1