Search in sources :

Example 1 with QueryResultRowArray

use of de.catma.queryengine.result.QueryResultRowArray in project catma by forTEXT.

the class AnnotatedDocumentQueryResultRowItem method addChildRowItems.

@Override
public void addChildRowItems(TreeData<QueryResultRowItem> treeData, LoadingCache<String, KwicProvider> kwicProviderCache) {
    try {
        HashMap<String, QueryResultRowArray> rowsByCollectionId = new HashMap<String, QueryResultRowArray>();
        for (QueryResultRow row : groupedQueryResult) {
            if (row instanceof TagQueryResultRow) {
                TagQueryResultRow tRow = (TagQueryResultRow) row;
                String collectionId = tRow.getMarkupCollectionId();
                QueryResultRowArray rows = null;
                if (!rowsByCollectionId.containsKey(collectionId)) {
                    rows = new QueryResultRowArray();
                    rowsByCollectionId.put(collectionId, rows);
                } else {
                    rows = rowsByCollectionId.get(collectionId);
                }
                rows.add(row);
            }
        }
        for (String collectionId : rowsByCollectionId.keySet()) {
            SourceDocument document = kwicProviderCache.get(getDocumentId()).getSourceDocument();
            String collectionName = document.getUserMarkupCollectionReference(collectionId).getName();
            QueryResultRowArray rows = rowsByCollectionId.get(collectionId);
            CollectionQueryResultRowItem item = new CollectionQueryResultRowItem(identity, collectionName, getDocumentId(), collectionId, rows, project);
            if (!treeData.contains(item)) {
                treeData.addItem(this, item);
                treeData.addItem(item, new DummyQueryResultRowItem());
            }
        }
    } catch (Exception e) {
        ((ErrorHandler) UI.getCurrent()).showAndLogError("error displaying annotated query results", e);
    }
}
Also used : HashMap(java.util.HashMap) QueryResultRow(de.catma.queryengine.result.QueryResultRow) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) SourceDocument(de.catma.document.source.SourceDocument) QueryResultRowArray(de.catma.queryengine.result.QueryResultRowArray)

Example 2 with QueryResultRowArray

use of de.catma.queryengine.result.QueryResultRowArray in project catma by forTEXT.

the class DocumentQueryResultRowItem method getRows.

@Override
public QueryResultRowArray getRows() {
    if (rows == null) {
        rows = new QueryResultRowArray();
        groupedQueryResult.forEach(row -> rows.add(row));
    }
    return rows;
}
Also used : QueryResultRowArray(de.catma.queryengine.result.QueryResultRowArray)

Example 3 with QueryResultRowArray

use of de.catma.queryengine.result.QueryResultRowArray in project catma by forTEXT.

the class OrRefinement method refine.

public QueryResult refine(QueryResult result) throws Exception {
    QueryResultRowArray refinedResult1 = refinement1.refine(result).asQueryResultRowArray();
    QueryResultRowArray refinedResult2 = refinement2.refine(result).asQueryResultRowArray();
    Set<QueryResultRow> withoutDuplicates = new HashSet<QueryResultRow>();
    withoutDuplicates.addAll(refinedResult1);
    withoutDuplicates.addAll(refinedResult2);
    QueryResultRowArray combinedResult = new QueryResultRowArray();
    combinedResult.addAll(withoutDuplicates);
    return combinedResult;
}
Also used : QueryResultRow(de.catma.queryengine.result.QueryResultRow) QueryResultRowArray(de.catma.queryengine.result.QueryResultRowArray) HashSet(java.util.HashSet)

Example 4 with QueryResultRowArray

use of de.catma.queryengine.result.QueryResultRowArray in project catma by forTEXT.

the class TPGraphProjectIndexer method searchProperty.

