Search in sources :

Example 91 with SpanQueryWrapper

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

the class KrillQuery method _handleAttrGroup.

// Deserialize attribute groups
private SpanQueryWrapper _handleAttrGroup(SpanQueryWrapper elementWithIdWrapper, JsonNode attrNode) throws QueryException {
    if (!attrNode.has("relation")) {
        throw new QueryException(743, "Term group expects a relation");
    }
    if (!attrNode.has("operands")) {
        throw new QueryException(742, "Term group needs operand list");
    }
    String relation = attrNode.get("relation").asText();
    JsonNode operands = attrNode.get("operands");
    SpanQueryWrapper attrWrapper;
    if ("relation:and".equals(relation)) {
        List<SpanQueryWrapper> wrapperList = new ArrayList<SpanQueryWrapper>();
        for (JsonNode operand : operands) {
            attrWrapper = _termFromJson(operand);
            if (attrWrapper == null) {
                throw new QueryException(747, "Attribute is null");
            }
            wrapperList.add(attrWrapper);
        }
        if (elementWithIdWrapper != null) {
            return new SpanWithAttributeQueryWrapper(elementWithIdWrapper, wrapperList);
        } else {
            return new SpanWithAttributeQueryWrapper(wrapperList);
        }
    } else if ("relation:or".equals(relation)) {
        SpanAlterQueryWrapper saq = new SpanAlterQueryWrapper(field);
        SpanWithAttributeQueryWrapper saqw;
        for (JsonNode operand : operands) {
            attrWrapper = _termFromJson(operand);
            if (attrWrapper == null) {
                throw new QueryException(747, "Attribute is null");
            }
            if (elementWithIdWrapper != null) {
                saqw = new SpanWithAttributeQueryWrapper(elementWithIdWrapper, attrWrapper);
            } else {
                saqw = new SpanWithAttributeQueryWrapper(attrWrapper);
            }
            saq.or(saqw);
        }
        return saq;
    } else {
        throw new QueryException(716, "Unknown relation");
    }
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) SpanAlterQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanAlterQueryWrapper) ArrayList(java.util.ArrayList) SpanWithAttributeQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanWithAttributeQueryWrapper) JsonNode(com.fasterxml.jackson.databind.JsonNode) SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper)

Example 92 with SpanQueryWrapper

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

the class KrillQuery method _operationRelationFromJson.

private SpanQueryWrapper _operationRelationFromJson(JsonNode operands, JsonNode relation) throws QueryException {
    if (operands.size() < 2) {
        throw new QueryException(705, "Number of operands is not acceptable");
    }
    SpanQueryWrapper operand1 = this._fromKoral(operands.get(0), true);
    SpanQueryWrapper operand2 = this._fromKoral(operands.get(1), true);
    RelationDirection direction;
    if (operand1.isEmpty() && !operand2.isEmpty()) {
        // "<:";
        direction = RelationDirection.LEFT;
    } else {
        // ">:"
        direction = RelationDirection.RIGHT;
    }
    if (!relation.has("@type")) {
        throw new QueryException(701, "JSON-LD group has no @type attribute");
    }
    if (relation.get("@type").asText().equals("koral:relation")) {
        SpanRelationWrapper spanRelationWrapper;
        SpanQueryWrapper relationTermWrapper;
        if (!relation.has("wrap")) {
            throw new QueryException(718, "Missing relation term.");
        } else {
            relationTermWrapper = _termFromJson(relation.get("wrap"), false, direction);
            spanRelationWrapper = new SpanRelationWrapper(relationTermWrapper, operand1, operand2);
        }
        spanRelationWrapper.setDirection(direction);
        return spanRelationWrapper;
    } else {
        throw new QueryException(713, "Query type is not supported");
    }
// if (relation.has("boundary")){
// _operationRepetitionFromJson(relation, operands);
// }
// else{
// 
// }
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) SpanRelationWrapper(de.ids_mannheim.korap.query.wrap.SpanRelationWrapper) RelationDirection(de.ids_mannheim.korap.constants.RelationDirection) SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper)

Example 93 with SpanQueryWrapper

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

the class KrillQuery method _segFromJson.

