Search in sources :

Example 6 with SpanDistanceQuery

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

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

Example 8 with SpanDistanceQuery

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

the class TestWPDIndex method createElementDistanceQuery.

private SpanDistanceQuery createElementDistanceQuery(String e, String x, String y, int min, int max, boolean isOrdered, boolean exclusion) {
    SpanElementQuery eq = new SpanElementQuery("tokens", e);
    SpanDistanceQuery sq = new SpanDistanceQuery(new SpanTermQuery(new Term("tokens", x)), new SpanTermQuery(new Term("tokens", y)), new DistanceConstraint(eq, min, max, isOrdered, exclusion), true);
    return sq;
}
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) SpanElementQuery(de.ids_mannheim.korap.query.SpanElementQuery)

Example 9 with SpanDistanceQuery

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

the class TestWPDIndex method testCase1.

/**
 * Token distance spans
 */
@Test
public void testCase1() throws IOException {
    SpanDistanceQuery sq;
    // ordered
    sq = createDistanceQuery("s:Wir", "s:kommen", 1, 1, true, false);
    ks = new Krill(sq);
    kr = ks.apply(ki);
    assertEquals(kr.getTotalResults(), 8);
    // unordered
    sq = createDistanceQuery("s:Wir", "s:kommen", 1, 1, false, false);
    ks = new Krill(sq);
    kr = ks.apply(ki);
    assertEquals(kr.getTotalResults(), 11);
    sq = createDistanceQuery("s:kommen", "s:Wir", 1, 1, false, false);
    ks = new Krill(sq);
    kr = ks.apply(ki);
    assertEquals(kr.getTotalResults(), 11);
// System.out.println(kr.getTotalResults());
// for (Match km : kr.getMatches()){
// System.out.println(km.getDocID() +" "+km.getStartPos() +" "+ km.getEndPos());
// System.out.println(km.getSnippetBrackets());
// System.out.println(km.toJSON());
// }
}
Also used : Krill(de.ids_mannheim.korap.Krill) SpanDistanceQuery(de.ids_mannheim.korap.query.SpanDistanceQuery) Test(org.junit.Test)

Example 10 with SpanDistanceQuery

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

the class TestSampleIndex method testMultipleDistanceWithWildcards.

@Test
public void testMultipleDistanceWithWildcards() throws IOException, QueryException {
    WildcardQuery wcquery = new WildcardQuery(new Term("tokens", "s:meine*"));
    SpanMultiTermQueryWrapper<WildcardQuery> mtq = new SpanMultiTermQueryWrapper<WildcardQuery>(wcquery);
    // meine* /+w1:2 &Erfahrung
    SpanQuery tdq = new SpanDistanceQuery(mtq, sq, TestMultipleDistanceIndex.createConstraint("w", 1, 2, true, false), true);
    kr = sample.search(tdq, (short) 10);
    assertEquals(4, kr.getMatches().size());
    assertEquals(107, kr.getMatch(0).getStartPos());
    assertEquals(109, kr.getMatch(0).getEndPos());
    assertEquals(132566, kr.getMatch(1).getStartPos());
    assertEquals(132569, kr.getMatch(1).getEndPos());
    assertEquals(161393, kr.getMatch(2).getStartPos());
    assertEquals(161396, kr.getMatch(2).getEndPos());
    assertEquals(10298, kr.getMatch(3).getStartPos());
    assertEquals(10301, kr.getMatch(3).getEndPos());
    // meine* /+s0 &Erfahrung
    SpanQuery edq = new SpanDistanceQuery(mtq, sq, TestMultipleDistanceIndex.createConstraint("tokens", "base/s:s", 0, 0, true, false), true);
    kr = sample.search(edq, (short) 20);
    assertEquals(18, kr.getMatches().size());
    // meine* /+w1:2,s0 &Erfahrung
    SpanQuery mdsq = new SpanMultipleDistanceQuery(new SpanClassQuery(mtq, (byte) 129), new SpanClassQuery(sq, (byte) 129), constraints, true, true);
    kr = sample.search(mdsq, (short) 10);
    assertEquals(4, kr.getMatches().size());
    // check SpanQueryWrapper generated query
    SpanQueryWrapper sqwi = getJSONQuery(getClass().getResource("/queries/bugs/cosmas_wildcards.jsonld").getFile());
    SpanQuery jsq = sqwi.toQuery();
    assertEquals(mdsq.toString(), jsq.toString());
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) SpanMultiTermQueryWrapper(org.apache.lucene.search.spans.SpanMultiTermQueryWrapper) SpanClassQuery(de.ids_mannheim.korap.query.SpanClassQuery) SpanDistanceQuery(de.ids_mannheim.korap.query.SpanDistanceQuery) Term(org.apache.lucene.index.Term) SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper) SpanMultipleDistanceQuery(de.ids_mannheim.korap.query.SpanMultipleDistanceQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Test(org.junit.Test)

Aggregations

SpanDistanceQuery (de.ids_mannheim.korap.query.SpanDistanceQuery)24 Test (org.junit.Test)21 DistanceConstraint (de.ids_mannheim.korap.query.DistanceConstraint)19 KrillIndex (de.ids_mannheim.korap.KrillIndex)13 Term (org.apache.lucene.index.Term)13 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)12 SpanElementQuery (de.ids_mannheim.korap.query.SpanElementQuery)11 SpanClassQuery (de.ids_mannheim.korap.query.SpanClassQuery)9 SpanQuery (org.apache.lucene.search.spans.SpanQuery)8 SpanClassFilterQuery (de.ids_mannheim.korap.query.SpanClassFilterQuery)6 Krill (de.ids_mannheim.korap.Krill)4 SpanSubspanQuery (de.ids_mannheim.korap.query.SpanSubspanQuery)3 ArrayList (java.util.ArrayList)3 SpanFocusQuery (de.ids_mannheim.korap.query.SpanFocusQuery)2 SpanMultipleDistanceQuery (de.ids_mannheim.korap.query.SpanMultipleDistanceQuery)2 SpanNextQuery (de.ids_mannheim.korap.query.SpanNextQuery)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 SpanQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanQueryWrapper)2