@Override
public QueryResult searchProperty(QueryId queryId, List<String> collectionIdList, String propertyNamePattern, String propertyValuePattern, String tagPathPattern) throws Exception {
    QueryResultRowArray result = new QueryResultRowArray();
    PropertyNameFilter propertyNameFilter = new PropertyNameFilter(propertyNamePattern);
    PropertyValueFilter propertyValueFilter = new PropertyValueFilter(propertyValuePattern);
    // add default wildcard if no explicit root is defined
    if (tagPathPattern != null) {
        if (!tagPathPattern.startsWith("/")) {
            tagPathPattern = "%" + tagPathPattern;
        }
    }
    final String tagPathRegex = tagPathPattern == null ? null : SQLWildcard2RegexConverter.convert(tagPathPattern);
    GraphTraversalSource g = graph.traversal();
    // get all Tags referenced by the participating Collections
    GraphTraversal<Vertex, Vertex> traversal = g.V().hasLabel(nt(ProjectRevision)).outE(rt(hasDocument)).inV().hasLabel(nt(SourceDocument)).outE(rt(hasCollection)).inV().has(nt(MarkupCollection), "collectionId", P.within(collectionIdList)).outE(rt(hasInstance)).inV().hasLabel(nt(TagInstance)).inE(rt(hasInstance)).outV().hasLabel(nt(Tag));
    Set<Vertex> tagVs = traversal.toSet();
    if (!tagVs.isEmpty()) {
        // get all paths for the Tags
        List<Path> tagPaths = g.V(tagVs).optional(__.repeat(__.out(rt(hasParent))).until(__.outE(rt(hasParent)).count().is(0))).path().toList();
        // collect all Tags matching the given pattern and map them by their tagId
        Map<String, String> validTagIdToTagPathMapping = new HashMap<>();
        for (Path path : tagPaths) {
            Vertex tag = path.get(0);
            String tagId = (String) tag.properties("tagId").next().orElse(null);
            StringBuilder builder = new StringBuilder();
            String conc = "/";
            path.forEach(tagVertex -> {
                builder.insert(0, ((Vertex) tagVertex).properties("name").next().orElse(null));
                builder.insert(0, conc);
            });
            String tagPathStr = builder.toString();
            if ((tagPathRegex == null) || Pattern.matches(tagPathRegex, tagPathStr)) {
                validTagIdToTagPathMapping.put(tagId, tagPathStr);
            }
        }
        // get all Annotations for the participating Collections and Tags with their matching Annotaiton Properties
        List<Map<String, Object>> resultMap = g.V().hasLabel(nt(ProjectRevision)).outE(rt(hasDocument)).inV().hasLabel(nt(SourceDocument)).as("doc-uuid").outE(rt(hasCollection)).inV().has(nt(MarkupCollection), "collectionId", P.within(collectionIdList)).as("collection-uuid").outE(rt(hasInstance)).inV().hasLabel(nt(TagInstance)).as("anno").optional(__.outE(rt(hasProperty)).inV().hasLabel(nt(AnnotationProperty)).filter(propertyValueFilter)).as("anno-property").select("anno").inE(rt(hasInstance)).outV().has(nt(Tag), "tagId", P.within(validTagIdToTagPathMapping.keySet())).as("tag").optional(__.outE(rt(hasProperty)).inV().hasLabel(nt(Property)).filter(propertyNameFilter)).as("property").select("doc-uuid", "collection-uuid", "anno", "tag", "anno-property", "property").by("documentId").by("collectionId").by().by().by().by().toList();
        HashSet<String> systemPropertiesAddedTagInstanceIds = new HashSet<>();
        for (Map<String, Object> entry : resultMap) {
            String documentId = (String) entry.get("doc-uuid");
            String collectionId = (String) entry.get("collection-uuid");
            Vertex annoV = (Vertex) entry.get("anno");
            String tagInstanceId = (String) annoV.property("tagInstanceId").value();
            @SuppressWarnings("unchecked") List<Integer> ranges = (List<Integer>) annoV.property("ranges").value();
            List<Range> rangeList = new ArrayList<>();
            for (int i = 0; i < ranges.size() - 1; i += 2) {
                rangeList.add(new Range(ranges.get(i), ranges.get(i + 1)));
            }
            String annoAuthor = (String) annoV.property("author").value();
            String annoTimestamp = (String) annoV.property("timestamp").value();
            Vertex tagV = (Vertex) entry.get("tag");
            String tagId = (String) tagV.property("tagId").value();
            String tagPath = validTagIdToTagPathMapping.get(tagId);
            TagDefinition tag = (TagDefinition) tagV.property("tag").value();
            String color = tag.getColor();
            Vertex propertyV = (Vertex) entry.get("property");
            if (propertyV.equals(tagV)) {
                // no matching Properties for this Tag
                propertyV = null;
            }
            Vertex annoPropertyV = (Vertex) entry.get("anno-property");
            if (annoPropertyV.equals(annoV)) {
                // no matching Annotation Property for this Annotation
                annoPropertyV = null;
            }
            // we try to add them now with respect to user defined name and value filters
            if (!systemPropertiesAddedTagInstanceIds.contains(tagInstanceId)) {
                // try to add rows for matching system properties
                addTagQueryResultRowForSystemProperty(queryId, result, PropertyDefinition.SystemPropertyName.catma_markupauthor, annoAuthor, propertyNameFilter, propertyValueFilter, documentId, collectionId, tagId, tagPath, tagInstanceId, rangeList);
                addTagQueryResultRowForSystemProperty(queryId, result, PropertyDefinition.SystemPropertyName.catma_markuptimestamp, annoTimestamp, propertyNameFilter, propertyValueFilter, documentId, collectionId, tagId, tagPath, tagInstanceId, rangeList);
                addTagQueryResultRowForSystemProperty(queryId, result, PropertyDefinition.SystemPropertyName.catma_displaycolor, color, propertyNameFilter, propertyValueFilter, documentId, collectionId, tagId, tagPath, tagInstanceId, rangeList);
                systemPropertiesAddedTagInstanceIds.add(tagInstanceId);
            }
            // add rows for user defined properties for each matching value
            if ((propertyV != null) && (annoPropertyV != null)) {
                @SuppressWarnings("unchecked") List<String> propertyValues = (List<String>) annoPropertyV.property("values").value();
                String annoPropertyDefinitionId = (String) annoPropertyV.property("uuid").value();
                String propertyName = (String) propertyV.property("name").value();
                String propertyDefinitionId = (String) propertyV.property("uuid").value();
                if (annoPropertyDefinitionId.equals(propertyDefinitionId)) {
                    for (String propValue : propertyValues) {
                        if (propertyValueFilter.testValue(propValue)) {
                            result.add(new TagQueryResultRow(queryId, documentId, rangeList, collectionId, tagId, tagPath, // TODO: Version
                            "", tagInstanceId, annoPropertyDefinitionId, propertyName, propValue));
                        }
                    }
                }
            }
        }
    }
    return result;
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) TagDefinition(de.catma.tag.TagDefinition) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Path(org.apache.tinkerpop.gremlin.process.traversal.Path) Range(de.catma.document.Range) QueryResultRowArray(de.catma.queryengine.result.QueryResultRowArray) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with QueryResultRowArray

