use of com.yahoo.search.query.QueryTree in project vespa by vespa-engine.
the class VespaSearcher method marshalQuery.
public String marshalQuery(QueryTree root) {
// TODO: Why?
QueryTree rootClone = root.clone();
String error = QueryCanonicalizer.canonicalize(rootClone);
if (error != null)
return null;
return marshalRoot(rootClone.getRoot());
}
use of com.yahoo.search.query.QueryTree in project vespa by vespa-engine.
the class MinimalQueryInserter method search.
@Override
public Result search(Query query, Execution execution) {
if (query.properties().get(YQL) == null) {
return execution.search(query);
}
ParserEnvironment env = ParserEnvironment.fromExecutionContext(execution.context());
YqlParser parser = (YqlParser) ParserFactory.newInstance(Query.Type.YQL, env);
parser.setQueryParser(false);
parser.setUserQuery(query);
QueryTree newTree;
try {
newTree = parser.parse(Parsable.fromQueryModel(query.getModel()).setQuery(query.properties().getString(YQL)));
} catch (RuntimeException e) {
return new Result(query, ErrorMessage.createInvalidQueryParameter("Could not instantiate query from YQL", e));
}
if (parser.getOffset() != null) {
int maxHits = query.properties().getInteger(MAX_HITS);
int maxOffset = query.properties().getInteger(MAX_OFFSET);
if (parser.getOffset() > maxOffset) {
return new Result(query, ErrorMessage.createInvalidQueryParameter("Requested offset " + parser.getOffset() + ", but the max offset allowed is " + maxOffset + "."));
}
if (parser.getHits() > maxHits) {
return new Result(query, ErrorMessage.createInvalidQueryParameter("Requested " + parser.getHits() + " hits returned, but max hits allowed is " + maxHits + "."));
}
}
query.getModel().getQueryTree().setRoot(newTree.getRoot());
query.getPresentation().getSummaryFields().addAll(parser.getYqlSummaryFields());
for (VespaGroupingStep step : parser.getGroupingSteps()) {
GroupingRequest.newInstance(query).setRootOperation(step.getOperation()).continuations().addAll(step.continuations());
}
if (parser.getYqlSources().size() == 0) {
query.getModel().getSources().clear();
} else {
query.getModel().getSources().addAll(parser.getYqlSources());
}
if (parser.getOffset() != null) {
query.setOffset(parser.getOffset());
query.setHits(parser.getHits());
}
if (parser.getTimeout() != null) {
query.setTimeout(parser.getTimeout().longValue());
}
if (parser.getSorting() != null) {
query.getRanking().setSorting(parser.getSorting());
}
query.trace("YQL+ query parsed", true, 2);
return execution.search(query);
}
use of com.yahoo.search.query.QueryTree in project vespa by vespa-engine.
the class ValidatePredicateSearcherTestCase method doSearch.
private static Result doSearch(ValidatePredicateSearcher searcher, String yqlQuery, String command) {
QueryTree queryTree = new YqlParser(new ParserEnvironment()).parse(new Parsable().setQuery(yqlQuery));
Query query = new Query();
query.getModel().getQueryTree().setRoot(queryTree.getRoot());
TreeMap<String, List<String>> masterClusters = new TreeMap<>();
masterClusters.put("cluster", Arrays.asList("document"));
SearchDefinition searchDefinition = new SearchDefinition("document");
Index index = new Index("predicate_field");
index.addCommand(command);
searchDefinition.addIndex(index);
Map<String, SearchDefinition> searchDefinitionMap = new HashMap<>();
searchDefinitionMap.put("document", searchDefinition);
IndexFacts indexFacts = new IndexFacts(new IndexModel(masterClusters, searchDefinitionMap, searchDefinition));
Execution.Context context = new Execution.Context(null, indexFacts, null, new RendererRegistry(MoreExecutors.directExecutor()), new SimpleLinguistics());
return new Execution(searcher, context).search(query);
}
use of com.yahoo.search.query.QueryTree in project vespa by vespa-engine.
the class VespaSerializerTestCase method parseAndConfirm.
private void parseAndConfirm(String expected, String toParse) {
QueryTree item = parser.parse(new Parsable().setQuery(SELECT + toParse + ";"));
// System.out.println(item.toString());
String q = VespaSerializer.serialize(item.getRoot());
assertEquals(expected, q);
}
use of com.yahoo.search.query.QueryTree in project vespa by vespa-engine.
the class YqlParserTestCase method testWeakAnd.
@Test
public void testWeakAnd() {
assertParse("select foo from bar where weakAnd(a contains \"A\", b contains \"B\");", "WAND(100) a:A b:B");
assertParse("select foo from bar where [{\"targetNumHits\": 37}]weakAnd(a contains \"A\", " + "b contains \"B\");", "WAND(37) a:A b:B");
QueryTree tree = parse("select foo from bar where [{\"scoreThreshold\": 41}]weakAnd(a " + "contains \"A\", b contains \"B\");");
assertEquals("WAND(100) a:A b:B", tree.toString());
assertEquals(WeakAndItem.class, tree.getRoot().getClass());
assertEquals(41, ((WeakAndItem) tree.getRoot()).getScoreThreshold());
}
Aggregations