Search in sources :

Example 1 with QueryData

use of annis.ql.parser.QueryData in project ANNIS by korpling.

the class QueryServiceImpl method frequency.

/**
 * Frequency analysis.
 *
 * @param  rawFields Comma seperated list of result vector elements.
 *                   Each element has the form <node-nr>:<anno-name>. The
 *                   annotation name can be set to "tok" to indicate that the
 *                   span should be used instead of an annotation.
 */
@GET
@Path("search/frequency")
@Produces("application/xml")
public FrequencyTable frequency(@QueryParam("q") String query, @QueryParam("corpora") String rawCorpusNames, @QueryParam("fields") String rawFields) {
    requiredParameter(query, "q", "AnnisQL query");
    requiredParameter(rawCorpusNames, "corpora", "comma separated list of corpus names");
    requiredParameter(rawFields, "fields", "Comma seperated list of result vector elements.");
    Subject user = SecurityUtils.getSubject();
    List<String> corpusNames = splitCorpusNamesFromRaw(rawCorpusNames);
    for (String c : corpusNames) {
        user.checkPermission("query:matrix:" + c);
    }
    QueryData data = queryDataFromParameters(query, rawCorpusNames);
    FrequencyTableQuery ext = FrequencyTableQuery.parse(rawFields);
    data.addExtension(ext);
    long start = new Date().getTime();
    FrequencyTable freqTable = queryDao.frequency(data);
    long end = new Date().getTime();
    logQuery("FREQUENCY", query, splitCorpusNamesFromRaw(rawCorpusNames), end - start);
    return freqTable;
}
Also used : FrequencyTableQuery(annis.service.objects.FrequencyTableQuery) QueryData(annis.ql.parser.QueryData) LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData) AnnotateQueryData(annis.sqlgen.extensions.AnnotateQueryData) MatrixQueryData(annis.sqlgen.MatrixQueryData) FrequencyTable(annis.service.objects.FrequencyTable) Subject(org.apache.shiro.subject.Subject) Date(java.util.Date) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with QueryData

use of annis.ql.parser.QueryData in project ANNIS by korpling.

the class AnnisRunner method doExplain.

public void doExplain(String functionCall, boolean analyze) {
    SqlGenerator<QueryData> generator = getGeneratorForQueryFunction(functionCall);
    String function = getAnnisQueryFromFunctionCall(functionCall);
    String annisQuery = getAnnisQueryFromFunctionCall(functionCall);
    QueryData queryData = analyzeQuery(annisQuery, function);
    out.println("NOTICE: left = " + left + "; right = " + right + "; limit = " + limit + "; offset = " + offset);
    out.println(queryDao.explain(generator, queryData, analyze));
}
Also used : QueryData(annis.ql.parser.QueryData) LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData) AnnotateQueryData(annis.sqlgen.extensions.AnnotateQueryData)

Example 3 with QueryData

use of annis.ql.parser.QueryData 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 4 with QueryData

use of annis.ql.parser.QueryData in project ANNIS by korpling.

the class GraphHelper method createQueryData.

/**
 * This is a helper function to make it easier to create a correct query data object
 * from a {@link MatchGroup} for a {@link AnnisDao#graph(annis.ql.parser.QueryData)  } query.
 *
 * @param matchGroup
 * @return
 */
public static QueryData createQueryData(MatchGroup matchGroup, QueryDao annisDao) {
    QueryData queryData = new QueryData();
    Set<String> corpusNames = new TreeSet<>();
    for (Match m : matchGroup.getMatches()) {
        // collect list of used corpora and created pseudo QueryNodes for each URI
        List<QueryNode> pseudoNodes = new ArrayList<>(m.getSaltIDs().size());
        for (java.net.URI u : m.getSaltIDs()) {
            pseudoNodes.add(new QueryNode());
            corpusNames.add(CommonHelper.getCorpusPath(u).get(0));
        }
        queryData.addAlternative(pseudoNodes);
    }
    List<Long> corpusIDs = annisDao.mapCorpusNamesToIds(new LinkedList<>(corpusNames));
    queryData.setCorpusList(corpusIDs);
    queryData.addExtension(matchGroup);
    return queryData;
}
Also used : QueryData(annis.ql.parser.QueryData) ArrayList(java.util.ArrayList) Match(annis.service.objects.Match) TreeSet(java.util.TreeSet) QueryNode(annis.model.QueryNode)

Example 5 with QueryData

use of annis.ql.parser.QueryData in project ANNIS by korpling.

the class TestAbstractSqlGenerator method shouldAppendOrderByClause.

/**
 * Append ORDER BY clause.
 */
@Test
public void shouldAppendOrderByClause() {
    // given
    String selectClause = uniqueString();
    String fromClause = uniqueString();
    setupSelectAndFromClause(selectClause, fromClause);
    String orderBy = uniqueString();
    @SuppressWarnings("unchecked") OrderByClauseSqlGenerator<QueryData> orderByClauseSqlGenerator = mock(OrderByClauseSqlGenerator.class);
    generator.setOrderByClauseSqlGenerator(orderByClauseSqlGenerator);
    given(orderByClauseSqlGenerator.orderByClause(eq(queryData), eq(alternative), anyString())).willReturn(orderBy);
    // when
    String sql = generator.toSql(queryData);
    // then
    String expected = createMinimalSqlStatement(selectClause, fromClause);
    expected += "ORDER BY " + orderBy + "\n";
    assertThat(sql, is(expected));
}
Also used : QueryData(annis.ql.parser.QueryData) TestUtils.uniqueString(annis.test.TestUtils.uniqueString) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

QueryData (annis.ql.parser.QueryData)20 AnnotateQueryData (annis.sqlgen.extensions.AnnotateQueryData)13 LimitOffsetQueryData (annis.sqlgen.extensions.LimitOffsetQueryData)13 MatrixQueryData (annis.sqlgen.MatrixQueryData)6 Date (java.util.Date)6 Subject (org.apache.shiro.subject.Subject)6 TestUtils.uniqueString (annis.test.TestUtils.uniqueString)5 GET (javax.ws.rs.GET)5 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 Test (org.junit.Test)5 Matchers.anyString (org.mockito.Matchers.anyString)5 QueryNode (annis.model.QueryNode)4 Match (annis.service.objects.Match)4 LinkedList (java.util.LinkedList)3 SaltProject (org.corpus_tools.salt.common.SaltProject)3 ExampleQuery (annis.examplequeries.ExampleQuery)2 MatchGroup (annis.service.objects.MatchGroup)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2