Search in sources :

Example 11 with QueryBuilder

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

the class TestRegexWildcardIndex method indexRegexCombined.

@Test
public void indexRegexCombined() throws Exception {
    KrillIndex ki = new KrillIndex();
    // abcabcabac
    FieldDocument fd = new FieldDocument();
    fd.addTV("base", "affe afffe baum baumgarten steingarten franz hans haus efeu effe", "[(0-4)s:affe|_0$<i>0<i>4|-:t$<i>10]" + "[(5-10)s:afffe|_1$<i>5<i>10]" + "[(11-15)s:baum|_2$<i>11<i>15]" + "[(16-26)s:baumgarten|_3$<i>16<i>26]" + "[(27-38)s:steingarten|_4$<i>27<i>38]" + "[(39-44)s:franz|_5$<i>39<i>44]" + "[(45-49)s:hans|_6$<i>45<i>49]" + "[(50-54)s:haus|_7$<i>50<i>54]" + "[(55-59)s:efeu|_8$<i>55<i>59]" + "[(60-64)s:effe|_9$<i>60<i>64]");
    ki.addDoc(fd);
    ki.commit();
    QueryBuilder kq = new QueryBuilder("base");
    SpanQuery sq = kq.seq(kq.seg("s:affe")).append(kq.re("s:af*e")).toQuery();
    assertEquals("spanNext(base:s:affe, SpanMultiTermQueryWrapper(base:/s:af*e/))", sq.toString());
    Krill ks = new Krill(sq);
    ks.getMeta().getContext().left.setToken(true).setLength(1);
    ks.getMeta().getContext().right.setToken(true).setLength(1);
    Result kr = ki.search(ks);
    assertEquals((long) 1, kr.getTotalResults());
    assertEquals("[[affe afffe]] baum ...", kr.getMatch(0).getSnippetBrackets());
}
Also used : Krill(de.ids_mannheim.korap.Krill) QueryBuilder(de.ids_mannheim.korap.query.QueryBuilder) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Result(de.ids_mannheim.korap.response.Result) Test(org.junit.Test)

Example 12 with QueryBuilder

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

the class TestRegexWildcardIndex method indexWildcard.

