Search in sources :

Example 6 with DistanceConstraint

use of de.ids_mannheim.korap.query.DistanceConstraint 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 7 with DistanceConstraint

use of de.ids_mannheim.korap.query.DistanceConstraint 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)

Example 8 with DistanceConstraint

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

the class TestMultipleDistanceIndex method testCase5.

/**
 * Same tokens: unordered yields twice the same results as ordered
 */
@Test
public void testCase5() throws IOException {
    ki = new KrillIndex();
    ki.addDoc(createFieldDoc0());
    ki.addDoc(createFieldDoc1());
    ki.commit();
    // ordered
    List<DistanceConstraint> constraints = new ArrayList<DistanceConstraint>();
    constraints.add(createConstraint("w", 1, 2, true, false));
    constraints.add(createConstraint("s", 1, 2, true, false));
    SpanQuery mdq;
    mdq = createQuery("s:c", "s:c", constraints, false);
    kr = ki.search(mdq, (short) 10);
    assertEquals((long) 4, kr.getTotalResults());
    assertEquals(1, kr.getMatch(0).getStartPos());
    assertEquals(3, kr.getMatch(0).getEndPos());
    assertEquals(2, kr.getMatch(1).getStartPos());
    assertEquals(5, kr.getMatch(1).getEndPos());
    assertEquals(1, kr.getMatch(2).getLocalDocID());
    assertEquals(1, kr.getMatch(2).getStartPos());
    assertEquals(4, kr.getMatch(2).getEndPos());
    assertEquals(3, kr.getMatch(3).getStartPos());
    assertEquals(6, kr.getMatch(3).getEndPos());
    // unordered
    constraints = new ArrayList<DistanceConstraint>();
    constraints.add(createConstraint("w", 1, 2, false, false));
    constraints.add(createConstraint("s", 1, 2, false, false));
    mdq = createQuery("s:c", "s:c", constraints, false);
    kr = ki.search(mdq, (short) 10);
    assertEquals((long) 8, kr.getTotalResults());
}
Also used : ArrayList(java.util.ArrayList) DistanceConstraint(de.ids_mannheim.korap.query.DistanceConstraint) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Test(org.junit.Test)

Example 9 with DistanceConstraint

use of de.ids_mannheim.korap.query.DistanceConstraint 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 10 with DistanceConstraint

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

the class TestUnorderedDistanceIndex method testCase7.

/**
 * Nested distance queries
 */
@Test
public void testCase7() throws IOException {
    // System.out.println("testcase 7");
    ki = new KrillIndex();
    ki.addDoc(createFieldDoc0());
    ki.addDoc(createFieldDoc1());
    ki.commit();
    SpanQuery sq = createQuery("s:c", "s:d", 1, 2, false);
    SpanQuery sq2 = new SpanDistanceQuery(sq, new SpanTermQuery(new Term("base", "s:e")), new DistanceConstraint(1, 2, true, false), true);
    kr = ki.search(sq2, (short) 10);
    assertEquals(kr.getTotalResults(), 3);
    assertEquals(5, kr.getMatch(0).getStartPos());
    assertEquals(9, kr.getMatch(0).getEndPos());
    assertEquals(1, kr.getMatch(1).getLocalDocID());
    assertEquals(0, kr.getMatch(1).getStartPos());
    assertEquals(3, kr.getMatch(1).getEndPos());
    assertEquals(0, kr.getMatch(2).getStartPos());
    assertEquals(4, kr.getMatch(2).getEndPos());
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanDistanceQuery(de.ids_mannheim.korap.query.SpanDistanceQuery) 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) Test(org.junit.Test)

Aggregations

DistanceConstraint (de.ids_mannheim.korap.query.DistanceConstraint)26 Test (org.junit.Test)23 KrillIndex (de.ids_mannheim.korap.KrillIndex)20 SpanDistanceQuery (de.ids_mannheim.korap.query.SpanDistanceQuery)19 Term (org.apache.lucene.index.Term)13 SpanQuery (org.apache.lucene.search.spans.SpanQuery)13 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)13 SpanElementQuery (de.ids_mannheim.korap.query.SpanElementQuery)11 ArrayList (java.util.ArrayList)10 SpanClassQuery (de.ids_mannheim.korap.query.SpanClassQuery)8 SpanClassFilterQuery (de.ids_mannheim.korap.query.SpanClassFilterQuery)6 SpanMultipleDistanceQuery (de.ids_mannheim.korap.query.SpanMultipleDistanceQuery)3 SpanNextQuery (de.ids_mannheim.korap.query.SpanNextQuery)3 SpanSubspanQuery (de.ids_mannheim.korap.query.SpanSubspanQuery)3 SpanFocusQuery (de.ids_mannheim.korap.query.SpanFocusQuery)2 SpanReferenceQuery (de.ids_mannheim.korap.query.SpanReferenceQuery)2 SpanRelationMatchQuery (de.ids_mannheim.korap.query.SpanRelationMatchQuery)2 SpanRelationQuery (de.ids_mannheim.korap.query.SpanRelationQuery)2 Krill (de.ids_mannheim.korap.Krill)1 QueryBuilder (de.ids_mannheim.korap.query.QueryBuilder)1