Search in sources :

Example 1 with SpanNextQuery

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

the class TestDistanceExclusionIndex method testCase6.

// Add skipTo test
@Test
public void testCase6() throws IOException {
    ki = new KrillIndex();
    ki.addDoc(createFieldDoc1());
    ki.addDoc(createFieldDoc2());
    ki.commit();
    SpanQuery sq;
    // ordered distance 0 to 1
    sq = createQuery("s:d", "s:b", 0, 1, true);
    kr = ki.search(sq, (short) 10);
    assertEquals((long) 4, kr.getTotalResults());
    SpanTermQuery stq = new SpanTermQuery(new Term("base", "s:c"));
    kr = ki.search(stq, (short) 10);
    assertEquals((long) 6, kr.getTotalResults());
    SpanNextQuery snq = new SpanNextQuery(stq, sq);
    kr = ki.search(snq, (short) 10);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals(3, kr.getMatch(0).getStartPos());
    assertEquals(5, kr.getMatch(0).getEndPos());
    assertEquals(8, kr.getMatch(1).getStartPos());
    assertEquals(10, kr.getMatch(1).getEndPos());
/*System.out.print(kr.getTotalResults()+"\n");
        for (int i=0; i< kr.getTotalResults(); i++){
        	System.out.println(
        		kr.getMatch(i).getLocalDocID()+" "+
        		kr.getMatch(i).startPos + " " +
        		kr.getMatch(i).endPos
            );
        }*/
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNextQuery(de.ids_mannheim.korap.query.SpanNextQuery) Test(org.junit.Test)

Example 2 with SpanNextQuery

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

the class TestElementDistanceExclusionIndex method testCase5.

/**
 * Skip to
 */
@Test
public void testCase5() throws IOException {
    ki = new KrillIndex();
    ki.addDoc(createFieldDoc0());
    ki.addDoc(createFieldDoc1());
    ki.addDoc(createFieldDoc0());
    ki.addDoc(createFieldDoc2());
    ki.commit();
    SpanQuery sq = createQuery("s", "s:c", "s:d", 1, 1, false, true);
    kr = ki.search(sq, (short) 10);
    assertEquals(kr.getTotalResults(), 3);
    assertEquals(3, kr.getMatch(2).getLocalDocID());
    assertEquals(3, kr.getMatch(2).startPos);
    assertEquals(4, kr.getMatch(2).endPos);
    sq = new SpanNextQuery(createQuery("s", "s:c", "s:d", 1, 1, false, true), new SpanTermQuery(new Term("base", "s:a")));
    kr = ki.search(sq, (short) 10);
    assertEquals(kr.getTotalResults(), 1);
    assertEquals(3, kr.getMatch(0).getLocalDocID());
    assertEquals(3, kr.getMatch(0).startPos);
    assertEquals(5, kr.getMatch(0).endPos);
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNextQuery(de.ids_mannheim.korap.query.SpanNextQuery) Test(org.junit.Test)

Example 3 with SpanNextQuery

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

the class TestMatchIndex method testEmbeddedClassQuery.

@Test
public void testEmbeddedClassQuery() 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]" + "[(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();
    SpanQuery sq;
    Result kr;
    sq = new SpanFocusQuery(new SpanClassQuery(new SpanNextQuery(new SpanClassQuery(new SpanTermQuery(new Term("base", "s:b")), (byte) 1), new SpanClassQuery(new SpanTermQuery(new Term("base", "s:c")), (byte) 2)), (byte) 3), (byte) 3);
    kr = ki.search(sq, (short) 10);
    assertEquals("totalResults", kr.getTotalResults(), 2);
    assertEquals("StartPos (0)", 1, kr.getMatch(0).startPos);
    assertEquals("EndPos (0)", 3, kr.getMatch(0).endPos);
    assertEquals("SnippetBrackets (0)", "a[[{3:{1:b}{2:c}}]]abcaba ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("StartPos (1)", 4, kr.getMatch(1).startPos);
    assertEquals("EndPos (1)", 6, kr.getMatch(1).endPos);
    assertEquals("SnippetBrackets (1)", "abca[[{3:{1:b}{2:c}}]]abac", kr.getMatch(1).getSnippetBrackets());
    assertEquals("Document count", 1, ki.numberOf("base", "documents"));
    assertEquals("Token count", 10, ki.numberOf("base", "t"));
}
Also used : SpanClassQuery(de.ids_mannheim.korap.query.SpanClassQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanFocusQuery(de.ids_mannheim.korap.query.SpanFocusQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Result(de.ids_mannheim.korap.response.Result) SpanNextQuery(de.ids_mannheim.korap.query.SpanNextQuery) Test(org.junit.Test)

Example 4 with SpanNextQuery

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

the class SpanSequenceQueryWrapper method toFragmentQuery.

/**
 * Serialize the wrapped sequence to a {@link SpanQuery} object.
 *
 * @return A {@link SpanQuery} object.
 * @throws QueryException
 */
public SpanQuery toFragmentQuery() throws QueryException {
    // There was a serialization failure not yet reported
    if (this.constraintException != null)
        throw constraintException;
    int size = this.segments.size();
    // Nothing to do
    if (size == 0 || this.isNull())
        return (SpanQuery) null;
    // No real sequence - only one element
    if (size == 1) {
        // But the element may be expanded
        if (this.segments.get(0).isExtended() && (this.hasConstraints() || !this.isInOrder())) {
            throw new QueryException(613, limitationError);
        }
        ;
        // Unproblematic single query
        if (this.segments.get(0).maybeAnchor())
            return (SpanQuery) this.segments.get(0).retrieveNode(this.retrieveNode).toFragmentQuery();
        if (this.segments.get(0).isEmpty())
            throw new QueryException(613, "Sequence is not allowed to be empty");
        if (this.segments.get(0).isOptional())
            throw new QueryException(613, "Sequence is not allowed to be optional");
        if (this.segments.get(0).isNegative())
            throw new QueryException(613, "Sequence is not allowed to be negative");
    }
    ;
    if (!this.isSolved) {
        if (!_solveProblematicSequence()) {
            if (this.segments.get(0).maybeExtension()) {
                throw new QueryException(613, "Sequence contains unresolvable " + "empty, optional, or negative segments");
            }
            ;
        }
        ;
    }
    ;
    // The element may be expanded
    if (this.segments.size() == 1 && this.segments.get(0).isExtended() && (this.hasConstraints() || !this.isInOrder())) {
        throw new QueryException(613, limitationError);
    }
    ;
    // Create the initial query
    SpanQuery query = null;
    int i = 0;
    // Get the first valid segment
    while (query == null && i < this.segments.size()) {
        query = this.segments.get(i).retrieveNode(this.retrieveNode).toFragmentQuery();
        i++;
    }
    ;
    // No valid segment found
    if (query == null)
        return (SpanQuery) null;
    // NextQueries
    if (!this.hasConstraints() && this.isInOrder()) {
        for (; i < this.segments.size(); i++) {
            // Get the first query for next sequence
            SpanQuery second = this.segments.get(i).retrieveNode(this.retrieveNode).toFragmentQuery();
            if (second == null)
                continue;
            query = new SpanNextQuery(query, second);
        }
        ;
        return (SpanQuery) query;
    }
    ;
    // DistanceQueries with problems
    if (this.hasConstraints() && this.isProblematic) {
        throw new QueryException(613, "Distance constraints not supported with empty, optional or negative operands");
    }
    ;
    // DistanceQueries
    if (this.constraints.size() == 1) {
        DistanceConstraint constraint = this.constraints.get(0);
        // Create spanElementDistance query
        if (!constraint.getUnit().equals("w")) {
            for (i = 1; i < this.segments.size(); i++) {
                // No support for extended spans in constraints
                if (this.segments.get(i).isExtended())
                    throw new QueryException(613, limitationError);
                /* Maybe important
                    if (this.segments.get(i).isOptional())
                        throw new QueryException(613, limitationError);
                    */
                SpanQuery sq = (SpanQuery) this.segments.get(i).retrieveNode(this.retrieveNode).toFragmentQuery();
                if (sq == null)
                    continue;
                SpanDistanceQuery sdquery = new SpanDistanceQuery(query, sq, constraint, true);
                query = (SpanQuery) sdquery;
            }
            ;
        } else // Create spanDistance query
        {
            for (i = 1; i < this.segments.size(); i++) {
                // No support for extended spans in constraints
                if (this.segments.get(i).isExtended())
                    throw new QueryException(613, limitationError);
                /* May be necessary
                    if (this.segments.get(i).isOptional())
                        throw new QueryException(613, limitationError);
                    */
                SpanQuery sq = (SpanQuery) this.segments.get(i).retrieveNode(this.retrieveNode).toFragmentQuery();
                if (sq == null)
                    continue;
                SpanDistanceQuery sdquery = new SpanDistanceQuery(query, sq, constraint, true);
                query = (SpanQuery) sdquery;
            }
            ;
        }
        ;
        return (SpanQuery) query;
    }
    ;
    // MultipleDistanceQueries
    for (i = 1; i < this.segments.size(); i++) {
        // No support for extended spans in constraints
        if (this.segments.get(i).isExtended())
            throw new QueryException(613, limitationError);
        SpanQuery sq = (SpanQuery) this.segments.get(i).retrieveNode(this.retrieveNode).toFragmentQuery();
        if (sq == null)
            continue;
        query = new SpanMultipleDistanceQuery(query, sq, this.constraints, isInOrder, true);
    }
    ;
    return (SpanQuery) query;
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) SpanDistanceQuery(de.ids_mannheim.korap.query.SpanDistanceQuery) DistanceConstraint(de.ids_mannheim.korap.query.DistanceConstraint) DistanceConstraint(de.ids_mannheim.korap.query.DistanceConstraint) SpanMultipleDistanceQuery(de.ids_mannheim.korap.query.SpanMultipleDistanceQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNextQuery(de.ids_mannheim.korap.query.SpanNextQuery)

Example 5 with SpanNextQuery

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

the class TestMultipleDistanceIndex method testCase4.

/**
 * Skip to
 */
@Test
public void testCase4() throws IOException {
    ki = new KrillIndex();
    ki.addDoc(createFieldDoc0());
    ki.addDoc(createFieldDoc3());
    ki.addDoc(createFieldDoc1());
    ki.addDoc(createFieldDoc2());
    ki.commit();
    List<DistanceConstraint> constraints = new ArrayList<DistanceConstraint>();
    constraints.add(createConstraint("w", 1, 2, false, false));
    constraints.add(createConstraint("s", 1, 2, false, false));
    SpanQuery mdq;
    mdq = createQuery("s:b", "s:c", constraints, false);
    SpanQuery sq = new SpanNextQuery(mdq, new SpanTermQuery(new Term("base", "s:e")));
    kr = ki.search(sq, (short) 10);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals(3, kr.getMatch(0).getStartPos());
    assertEquals(6, kr.getMatch(0).getEndPos());
    assertEquals(3, kr.getMatch(1).getLocalDocID());
    assertEquals(1, kr.getMatch(1).getStartPos());
    assertEquals(5, kr.getMatch(1).getEndPos());
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ArrayList(java.util.ArrayList) DistanceConstraint(de.ids_mannheim.korap.query.DistanceConstraint) Term(org.apache.lucene.index.Term) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNextQuery(de.ids_mannheim.korap.query.SpanNextQuery) Test(org.junit.Test)

Aggregations

SpanNextQuery (de.ids_mannheim.korap.query.SpanNextQuery)36 Test (org.junit.Test)35 Term (org.apache.lucene.index.Term)34 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)34 KrillIndex (de.ids_mannheim.korap.KrillIndex)31 SpanQuery (org.apache.lucene.search.spans.SpanQuery)31 Result (de.ids_mannheim.korap.response.Result)15 SpanElementQuery (de.ids_mannheim.korap.query.SpanElementQuery)9 SpanClassQuery (de.ids_mannheim.korap.query.SpanClassQuery)6 SpanFocusQuery (de.ids_mannheim.korap.query.SpanFocusQuery)6 SpanRepetitionQuery (de.ids_mannheim.korap.query.SpanRepetitionQuery)6 DistanceConstraint (de.ids_mannheim.korap.query.DistanceConstraint)3 SpanWithinQuery (de.ids_mannheim.korap.query.SpanWithinQuery)3 SpanDistanceQuery (de.ids_mannheim.korap.query.SpanDistanceQuery)2 SpanRelationQuery (de.ids_mannheim.korap.query.SpanRelationQuery)2 SpanSegmentQuery (de.ids_mannheim.korap.query.SpanSegmentQuery)2 ArrayList (java.util.ArrayList)2 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)2 Krill (de.ids_mannheim.korap.Krill)1 KrillQuery (de.ids_mannheim.korap.KrillQuery)1