@Test
public void indexWildcard() throws Exception {
    KrillIndex ki = new KrillIndex();
    // abcabcabac
    FieldDocument fd = new FieldDocument();
    fd.addTV("base", "affe afffe baum baumgarten steingarten franz hans haus efeu effe", "[(0-4)s:affe|_0$<i>0<i>4|-:t$<i>10]" + "[(5-10)s:afffe|_1$<i>5<i>10]" + "[(11-15)s:baum|_2$<i>11<i>15]" + "[(16-26)s:baumgarten|_3$<i>16<i>26]" + "[(27-38)s:steingarten|_4$<i>27<i>38]" + "[(39-44)s:franz|_5$<i>39<i>44]" + "[(45-49)s:hans|_6$<i>45<i>49]" + "[(50-54)s:haus|_7$<i>50<i>54]" + "[(55-59)s:efeu|_8$<i>55<i>59]" + "[(60-64)s:effe|_9$<i>60<i>64]");
    ki.addDoc(fd);
    ki.commit();
    QueryBuilder kq = new QueryBuilder("base");
    SpanQueryWrapper sq = kq.wc("s:af*e");
    assertEquals("SpanMultiTermQueryWrapper(base:s:af*e)", sq.toQuery().toString());
    Krill ks = _newKrill(sq);
    Result kr = ki.search(ks);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals("[[affe]] afffe ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("affe [[afffe]] baum ...", kr.getMatch(1).getSnippetBrackets());
    ks = _newKrill(new QueryBuilder("base").wc("s:baum.*"));
    kr = ki.search(ks);
    assertEquals((long) 0, kr.getTotalResults());
    ks = _newKrill(new QueryBuilder("base").wc("s:baum*"));
    kr = ki.search(ks);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals("... afffe [[baum]] baumgarten ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("... baum [[baumgarten]] steingarten ...", kr.getMatch(1).getSnippetBrackets());
    ks = _newKrill(new QueryBuilder("base").wc("s:*garten"));
    kr = ki.search(ks);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals("... baum [[baumgarten]] steingarten ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("... baumgarten [[steingarten]] franz ...", kr.getMatch(1).getSnippetBrackets());
    ks = _newKrill(new QueryBuilder("base").wc("s:ha?s"));
    kr = ki.search(ks);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals("... franz [[hans]] haus ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("... hans [[haus]] efeu ...", kr.getMatch(1).getSnippetBrackets());
    ks = _newKrill(new QueryBuilder("base").wc("s:?ff?"));
    kr = ki.search(ks);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals("[[affe]] afffe ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("... efeu [[effe]]", kr.getMatch(1).getSnippetBrackets());
}
Also used : Krill(de.ids_mannheim.korap.Krill) QueryBuilder(de.ids_mannheim.korap.query.QueryBuilder) 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 13 with QueryBuilder

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

the class TestRegexWildcardIndex method indexRegexWithinRewrite.

@Test
public void indexRegexWithinRewrite() throws Exception {
    KrillIndex ki = new KrillIndex();
    // abcabcabac
    FieldDocument fd = new FieldDocument();
    fd.addTV("base", "affe afffe baum baumgarten steingarten franz hans haus efeu effe", "[(0-4)s:affe|_0$<i>0<i>4|-:t$<i>10]" + "[(5-10)s:afffe|_1$<i>5<i>10]" + "[(11-15)s:baum|_2$<i>11<i>15]" + "[(16-26)s:baumgarten|_3$<i>16<i>26]" + "[(27-38)s:steingarten|_4$<i>27<i>38]" + "[(39-44)s:franz|_5$<i>39<i>44]" + "[(45-49)s:hans|_6$<i>45<i>49]" + "[(50-54)s:haus|_7$<i>50<i>54]" + "[(55-59)s:efeu|_8$<i>55<i>59]" + "[(60-64)s:effe|_9$<i>60<i>64]");
    ki.addDoc(fd);
    ki.commit();
    QueryBuilder kq = new QueryBuilder("base");
    SpanQuery sq = kq.contains(kq.seq(kq.re("s:a.*e")).append(kq.re("s:af*e")), kq.seg("s:affe")).toQuery();
    assertEquals("spanContain(spanNext(SpanMultiTermQueryWrapper(base:/s:a.*e/), SpanMultiTermQueryWrapper(base:/s:af*e/)), base:s:affe)", sq.toString());
    Krill ks = new Krill(sq);
    ks.getMeta().getContext().left.setToken(true).setLength(1);
    ks.getMeta().getContext().right.setToken(true).setLength(1);
    Result kr = ki.search(ks);
    assertEquals((long) 1, kr.getTotalResults());
    assertEquals("[[affe afffe]] baum ...", kr.getMatch(0).getSnippetBrackets());
    // Test without matches in sequence
    sq = kq.seq(kq.re("s:z.*e")).append(kq.seg("s:affe")).toQuery();
    assertEquals("spanNext(SpanMultiTermQueryWrapper(base:/s:z.*e/), base:s:affe)", sq.toString());
    kr = ki.search(new Krill(sq));
    assertEquals((long) 0, kr.getTotalResults());
    // Test without matches in segment
    sq = kq.seg().with(kq.re("s:z.*e")).with("s:affe").toQuery();
    assertEquals("spanSegment(SpanMultiTermQueryWrapper(base:/s:z.*e/), base:s:affe)", sq.toString());
    kr = ki.search(new Krill(sq));
    assertEquals((long) 0, kr.getTotalResults());
    // Test without matches in or
    sq = kq.or(kq.re("s:z.*e"), kq.seg("s:affe")).toQuery();
    assertEquals("spanOr([SpanMultiTermQueryWrapper(base:/s:z.*e/), base:s:affe])", sq.toString());
    kr = ki.search(new Krill(sq));
    assertEquals((long) 1, kr.getTotalResults());
    // Test without matches in within
    sq = kq.within(kq.re("s:z.*e"), kq.seg("s:affe")).toQuery();
    assertEquals("spanContain(SpanMultiTermQueryWrapper(base:/s:z.*e/), base:s:affe)", sq.toString());
    kr = ki.search(new Krill(sq));
    assertEquals((long) 0, kr.getTotalResults());
    // Test without matches in within (reversed)
    sq = kq.within(kq.seg("s:affe"), kq.re("s:z.*e")).toQuery();
    assertEquals("spanContain(base:s:affe, SpanMultiTermQueryWrapper(base:/s:z.*e/))", sq.toString());
    kr = ki.search(new Krill(sq));
    assertEquals((long) 0, kr.getTotalResults());
    // Test with classes
    sq = kq.nr(kq.re("s:z.*e")).toQuery();
    assertEquals("{1: SpanMultiTermQueryWrapper(base:/s:z.*e/)}", sq.toString());
    kr = ki.search(new Krill(sq));
    assertEquals((long) 0, kr.getTotalResults());
    // Test with nested classes
    sq = kq.within(kq.nr(kq.re("s:z.*e")), kq.seg("s:affe")).toQuery();
    assertEquals("spanContain({1: SpanMultiTermQueryWrapper(base:/s:z.*e/)}, base:s:affe)", sq.toString());
    kr = ki.search(new Krill(sq));
    assertEquals((long) 0, kr.getTotalResults());
    // Test with multiple distances
    List<DistanceConstraint> constraints = new ArrayList<DistanceConstraint>();
    constraints.add(TestMultipleDistanceIndex.createConstraint("w", 1, 2, true, false));
    constraints.add(TestMultipleDistanceIndex.createConstraint("tokens", "base/s:s", 0, 0, true, false));
    sq = new SpanMultipleDistanceQuery(kq.re("s:z.*e").toQuery(), kq.seg("s:affe").toQuery(), constraints, true, true);
    assertEquals("spanMultipleDistance(SpanMultiTermQueryWrapper(base:/s:z.*e/), " + "base:s:affe, [(w[1:2], ordered, notExcluded), (" + "base/s:s[0:0], ordered, notExcluded)])", sq.toString());
    kr = ki.search(new Krill(sq));
    assertEquals((long) 0, kr.getTotalResults());
    // Test with multiple distances and a class
    sq = new SpanMultipleDistanceQuery(kq.nr(kq.re("s:z.*e")).toQuery(), kq.seg("s:affe").toQuery(), constraints, true, true);
    assertEquals("spanMultipleDistance({1: SpanMultiTermQueryWrapper(base:/s:z.*e/)}, " + "base:s:affe, [(w[1:2], ordered, notExcluded), (base/s:s[0:0], ordered, notExcluded)])", sq.toString());
    kr = ki.search(new Krill(sq));
    assertEquals((long) 0, kr.getTotalResults());
}
Also used : Krill(de.ids_mannheim.korap.Krill) ArrayList(java.util.ArrayList) DistanceConstraint(de.ids_mannheim.korap.query.DistanceConstraint) QueryBuilder(de.ids_mannheim.korap.query.QueryBuilder) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanMultipleDistanceQuery(de.ids_mannheim.korap.query.SpanMultipleDistanceQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Result(de.ids_mannheim.korap.response.Result) Test(org.junit.Test)

Example 14 with QueryBuilder

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

the class TestWithinIndex method indexExample8.

/**
 * SpanElementQueries
 */
@Test
public void indexExample8() throws QueryException, IOException {
    KrillIndex ki = new KrillIndex();
    FieldDocument fd = new FieldDocument();
    // <a>xx <e>hi j <e>hi j</e></e></a>
    fd.addTV("base", "xx hi j hi j", "[(0-1)s:x|i:x|_0$<i>0<i>1|<>:a$<b>64<i>0<i>12<i>8<b>0]" + "[(1-2)s:x|i:x|_1$<i>1<i>2]" + "[(3-4)s:h|i:h|_2$<i>3<i>4|<>:e$<b>64<i>3<i>12<i>8<b>0]" + "[(4-5)s:i|i:i|_3$<i>4<i>5]" + "[(6-7)s:j|i:j|_4$<i>6<i>7]" + "[(8-9)s:h|i:h|_5$<i>8<i>9|<>:e$<b>64<i>8<i>9<i>8<b>0]" + "[(9-10)s:i|i:i|_6$<i>9<i>10]" + "[(11-12)s:j|i:j|_7$<i>11<i>12]");
    ki.addDoc(fd);
    ki.commit();
    assertEquals(1, ki.numberOf("documents"));
    QueryBuilder qb = new KrillQuery("base").builder();
    SpanQueryWrapper sqw;
    Result kr;
    /*
        sqw = qb.seg("i:x");
        kr = ki.search(sqw.toQuery(), (short) 10);
        assertEquals(2, kr.getTotalResults());
        
        sqw = qb.tag("a");
        kr = ki.search(sqw.toQuery(), (short) 10);
        assertEquals(1, kr.getTotalResults());
        
        sqw = qb.startswith(qb.tag("a"), qb.seg("i:x"));
        assertEquals("spanStartsWith(<base:a />, base:i:x)",
                     sqw.toQuery().toString());
        kr = ki.search(sqw.toQuery(), (short) 10);
        assertEquals(1, kr.getTotalResults());
        */
    sqw = qb.startswith(qb.tag("e"), qb.seg("i:h"));
    assertEquals("spanStartsWith(<base:e />, base:i:h)", sqw.toQuery().toString());
    kr = ki.search(sqw.toQuery(), (short) 10);
    assertEquals(2, kr.getTotalResults());
}
Also used : KrillQuery(de.ids_mannheim.korap.KrillQuery) QueryBuilder(de.ids_mannheim.korap.query.QueryBuilder) 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 15 with QueryBuilder

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

the class TestKrillQuery method KorapSequenceQuery5.

@Test
public void KorapSequenceQuery5() throws QueryException {
    QueryBuilder kq = new QueryBuilder("field");
    SpanQuery sq = kq.seq(kq.seg("try1")).append(kq.seg("try2")).withConstraint(2, 3, true).toQuery();
    assertEquals("spanDistance(field:try1, field:try2, [(w[2:3], ordered, excluded)])", sq.toString());
}
Also used : QueryBuilder(de.ids_mannheim.korap.query.QueryBuilder) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Test(org.junit.Test)

Aggregations

QueryBuilder (de.ids_mannheim.korap.query.QueryBuilder)72 Test (org.junit.Test)67 SpanQuery (org.apache.lucene.search.spans.SpanQuery)39 KrillIndex (de.ids_mannheim.korap.KrillIndex)33 Result (de.ids_mannheim.korap.response.Result)32 Krill (de.ids_mannheim.korap.Krill)27 FieldDocument (de.ids_mannheim.korap.index.FieldDocument)14 SpanQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanQueryWrapper)11 Match (de.ids_mannheim.korap.response.Match)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 KrillQuery (de.ids_mannheim.korap.KrillQuery)5 QueryException (de.ids_mannheim.korap.util.QueryException)5 KrillMeta (de.ids_mannheim.korap.KrillMeta)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 KrillCollection (de.ids_mannheim.korap.KrillCollection)3 TestSimple.getJsonString (de.ids_mannheim.korap.TestSimple.getJsonString)3 CollectionBuilder (de.ids_mannheim.korap.collection.CollectionBuilder)3 SearchContext (de.ids_mannheim.korap.response.SearchContext)3 Test (de.ids_mannheim.korap.Test)2 DistanceConstraint (de.ids_mannheim.korap.query.DistanceConstraint)1