use of annis.ql.parser.QueryData in project ANNIS by korpling.
the class QueryServiceImpl method parseNodes.
/**
* Return the list of the query nodes if this is a valid query
* or throw exception when invalid
*
* @param query Query to get the query nodes for
* @param rawCorpusNames
* @return
*/
@GET
@Path("parse/nodes")
@Produces("application/xml")
public Response parseNodes(@QueryParam("q") String query, @DefaultValue("") @QueryParam("corpora") String rawCorpusNames) {
Subject user = SecurityUtils.getSubject();
List<String> corpusNames = splitCorpusNamesFromRaw(rawCorpusNames);
for (String c : corpusNames) {
user.checkPermission("query:parse:" + c);
}
Collections.sort(corpusNames);
List<Long> corpusIDs = queryDao.mapCorpusNamesToIds(corpusNames);
QueryData data = queryDao.parseAQL(query, corpusIDs);
List<QueryNode> nodes = new LinkedList<>();
int i = 0;
for (List<QueryNode> alternative : data.getAlternatives()) {
for (QueryNode n : alternative) {
n.setAlternativeNumber(i);
nodes.add(n);
}
i++;
}
return Response.ok(new GenericEntity<List<QueryNode>>(nodes) {
}).build();
}
use of annis.ql.parser.QueryData in project ANNIS by korpling.
the class QueryServiceImpl method count.
@GET
@Path("search/count")
@Produces("application/xml")
@Override
public Response count(@QueryParam("q") String query, @QueryParam("corpora") String rawCorpusNames) {
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:count:" + c);
}
QueryData data = queryDataFromParameters(query, rawCorpusNames);
long start = new Date().getTime();
MatchAndDocumentCount count = queryDao.countMatchesAndDocuments(data);
long end = new Date().getTime();
logQuery("COUNT", query, splitCorpusNamesFromRaw(rawCorpusNames), end - start);
return Response.ok(count).type(MediaType.APPLICATION_XML_TYPE).build();
}
use of annis.ql.parser.QueryData in project ANNIS by korpling.
the class QueryServiceImpl method matrix.
/**
* Get result as matrix in WEKA (ARFF) format.
* @param query
* @param rawCorpusNames
* @param rawMetaKeys
* @param rawCsv
* @return
*/
@GET
@Path("search/matrix")
@Produces("text/plain")
public StreamingOutput matrix(@QueryParam("q") final String query, @QueryParam("corpora") final String rawCorpusNames, @QueryParam("metakeys") String rawMetaKeys, @DefaultValue("false") @QueryParam("csv") String rawCsv) {
requiredParameter(query, "q", "AnnisQL query");
requiredParameter(rawCorpusNames, "corpora", "comma separated list of corpus names");
final boolean outputCsv = Boolean.parseBoolean(rawCsv);
Subject user = SecurityUtils.getSubject();
List<String> corpusNames = splitCorpusNamesFromRaw(rawCorpusNames);
for (String c : corpusNames) {
user.checkPermission("query:matrix:" + c);
}
final QueryData data = queryDataFromParameters(query, rawCorpusNames);
MatrixQueryData ext = new MatrixQueryData();
if (rawMetaKeys != null) {
ext.setMetaKeys(splitMatrixKeysFromRaw(rawMetaKeys));
}
if (ext.getMetaKeys() != null && ext.getMetaKeys().isEmpty()) {
ext.setMetaKeys(null);
}
data.addExtension(ext);
StreamingOutput result = new StreamingOutput() {
@Override
public void write(OutputStream output) throws IOException, WebApplicationException {
long start = new Date().getTime();
queryDao.matrix(data, outputCsv, output);
long end = new Date().getTime();
logQuery("MATRIX", query, splitCorpusNamesFromRaw(rawCorpusNames), end - start);
}
};
return result;
}
use of annis.ql.parser.QueryData in project ANNIS by korpling.
the class QueryServiceImpl method basicSubgraph.
protected SaltProject basicSubgraph(MatchGroup matches, @QueryParam("segmentation") String segmentation, @DefaultValue("0") @QueryParam("left") String leftRaw, @DefaultValue("0") @QueryParam("right") String rightRaw, @DefaultValue("all") @QueryParam("filter") String filterRaw) {
Subject user = SecurityUtils.getSubject();
int left = Integer.parseInt(leftRaw);
int right = Integer.parseInt(rightRaw);
SubgraphFilter filter = SubgraphFilter.valueOf(filterRaw);
QueryData data = GraphHelper.createQueryData(matches, queryDao);
data.addExtension(new AnnotateQueryData(left, right, segmentation, filter));
Set<String> corpusNames = new TreeSet<>();
for (Match singleMatch : matches.getMatches()) {
// collect list of used corpora
for (java.net.URI u : singleMatch.getSaltIDs()) {
corpusNames.add(CommonHelper.getCorpusPath(u).get(0));
}
}
for (String c : corpusNames) {
user.checkPermission("query:subgraph:" + c);
}
List<String> corpusNamesList = new LinkedList<>(corpusNames);
if (data.getCorpusList() == null || data.getCorpusList().isEmpty()) {
throw new WebApplicationException(Response.Status.BAD_REQUEST.getStatusCode());
}
long start = new Date().getTime();
SaltProject p = queryDao.graph(data);
long end = new Date().getTime();
String options = "matches: " + matches.toString() + ", seg: " + segmentation + ", left: " + left + ", right: " + right + ", filter: " + filter;
logQuery("SUBGRAPH", "", corpusNamesList, end - start, options);
return p;
}
use of annis.ql.parser.QueryData in project ANNIS by korpling.
the class QueriesGenerator method generateQuery.
private void generateQuery(QueryBuilder queryBuilder) {
try {
// retrieve the aql query for analyzing purposes
String aql = queryBuilder.getAQL();
// set some necessary extensions for generating complete sql
QueryData queryData = getQueryDao().parseAQL(aql, this.corpusIds);
queryData.addExtension(queryBuilder.getLimitOffsetQueryData());
// retrieve the salt project to analyze
List<Match> matches = getQueryDao().find(queryData);
if (matches.isEmpty()) {
return;
}
QueryData matchQueryData = GraphHelper.createQueryData(new MatchGroup(matches), queryDao);
matchQueryData.addExtension(queryBuilder.getAnnotateQueryData());
SaltProject saltProject = getQueryDao().graph(matchQueryData);
queryBuilder.analyzingQuery(saltProject);
// set the corpus name
ExampleQuery exampleQuery = queryBuilder.getExampleQuery();
exampleQuery.setCorpusName(corpusName);
// copy the example query to the database
if (exampleQuery.getExampleQuery() != null && !"".equals(exampleQuery.getExampleQuery())) {
if (getTableInsertSelect().containsKey("example_queries")) {
Object[] values = new Object[] { exampleQuery.getExampleQuery(), exampleQuery.getDescription(), exampleQuery.getType() == null ? "" : exampleQuery.getType(), exampleQuery.getNodes(), "{}", corpusIds.get(0) };
int[] argTypes = new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.VARCHAR, Types.INTEGER };
getJdbcTemplate().update("INSERT INTO example_queries(" + getTableInsertSelect().get("example_queries") + ") VALUES(?, ?, ?, ?, ?::text[], ?)", values, argTypes);
log.info("generated example query: {}", exampleQuery.getExampleQuery());
}
} else {
log.warn("could not generating auto query with {}", queryBuilder.getClass().getName());
}
} catch (Exception ex) {
log.warn("Cannot generate example query", ex);
}
}
Aggregations