Search in sources :

Example 1 with LimitOffsetQueryData

use of annis.sqlgen.extensions.LimitOffsetQueryData in project ANNIS by korpling.

the class AnnisRunner method analyzeQuery.

/**
 * Does the setup for the QueryData object.
 *
 * If the query function is "subgraph" or "sql_subgraph" the annisQuery string
 * should contain space separated salt ids. In this case the annisQuery is not
 * parsed and the {@link QueryData#getAlternatives()} method should return a
 * List with dummy QueryNode entries. Instead of parsing the annisQuery it
 * extracts the salt ids and put it into the extension's of {@link QueryData}.
 *
 * @param annisQuery should include a valid annis query
 * @param queryFunction should include a method name of {@link AnnisRunner}
 * which starts with do.
 * @return {@link QueryData} object, which contains a parsed annis query, the
 * default context {@link AnnisRunner#left} and {@link AnnisRunner#left}
 * values and the default {@link AnnisRunner#limit} and
 * {@link AnnisRunner#offset} values
 */
private QueryData analyzeQuery(String annisQuery, String queryFunction) {
    QueryData queryData;
    log.debug("analyze query for " + queryFunction + " function");
    if (queryFunction != null && !queryFunction.matches("(sql_)?subgraph")) {
        queryData = queryDao.parseAQL(annisQuery, corpusList);
    } else {
        queryData = GraphHelper.createQueryData(MatchGroup.parseString(annisQuery), queryDao);
    }
    queryData.setCorpusConfiguration(queryDao.getCorpusConfiguration());
    // filter by meta data
    queryData.setDocuments(metaDataFilter.getDocumentsForMetadata(queryData));
    if (queryFunction != null && queryFunction.matches("(sql_)?(annotate|find)")) {
        queryData.addExtension(new AnnotateQueryData(left, right, segmentationLayer, filter));
        queryData.addExtension(new LimitOffsetQueryData(offset, limit, order));
    } else if (queryFunction != null && queryFunction.matches("(sql_)?subgraph")) {
        queryData.addExtension(new AnnotateQueryData(left, right, segmentationLayer, filter));
    } else if (queryFunction != null && queryFunction.matches("(sql_)?frequency")) {
        if (frequencyDef == null) {
            out.println("You have to set the 'freq-def' property first");
        } else {
            queryData.addExtension(frequencyDef);
        }
    }
    if (annisQuery != null) {
        if (benchmarkName == null) {
            benchmarkName = "auto_" + benchmarks.size();
        }
        Benchmark b = new AnnisRunner.Benchmark(queryFunction + " " + annisQuery, queryData);
        b.name = benchmarkName;
        benchmarks.add(b);
        benchmarkName = null;
    }
    return queryData;
}
Also used : QueryData(annis.ql.parser.QueryData) LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData) AnnotateQueryData(annis.sqlgen.extensions.AnnotateQueryData) AnnotateQueryData(annis.sqlgen.extensions.AnnotateQueryData) LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData)

Example 2 with LimitOffsetQueryData

use of annis.sqlgen.extensions.LimitOffsetQueryData in project ANNIS by korpling.

the class AnnotateSqlGenerator method selectClause.

@Override
public String selectClause(QueryData queryData, List<QueryNode> alternative, String indent) {
    String innerIndent = indent + TABSTOP;
    StringBuilder sb = new StringBuilder();
    sb.append("DISTINCT\n");
    sb.append(innerIndent).append("solutions.\"key\",\n");
    sb.append(innerIndent);
    int matchStart = 0;
    List<LimitOffsetQueryData> extension = queryData.getExtensions(LimitOffsetQueryData.class);
    if (extension.size() > 0) {
        matchStart = extension.get(0).getOffset();
    }
    sb.append(matchStart).append(" AS \"matchstart\",\n");
    sb.append(innerIndent).append("solutions.n,\n");
    List<String> fields = getSelectFields();
    sb.append(innerIndent).append(StringUtils.join(fields, ",\n" + innerIndent));
    sb.append(innerIndent).append(",\n");
    // corpus.path_name
    sb.append(innerIndent).append("corpus.path_name AS path");
    if (isIncludeDocumentNameInAnnotateQuery()) {
        sb.append(",\n");
        sb.append(innerIndent).append("corpus.path_name[1] AS document_name");
    }
    return sb.toString();
}
Also used : SqlConstraints.sqlString(annis.sqlgen.SqlConstraints.sqlString) LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData)

Example 3 with LimitOffsetQueryData

use of annis.sqlgen.extensions.LimitOffsetQueryData in project ANNIS by korpling.

the class SolutionSqlGenerator method orderByClause.