// Deserialize koral:token
private SpanQueryWrapper _segFromJson(JsonNode json) throws QueryException {
    if (!json.has("@type"))
        throw new QueryException(701, "JSON-LD group has no @type attribute");
    String type = json.get("@type").asText();
    if (DEBUG)
        log.trace("Wrap new token definition by {}", type);
    // Branch on type
    switch(type) {
        case "koral:term":
            // case "match:eq":
            return this._termFromJson(json);
        case "koral:termGroup":
            if (!json.has("operands"))
                throw new QueryException(742, "Term group needs operand list");
            // Get operands
            JsonNode operands = json.get("operands");
            SpanSegmentQueryWrapper ssegqw = this.builder().seg();
            if (!json.has("relation"))
                throw new QueryException(743, "Term group expects a relation");
            switch(json.get("relation").asText()) {
                case "relation:and":
                    for (JsonNode operand : operands) {
                        SpanQueryWrapper part = this._segFromJson(operand);
                        if (part instanceof SpanAlterQueryWrapper) {
                            ssegqw.with((SpanAlterQueryWrapper) part);
                        } else if (part instanceof SpanRegexQueryWrapper) {
                            ssegqw.with((SpanRegexQueryWrapper) part);
                        } else if (part instanceof SpanSegmentQueryWrapper) {
                            ssegqw.with((SpanSegmentQueryWrapper) part);
                        } else {
                            throw new QueryException(744, "Operand not supported in term group");
                        }
                        ;
                    }
                    ;
                    return ssegqw;
                case "relation:or":
                    SpanAlterQueryWrapper ssaq = new SpanAlterQueryWrapper(this.field);
                    for (JsonNode operand : operands) {
                        ssaq.or(this._segFromJson(operand));
                    }
                    ;
                    return ssaq;
            }
            ;
    }
    ;
    throw new QueryException(745, "Token type is not supported");
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) SpanRegexQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanRegexQueryWrapper) SpanSegmentQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanSegmentQueryWrapper) SpanAlterQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanAlterQueryWrapper) JsonNode(com.fasterxml.jackson.databind.JsonNode) SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper)

Example 94 with SpanQueryWrapper

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

the class TestMultipleDistanceIndex method queryJSONwildcardNoFoundry.

@Test
public void queryJSONwildcardNoFoundry() throws QueryException, IOException {
    // meine*
    ki = new KrillIndex();
    ki.addDoc(createFieldDoc5());
    ki.commit();
    // treat merging gracefully
    SpanQueryWrapper sqw = getJSONQuery(getClass().getResource("/queries/bugs/cosmas_wildcards_missingfoundry.jsonld").getFile());
    SpanQuery sq = sqw.toQuery();
    assertEquals(sq.toString(), "SpanMultiTermQueryWrapper(tokens:l:Erfahr*)");
    kr = ki.search(sq, (short) 10);
    assertEquals(4, kr.getMatches().size());
    assertEquals(1, kr.getMatch(0).getStartPos());
    assertEquals(2, kr.getMatch(0).getEndPos());
}
Also used : SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Test(org.junit.Test)

Example 95 with SpanQueryWrapper

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

the class TestReferenceIndex method testCase4.

// multiple document
@Test
public void testCase4() throws Exception {
    ki = new KrillIndex();
    ki.addDoc(createFieldDoc0());
    ki.addDoc(createFieldDoc1());
    ki.commit();
    String filepath = getClass().getResource("/queries/reference/distance-reference.jsonld").getFile();
    SpanQueryWrapper sqwi = getJSONQuery(filepath);
    SpanQuery sq = sqwi.toQuery();
    kr = ki.search(sq, (short) 10);
    assertEquals(4, kr.getTotalResults());
    assertEquals("doc-1", kr.getMatch(3).getDocID());
    assertEquals(2, kr.getMatch(3).getStartPos());
    assertEquals(4, kr.getMatch(3).getEndPos());
}
Also used : SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Test(org.junit.Test)

Aggregations

SpanQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanQueryWrapper)165 Test (org.junit.Test)150 SpanQuery (org.apache.lucene.search.spans.SpanQuery)65 KrillIndex (de.ids_mannheim.korap.KrillIndex)17 QueryException (de.ids_mannheim.korap.util.QueryException)14 KrillQuery (de.ids_mannheim.korap.KrillQuery)13 QueryBuilder (de.ids_mannheim.korap.query.QueryBuilder)11 Result (de.ids_mannheim.korap.response.Result)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)7 Krill (de.ids_mannheim.korap.Krill)5 SpanRepetitionQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanRepetitionQueryWrapper)3 TestSimple.getJsonString (de.ids_mannheim.korap.TestSimple.getJsonString)2 SpanClassQuery (de.ids_mannheim.korap.query.SpanClassQuery)2 SpanDistanceQuery (de.ids_mannheim.korap.query.SpanDistanceQuery)2 SpanWithinQuery (de.ids_mannheim.korap.query.SpanWithinQuery)2 SpanAlterQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanAlterQueryWrapper)2 SpanRegexQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanRegexQueryWrapper)2 SpanSegmentQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanSegmentQueryWrapper)2 Term (org.apache.lucene.index.Term)2 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)2