Search in sources :

Example 36 with QueryBuilder

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

the class TestResult method checkJSONTokenResult.

@Test
public void checkJSONTokenResult() throws Exception {
    KrillIndex ki = new KrillIndex();
    FieldDocument fd = new FieldDocument();
    fd.addString("ID", "doc-1");
    fd.addString("UID", "1");
    fd.addTV("base", "abab", "[(0-1)s:a|i:a|_0#0-1|-:t$<i>4]" + "[(1-2)s:b|i:b|_1#1-2]" + "[(2-3)s:a|i:c|_2#2-3]" + "[(3-4)s:b|i:a|_3#3-4]");
    ki.addDoc(fd);
    fd = new FieldDocument();
    fd.addString("ID", "doc-2");
    fd.addString("UID", "2");
    fd.addTV("base", "aba", "[(0-1)s:a|i:a|_0#0-1|-:t$<i>3]" + "[(1-2)s:b|i:b|_1#1-2]" + "[(2-3)s:a|i:c|_2#2-3]");
    ki.addDoc(fd);
    // Commit!
    ki.commit();
    QueryBuilder kq = new QueryBuilder("base");
    SpanQuery q = (SpanQuery) kq.seq(kq.seg("s:a")).append(kq.seg("s:b")).toQuery();
    Result kr = ki.search(q);
    assertEquals((long) 3, kr.getTotalResults());
    ObjectMapper mapper = new ObjectMapper();
    JsonNode res = mapper.readTree(kr.toTokenListJsonString());
    assertEquals(3, res.at("/meta/totalResults").asInt());
    assertEquals("spanNext(base:s:a, base:s:b)", res.at("/meta/serialQuery").asText());
    assertEquals(0, res.at("/meta/startIndex").asInt());
    assertEquals(25, res.at("/meta/itemsPerPage").asInt());
    assertEquals("doc-1", res.at("/matches/0/textSigle").asText());
    assertEquals(0, res.at("/matches/0/tokens/0/0").asInt());
    assertEquals(1, res.at("/matches/0/tokens/0/1").asInt());
    assertEquals(1, res.at("/matches/0/tokens/1/0").asInt());
    assertEquals(2, res.at("/matches/0/tokens/1/1").asInt());
    assertEquals("doc-1", res.at("/matches/1/textSigle").asText());
    assertEquals(2, res.at("/matches/1/tokens/0/0").asInt());
    assertEquals(3, res.at("/matches/1/tokens/0/1").asInt());
    assertEquals(3, res.at("/matches/1/tokens/1/0").asInt());
    assertEquals(4, res.at("/matches/1/tokens/1/1").asInt());
    assertEquals("doc-2", res.at("/matches/2/textSigle").asText());
    assertEquals(0, res.at("/matches/2/tokens/0/0").asInt());
    assertEquals(1, res.at("/matches/2/tokens/0/1").asInt());
    assertEquals(1, res.at("/matches/2/tokens/1/0").asInt());
    assertEquals(2, res.at("/matches/2/tokens/1/1").asInt());
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) QueryBuilder(de.ids_mannheim.korap.query.QueryBuilder) FieldDocument(de.ids_mannheim.korap.index.FieldDocument) KrillIndex(de.ids_mannheim.korap.KrillIndex) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Result(de.ids_mannheim.korap.response.Result) Test(org.junit.Test) Test(de.ids_mannheim.korap.Test)

Example 37 with QueryBuilder

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

the class TestElementDistanceIndex method jsonQuery.

public static SpanQueryWrapper jsonQuery(String jsonFile) {
    SpanQueryWrapper sqwi;
    try {
        String json = getString(jsonFile);
        sqwi = new KrillQuery("tokens").fromKoral(json);
    } catch (QueryException e) {
        fail(e.getMessage());
        sqwi = new QueryBuilder("tokens").seg("???");
    }
    return sqwi;
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) KrillQuery(de.ids_mannheim.korap.KrillQuery) SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper) QueryBuilder(de.ids_mannheim.korap.query.QueryBuilder)

Example 38 with QueryBuilder

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

the class TestFieldDocument method jsonQuery.

public static SpanQueryWrapper jsonQuery(String jsonFile) {
    SpanQueryWrapper sqwi;
    try {
        String json = getString(jsonFile);
        sqwi = new KrillQuery("tokens").fromKoral(json);
    } catch (QueryException e) {
        fail(e.getMessage());
        sqwi = new QueryBuilder("tokens").seg("???");
    }
    ;
    return sqwi;
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) KrillQuery(de.ids_mannheim.korap.KrillQuery) SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper) QueryBuilder(de.ids_mannheim.korap.query.QueryBuilder)

Example 39 with QueryBuilder

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

the class TestMatchIndex method indexExample3.