@Override
public String orderByClause(QueryData queryData, List<QueryNode> alternative, String indent) {
    OrderType order = OrderType.ascending;
    List<LimitOffsetQueryData> ext = queryData.getExtensions(LimitOffsetQueryData.class);
    if (!ext.isEmpty()) {
        order = ext.get(0).getOrder();
    }
    if (order == OrderType.random) {
        return "random()";
    } else {
        String appendix = "";
        if (order == OrderType.descending) {
            appendix = " DESC";
        } else if (order == OrderType.ascending) {
            appendix = " ASC";
        }
        List<Long> corpusList = queryData.getCorpusList();
        List<String> ids = new ArrayList<>();
        if (corpusList.size() > 1) {
            // add the artificial "source index" which corresponds to the toplevel corpus name
            ids.add("sourceIdx");
        }
        // add the node ID for each output node an the category ID
        for (int i = 1; i <= queryData.getMaxWidth(); ++i) {
            ids.add("id" + i + appendix);
            if (annoCondition != null) {
                ids.add("cat" + i + appendix);
            }
        }
        return StringUtils.join(ids, ", ");
    }
}
Also used : OrderType(annis.service.objects.OrderType) ArrayList(java.util.ArrayList) LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData)

Example 4 with LimitOffsetQueryData

use of annis.sqlgen.extensions.LimitOffsetQueryData in project ANNIS by korpling.

the class QueryServiceImpl method find.

@GET
@Path("search/find")
@Produces({ "application/xml", "text/plain" })
@Override
public Response find(@QueryParam("q") String query, @QueryParam("corpora") String rawCorpusNames, @DefaultValue("0") @QueryParam("offset") String offsetRaw, @DefaultValue("-1") @QueryParam("limit") String limitRaw, @DefaultValue("ascending") @QueryParam("order") String orderRaw) throws IOException {
    requiredParameter(query, "q", "AnnisQL query");
    requiredParameter(rawCorpusNames, "corpora", "comma separated list of corpus names");
    Subject user = SecurityUtils.getSubject();
    List<String> corpusNames = splitCorpusNamesFromRaw(rawCorpusNames);
    for (String c : corpusNames) {
        user.checkPermission("query:find:" + c);
    }
    int offset = Integer.parseInt(offsetRaw);
    int limit = Integer.parseInt(limitRaw);
    OrderType order;
    try {
        order = OrderType.valueOf(orderRaw.toLowerCase());
    } catch (IllegalArgumentException ex) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).type(MediaType.TEXT_PLAIN).entity("parameter 'order' has the invalid value '" + orderRaw + "'. It should be one of" + " 'ascending', 'random' or 'descending").build());
    }
    final QueryData data = queryDataFromParameters(query, rawCorpusNames);
    data.setCorpusConfiguration(queryDao.getCorpusConfiguration());
    data.addExtension(new LimitOffsetQueryData(offset, limit, order));
    String acceptHeader = request.getHeader(HttpHeaders.ACCEPT);
    if (acceptHeader == null || acceptHeader.trim().isEmpty()) {
        acceptHeader = "*/*";
    }
    List<String> knownTypes = Lists.newArrayList("text/plain", "application/xml");
    // find the best matching mime type
    String bestMediaTypeMatch = MIMEParse.bestMatch(knownTypes, acceptHeader);
    if ("text/plain".equals(bestMediaTypeMatch)) {
        return Response.ok(findRaw(data, rawCorpusNames, query), "text/plain").build();
    } else {
        List<Match> result = findXml(data, rawCorpusNames, query);
        return Response.ok().type("application/xml").entity(new GenericEntity<MatchGroup>(new MatchGroup(result)) {
        }).build();
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) QueryData(annis.ql.parser.QueryData) LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData) AnnotateQueryData(annis.sqlgen.extensions.AnnotateQueryData) MatrixQueryData(annis.sqlgen.MatrixQueryData) Subject(org.apache.shiro.subject.Subject) Match(annis.service.objects.Match) OrderType(annis.service.objects.OrderType) GenericEntity(javax.ws.rs.core.GenericEntity) MatchGroup(annis.service.objects.MatchGroup) LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 5 with LimitOffsetQueryData

use of annis.sqlgen.extensions.LimitOffsetQueryData in project ANNIS by korpling.

the class CommonLimitOffsetGenerator method limitOffsetClause.

@Override
public String limitOffsetClause(QueryData queryData, List<QueryNode> alternative, String indent) {
    LimitOffsetQueryData LimitOffsetQueryData = getLimitOffsetQueryData(queryData);
    StringBuilder sb = new StringBuilder();
    Integer limit = null;
    Integer offset = null;
    if (LimitOffsetQueryData != null) {
        limit = LimitOffsetQueryData.getLimit();
        offset = LimitOffsetQueryData.getOffset();
    }
    if (limit != null && limit >= 0) {
        sb.append(indent).append("LIMIT ").append(limit).append("\n");
    }
    if (offset != null && offset >= 0) {
        sb.append(indent).append("OFFSET ").append(offset);
    }
    return sb.toString();
}
Also used : LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData)

Aggregations

LimitOffsetQueryData (annis.sqlgen.extensions.LimitOffsetQueryData)5 QueryData (annis.ql.parser.QueryData)2 OrderType (annis.service.objects.OrderType)2 AnnotateQueryData (annis.sqlgen.extensions.AnnotateQueryData)2 Match (annis.service.objects.Match)1 MatchGroup (annis.service.objects.MatchGroup)1 MatrixQueryData (annis.sqlgen.MatrixQueryData)1 SqlConstraints.sqlString (annis.sqlgen.SqlConstraints.sqlString)1 ArrayList (java.util.ArrayList)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 GenericEntity (javax.ws.rs.core.GenericEntity)1 Subject (org.apache.shiro.subject.Subject)1