Search in sources :

Example 26 with SpanElementQuery

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

the class TestRelationIndex method testCase10b.

/**
 * Relation whose nodes have a specific attribute. Alternative
 * query (actually used in serialization)
 */
@Test
public void testCase10b() throws IOException {
    ki.addDoc(createFieldDoc2());
    ki.commit();
    SpanAttributeQuery aq = new SpanAttributeQuery(new SpanTermQuery(new Term("base", "@:case=accusative")), true);
    kr = ki.search(aq, (short) 10);
    assertEquals((long) 3, kr.getTotalResults());
    SpanRelationQuery srq = new SpanRelationQuery(new SpanTermQuery(new Term("base", ">:child-of")), true, RelationDirection.RIGHT);
    srq.setSourceClass((byte) 1);
    srq.setTargetClass((byte) 2);
    kr = ki.search(srq, (short) 20);
    assertEquals((long) 13, kr.getTotalResults());
    // Matching relation source node with an attribute
    SpanWithAttributeQuery swaq = new SpanWithAttributeQuery(aq, true);
    SpanSegmentQuery ssq = new SpanSegmentQuery(srq, swaq);
    assertEquals("spanSegment({1: source:{2: target:spanRelation(base:>:child-of)}}, " + "spanWithAttribute(spanAttribute(base:@:case=accusative)))", ssq.toString());
    kr = ki.search(ssq, (short) 10);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals(2, kr.getMatch(0).getStartPos());
    assertEquals(4, kr.getMatch(0).getEndPos());
    assertEquals(5, kr.getMatch(1).getStartPos());
    assertEquals(7, kr.getMatch(1).getEndPos());
    // Matching relation target nodes with an attribute
    // NOTE: swaq must be the first parameter
    SpanFocusQuery sfq2 = new SpanFocusQuery(srq, (byte) 2);
    sfq2.setSorted(false);
    SpanSegmentQuery ssq2 = new SpanSegmentQuery(swaq, sfq2);
    kr = ki.search(ssq2, (short) 10);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals(2, kr.getMatch(0).getStartPos());
    assertEquals(7, kr.getMatch(0).getEndPos());
    assertEquals(5, kr.getMatch(1).getStartPos());
    assertEquals(7, kr.getMatch(1).getEndPos());
    // Matching specific relation source node with an attribute
    SpanElementQuery seq = new SpanElementQuery("base", "np");
    swaq = new SpanWithAttributeQuery(seq, aq, true);
    ssq = new SpanSegmentQuery(srq, swaq);
    assertEquals("spanSegment({1: source:{2: target:spanRelation(base:>:child-of)}}, " + "spanElementWithAttribute(<base:np />, " + "spanAttribute(base:@:case=accusative)))", ssq.toString());
    kr = ki.search(ssq, (short) 10);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals(2, kr.getMatch(0).getStartPos());
    assertEquals(4, kr.getMatch(0).getEndPos());
    assertEquals(5, kr.getMatch(1).getStartPos());
    assertEquals(7, kr.getMatch(1).getEndPos());
}
Also used : SpanAttributeQuery(de.ids_mannheim.korap.query.SpanAttributeQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanRelationQuery(de.ids_mannheim.korap.query.SpanRelationQuery) SpanWithAttributeQuery(de.ids_mannheim.korap.query.SpanWithAttributeQuery) SpanSegmentQuery(de.ids_mannheim.korap.query.SpanSegmentQuery) Term(org.apache.lucene.index.Term) SpanFocusQuery(de.ids_mannheim.korap.query.SpanFocusQuery) SpanElementQuery(de.ids_mannheim.korap.query.SpanElementQuery) Test(org.junit.Test)

Example 27 with SpanElementQuery

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

the class TestAttributeIndex method testCase2.

/**
 * Test multiple attributes and negation
 *
 * @throws IOException
 */
@Test
public void testCase2() throws IOException {
    ki.addDoc(createFieldDoc1());
    ki.commit();
    // header and title
    List<SpanQuery> sql = new ArrayList<>();
    sql.add(new SpanAttributeQuery(new SpanTermQuery(new Term("base", "@:class=header")), true));
    sql.add(new SpanAttributeQuery(new SpanTermQuery(new Term("base", "@:class=title")), true));
    SpanQuery sq = new SpanWithAttributeQuery(new SpanElementQuery("base", "div"), sql, true);
    kr = ki.search(sq, (short) 10);
    assertEquals((long) 4, kr.getTotalResults());
    assertEquals(0, kr.getMatch(0).getStartPos());
    assertEquals(3, kr.getMatch(0).getEndPos());
    assertEquals(1, kr.getMatch(1).getStartPos());
    assertEquals(2, kr.getMatch(1).getEndPos());
    assertEquals(4, kr.getMatch(2).getStartPos());
    assertEquals(5, kr.getMatch(2).getEndPos());
    assertEquals(6, kr.getMatch(3).getStartPos());
    assertEquals(7, kr.getMatch(3).getEndPos());
    // Add not Attribute
    // header and title, not book
    sql.add(new SpanAttributeQuery(new SpanTermQuery(new Term("base", "@:class=book")), true, true));
    sq = new SpanWithAttributeQuery(new SpanElementQuery("base", "div"), sql, true);
    kr = ki.search(sq, (short) 10);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals(1, kr.getMatch(0).getStartPos());
    assertEquals(2, kr.getMatch(0).getEndPos());
    assertEquals(6, kr.getMatch(1).getStartPos());
    assertEquals(7, kr.getMatch(1).getEndPos());
    // Test multiple negations
    // header, not title, not book
    sql.remove(1);
    sql.add(new SpanAttributeQuery(new SpanTermQuery(new Term("base", "@:class=title")), true, true));
    sq = new SpanWithAttributeQuery(new SpanElementQuery("base", "div"), sql, true);
    kr = ki.search(sq, (short) 10);
    assertEquals((long) 1, kr.getTotalResults());
    assertEquals(5, kr.getMatch(0).getStartPos());
    assertEquals(6, kr.getMatch(0).getEndPos());
}
Also used : SpanAttributeQuery(de.ids_mannheim.korap.query.SpanAttributeQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ArrayList(java.util.ArrayList) SpanWithAttributeQuery(de.ids_mannheim.korap.query.SpanWithAttributeQuery) Term(org.apache.lucene.index.Term) SpanElementQuery(de.ids_mannheim.korap.query.SpanElementQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Test(org.junit.Test)

Example 28 with SpanElementQuery

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

the class TestAttributeIndex method testCase1.

/**
 * Test matching elementRef
 *
 * @throws IOException
 */
@Test
public void testCase1() throws IOException {
    ki.addDoc(createFieldDoc0());
    ki.commit();
    SpanAttributeQuery saq = new SpanAttributeQuery(new SpanTermQuery(new Term("base", "@:class=header")), true);
    SpanElementQuery seq = new SpanElementQuery("base", "div");
    // div with @class=header
    SpanQuery sq = new SpanWithAttributeQuery(seq, saq, true);
    kr = ki.search(sq, (short) 10);
    // for (int i = 0; i < kr.getTotalResults(); i++) {
    // System.out.println(kr.getMatch(i).getLocalDocID() + " "
    // + kr.getMatch(i).startPos + " " + kr.getMatch(i).endPos);
    // }
    // 
    assertEquals((long) 4, kr.getTotalResults());
    assertEquals(0, kr.getMatch(0).getStartPos());
    assertEquals(2, kr.getMatch(0).getEndPos());
    assertEquals(0, kr.getMatch(1).getStartPos());
    assertEquals(3, kr.getMatch(1).getEndPos());
    assertEquals(3, kr.getMatch(2).getStartPos());
    assertEquals(5, kr.getMatch(2).getEndPos());
    assertEquals(6, kr.getMatch(3).getStartPos());
    assertEquals(7, kr.getMatch(3).getEndPos());
}
Also used : SpanAttributeQuery(de.ids_mannheim.korap.query.SpanAttributeQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanWithAttributeQuery(de.ids_mannheim.korap.query.SpanWithAttributeQuery) Term(org.apache.lucene.index.Term) SpanElementQuery(de.ids_mannheim.korap.query.SpanElementQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Test(org.junit.Test)

Example 29 with SpanElementQuery

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

the class TestAttributeIndex method testCase4.

/**
 * Test skipto doc for spanWithAttribute
 */
@Test
public void testCase4() throws IOException {
    ki.addDoc(createFieldDoc1());
    ki.addDoc(createFieldDoc0());
    ki.addDoc(createFieldDoc2());
    ki.commit();
    SpanAttributeQuery saq = new SpanAttributeQuery(new SpanTermQuery(new Term("base", "@:class=book")), true);
    List<SpanQuery> sql = new ArrayList<>();
    sql.add(saq);
    SpanWithAttributeQuery sq = new SpanWithAttributeQuery(new SpanElementQuery("base", "div"), sql, true);
    kr = ki.search(sq, (short) 10);
    // for (int i = 0; i < kr.getTotalResults(); i++) {
    // System.out.println(kr.getMatch(i).getLocalDocID() + " "
    // + kr.getMatch(i).startPos + " " + kr.getMatch(i).endPos);
    // }
    assertEquals((long) 6, kr.getTotalResults());
    SpanNextQuery snq = new SpanNextQuery(new SpanTermQuery(new Term("base", "s:e")), sq);
    kr = ki.search(snq, (short) 10);
    assertEquals((long) 1, kr.getTotalResults());
    assertEquals(2, kr.getMatch(0).getLocalDocID());
    assertEquals(1, kr.getMatch(0).getStartPos());
    assertEquals(5, kr.getMatch(0).getEndPos());
}
Also used : SpanAttributeQuery(de.ids_mannheim.korap.query.SpanAttributeQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ArrayList(java.util.ArrayList) SpanWithAttributeQuery(de.ids_mannheim.korap.query.SpanWithAttributeQuery) Term(org.apache.lucene.index.Term) SpanElementQuery(de.ids_mannheim.korap.query.SpanElementQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNextQuery(de.ids_mannheim.korap.query.SpanNextQuery) Test(org.junit.Test)

Example 30 with SpanElementQuery

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

the class TestAttributeIndex method testcase9.

/**
 * Element with only not attributes
 *
 * @throws IOException
 */
@Test
public void testcase9() throws IOException {
    ki.addDoc(createFieldDoc2());
    ki.commit();
    SpanAttributeQuery saq = new SpanAttributeQuery(new SpanTermQuery(new Term("base", "@:class=book")), true, true);
    SpanQuery sq = new SpanWithAttributeQuery(new SpanElementQuery("base", "div"), saq, true);
    kr = ki.search(sq, (short) 10);
    assertEquals(4, kr.getTotalResults());
    assertEquals(1, kr.getMatch(0).getStartPos());
    assertEquals(2, kr.getMatch(0).getEndPos());
    assertEquals(3, kr.getMatch(1).getStartPos());
    assertEquals(5, kr.getMatch(1).getEndPos());
    assertEquals(5, kr.getMatch(2).getStartPos());
    assertEquals(6, kr.getMatch(2).getEndPos());
    assertEquals(6, kr.getMatch(3).getStartPos());
    assertEquals(7, kr.getMatch(3).getEndPos());
    List<SpanQuery> sql = new ArrayList<>();
    sql.add(saq);
    sql.add(new SpanAttributeQuery(new SpanTermQuery(new Term("base", "@:class=header")), true, true));
    sq = new SpanWithAttributeQuery(new SpanElementQuery("base", "div"), sql, true);
    kr = ki.search(sq, (short) 10);
    assertEquals(1, kr.getTotalResults());
    assertEquals(3, kr.getMatch(0).getStartPos());
    assertEquals(5, kr.getMatch(0).getEndPos());
}
Also used : SpanAttributeQuery(de.ids_mannheim.korap.query.SpanAttributeQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanWithAttributeQuery(de.ids_mannheim.korap.query.SpanWithAttributeQuery) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) SpanElementQuery(de.ids_mannheim.korap.query.SpanElementQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Test(org.junit.Test)

Aggregations

SpanElementQuery (de.ids_mannheim.korap.query.SpanElementQuery)57 Test (org.junit.Test)53 KrillIndex (de.ids_mannheim.korap.KrillIndex)37 Term (org.apache.lucene.index.Term)36 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)35 SpanQuery (org.apache.lucene.search.spans.SpanQuery)32 Result (de.ids_mannheim.korap.response.Result)26 SpanClassQuery (de.ids_mannheim.korap.query.SpanClassQuery)16 SpanWithinQuery (de.ids_mannheim.korap.query.SpanWithinQuery)16 DistanceConstraint (de.ids_mannheim.korap.query.DistanceConstraint)11 SpanDistanceQuery (de.ids_mannheim.korap.query.SpanDistanceQuery)11 SpanFocusQuery (de.ids_mannheim.korap.query.SpanFocusQuery)10 SpanNextQuery (de.ids_mannheim.korap.query.SpanNextQuery)9 SpanRelationQuery (de.ids_mannheim.korap.query.SpanRelationQuery)8 SpanAttributeQuery (de.ids_mannheim.korap.query.SpanAttributeQuery)6 SpanClassFilterQuery (de.ids_mannheim.korap.query.SpanClassFilterQuery)6 SpanRelationMatchQuery (de.ids_mannheim.korap.query.SpanRelationMatchQuery)6 SpanWithAttributeQuery (de.ids_mannheim.korap.query.SpanWithAttributeQuery)6 SpanSegmentQuery (de.ids_mannheim.korap.query.SpanSegmentQuery)4 ArrayList (java.util.ArrayList)4