@Test
public void indexExample3() throws Exception {
    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]" + "[(2-3)s:c|i:c|_2$<i>2<i>3]" + "[(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();
    Result kr;
    QueryBuilder kq = new QueryBuilder("base");
    SpanQuery sq = kq.nr(1, kq.seq(kq.seg("s:b")).append(kq.seg("s:a")).append(kq.nr(2, kq.seg("s:c")))).toQuery();
    kr = ki.search(sq, 0, (short) 20, true, (short) 2, true, (short) 5);
    assertEquals("totalResults", kr.getTotalResults(), 1);
    assertEquals("SnippetBrackets (0)", "... ca[[{1:ba{2:c}}]]", kr.getMatch(0).getSnippetBrackets());
}
Also used : QueryBuilder(de.ids_mannheim.korap.query.QueryBuilder) KrillIndex(de.ids_mannheim.korap.KrillIndex) Result(de.ids_mannheim.korap.response.Result) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Test(org.junit.Test)

Example 40 with QueryBuilder

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

the class TestHighlight method checkHighlights.

@Test
public void checkHighlights() throws IOException, QueryException {
    KrillIndex ki = new KrillIndex();
    String json = new String("{" + "  \"fields\" : [" + "    { " + "      \"primaryData\" : \"abc\"" + "    }," + "    {" + "      \"name\" : \"tokens\"," + "      \"data\" : [" + "         [ \"s:a\", \"i:a\", \"_0#0-1\", \"-:t$<i>3\"]," + "         [ \"s:b\", \"i:b\", \"_1#1-2\" ]," + "         [ \"s:c\", \"i:c\", \"_2#2-3\" ]" + "      ]" + "    }" + "  ]" + "}");
    FieldDocument fd = ki.addDoc(json);
    ki.commit();
    QueryBuilder kq = new QueryBuilder("tokens");
    Result kr = ki.search((SpanQuery) kq.seq(kq.nr(1, kq.seg("s:b"))).toQuery());
    Match km = kr.getMatch(0);
    assertEquals(km.getStartPos(), 1);
    assertEquals(km.getEndPos(), 2);
    assertEquals(km.getStartPos(1), 1);
    assertEquals(km.getEndPos(1), 2);
    assertEquals("<span class=\"context-left\">a</span><span class=\"match\"><mark><mark class=\"class-1 level-0\">b</mark></mark></span><span class=\"context-right\">c</span>", km.getSnippetHTML());
    kr = ki.search((SpanQuery) kq.seq(kq.nr(1, kq.seg("s:b"))).append(kq.nr(2, kq.seg("s:c"))).toQuery());
    km = kr.getMatch(0);
    assertEquals(km.getStartPos(), 1);
    assertEquals(km.getEndPos(), 3);
    assertEquals(km.getStartPos(1), 1);
    assertEquals(km.getEndPos(1), 2);
    assertEquals(km.getStartPos(2), 2);
    assertEquals(km.getEndPos(2), 3);
    assertEquals("<span class=\"context-left\">a</span><span class=\"match\"><mark><mark class=\"class-1 level-0\">b</mark><mark class=\"class-2 level-0\">c</mark></mark></span><span class=\"context-right\"></span>", km.getSnippetHTML());
    kr = ki.search((SpanQuery) kq.seq(kq.nr(1, kq.seq(kq.seg("s:a")).append(kq.seg("s:b")))).append(kq.nr(2, kq.seg("s:c"))).toQuery());
    km = kr.getMatch(0);
    assertEquals(km.getStartPos(), 0);
    assertEquals(km.getEndPos(), 3);
    assertEquals(km.getStartPos(1), 0);
    assertEquals(km.getEndPos(1), 2);
    assertEquals(km.getStartPos(2), 2);
    assertEquals(km.getEndPos(2), 3);
    assertEquals("<span class=\"context-left\"></span><span class=\"match\"><mark><mark class=\"class-1 level-0\">ab</mark><mark class=\"class-2 level-0\">c</mark></mark></span><span class=\"context-right\"></span>", km.getSnippetHTML());
    kr = ki.search((SpanQuery) kq.nr(3, kq.seq(kq.nr(1, kq.seq(kq.seg("s:a")).append(kq.seg("s:b")))).append(kq.nr(2, kq.seg("s:c")))).toQuery());
    km = kr.getMatch(0);
    assertEquals(km.getStartPos(), 0);
    assertEquals(km.getEndPos(), 3);
    assertEquals(km.getStartPos(1), 0);
    assertEquals(km.getEndPos(1), 2);
    assertEquals(km.getStartPos(2), 2);
    assertEquals(km.getEndPos(2), 3);
    assertEquals(km.getStartPos(3), 0);
    assertEquals(km.getEndPos(3), 3);
    assertEquals("<span class=\"context-left\"></span><span class=\"match\"><mark><mark class=\"class-3 level-0\"><mark class=\"class-1 level-1\">ab</mark><mark class=\"class-2 level-1\">c</mark></mark></mark></span><span class=\"context-right\"></span>", km.getSnippetHTML());
}
Also used : QueryBuilder(de.ids_mannheim.korap.query.QueryBuilder) FieldDocument(de.ids_mannheim.korap.index.FieldDocument) KrillIndex(de.ids_mannheim.korap.KrillIndex) Result(de.ids_mannheim.korap.response.Result) Match(de.ids_mannheim.korap.response.Match) 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