use of de.catma.queryengine.result.QueryResultRowArray in project catma by forTEXT.

the class TPGraphProjectIndexer method searchPhrase.

private QueryResult searchPhrase(QueryId queryId, List<String> documentIdList, String phrase, List<String> termList, int limit, BiPredicate<String, String> termTestFunction) {
    GraphTraversalSource g = graph.traversal();
    GraphTraversal<Vertex, Vertex> currentTraversal = g.V().hasLabel(nt(ProjectRevision)).outE(rt(hasDocument)).inV().has(nt(SourceDocument), "documentId", P.within(documentIdList)).as("doc").inE(rt(isPartOf)).outV().has(nt(Term), "literal", P.test(termTestFunction, termList.get(0))).outE(rt(hasPosition)).inV().hasLabel(nt(Position)).as("startPos", "currentPos");
    if (termList.size() > 1) {
        for (String term : termList.subList(1, termList.size())) {
            currentTraversal = currentTraversal.outE(rt(isAdjacentTo)).inV().hasLabel(nt(Position)).as("currentPos").inE(rt(hasPosition)).outV().has(nt(Term), "literal", P.test(termTestFunction, term)).select("currentPos");
        }
    }
    if (limit > 0) {
        currentTraversal = currentTraversal.limit(limit);
    }
    return new QueryResultRowArray(currentTraversal.select("doc", "startPos", "currentPos").by("documentId").by("startOffset").by("endOffset").map(resultMap -> new QueryResultRow(queryId, (String) resultMap.get().get("doc"), new Range((Integer) resultMap.get().get("startPos"), (Integer) resultMap.get().get("currentPos")), phrase)).toList());
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) CommentQueryResultRow(de.catma.queryengine.result.CommentQueryResultRow) QueryResultRow(de.catma.queryengine.result.QueryResultRow) QueryResultRowArray(de.catma.queryengine.result.QueryResultRowArray) Range(de.catma.document.Range)

Aggregations

QueryResultRowArray (de.catma.queryengine.result.QueryResultRowArray)24 QueryResultRow (de.catma.queryengine.result.QueryResultRow)14 TagQueryResultRow (de.catma.queryengine.result.TagQueryResultRow)11 Range (de.catma.document.Range)7 QueryResult (de.catma.queryengine.result.QueryResult)6 SourceDocument (de.catma.document.source.SourceDocument)5 Project (de.catma.project.Project)4 HashMap (java.util.HashMap)4 Indexer (de.catma.indexer.Indexer)3 CommentQueryResultRow (de.catma.queryengine.result.CommentQueryResultRow)3 GroupedQueryResult (de.catma.queryengine.result.GroupedQueryResult)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)3 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)3 AnnotationCollectionReference (de.catma.document.annotation.AnnotationCollectionReference)2 TagDefinition (de.catma.tag.TagDefinition)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Path (org.apache.tinkerpop.gremlin.process.traversal.Path)2