Search in sources :

Example 11 with QueryTree

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());
}
Also used : QueryTree(com.yahoo.search.query.QueryTree)

Example 12 with QueryTree

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);
}
Also used : QueryTree(com.yahoo.search.query.QueryTree) ParserEnvironment(com.yahoo.search.query.parser.ParserEnvironment) Result(com.yahoo.search.Result)

Example 13 with QueryTree

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);
}
Also used : Query(com.yahoo.search.Query) IndexFacts(com.yahoo.prelude.IndexFacts) Parsable(com.yahoo.search.query.parser.Parsable) Index(com.yahoo.prelude.Index) IndexModel(com.yahoo.prelude.IndexModel) SearchDefinition(com.yahoo.prelude.SearchDefinition) SimpleLinguistics(com.yahoo.language.simple.SimpleLinguistics) YqlParser(com.yahoo.search.yql.YqlParser) Execution(com.yahoo.search.searchchain.Execution) QueryTree(com.yahoo.search.query.QueryTree) RendererRegistry(com.yahoo.search.rendering.RendererRegistry) ParserEnvironment(com.yahoo.search.query.parser.ParserEnvironment)

Example 14 with QueryTree

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);
}
Also used : QueryTree(com.yahoo.search.query.QueryTree) Parsable(com.yahoo.search.query.parser.Parsable)

Example 15 with QueryTree

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());
}
Also used : QueryTree(com.yahoo.search.query.QueryTree) Test(org.junit.Test)

Aggregations

QueryTree (com.yahoo.search.query.QueryTree)26 Test (org.junit.Test)13 AndItem (com.yahoo.prelude.query.AndItem)7 PhraseItem (com.yahoo.prelude.query.PhraseItem)7 WordItem (com.yahoo.prelude.query.WordItem)7 IndexedItem (com.yahoo.prelude.query.IndexedItem)6 Item (com.yahoo.prelude.query.Item)6 WeakAndItem (com.yahoo.prelude.query.WeakAndItem)6 ExactStringItem (com.yahoo.prelude.query.ExactStringItem)5 PrefixItem (com.yahoo.prelude.query.PrefixItem)5 RegExpItem (com.yahoo.prelude.query.RegExpItem)5 SubstringItem (com.yahoo.prelude.query.SubstringItem)5 SuffixItem (com.yahoo.prelude.query.SuffixItem)5 WordAlternativesItem (com.yahoo.prelude.query.WordAlternativesItem)5 Parsable (com.yahoo.search.query.parser.Parsable)3 ParserEnvironment (com.yahoo.search.query.parser.ParserEnvironment)3 IndexFacts (com.yahoo.prelude.IndexFacts)2 IndexModel (com.yahoo.prelude.IndexModel)2 CompositeItem (com.yahoo.prelude.query.CompositeItem)2 RankItem (com.yahoo.prelude.query.RankItem)2