diff options
Diffstat (limited to 'container-search/src/main/java/com/yahoo/search/query/parser')
5 files changed, 270 insertions, 0 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/query/parser/Parsable.java b/container-search/src/main/java/com/yahoo/search/query/parser/Parsable.java new file mode 100644 index 00000000000..92601a5464d --- /dev/null +++ b/container-search/src/main/java/com/yahoo/search/query/parser/Parsable.java @@ -0,0 +1,112 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.query.parser; + +import com.yahoo.language.Language; +import com.yahoo.search.query.Model; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +/** + * <p>This class encapsulates all the parameters required to call {@link Parser#parse(Parsable)}. Because all set- + * methods return a reference to self, you can write very compact calls to the parser:</p> + * + * <pre> + * parser.parse(new Parsable() + * .setQuery("foo") + * .setFilter("bar") + * .setDefaultIndexName("default") + * .setLanguage(Language.ENGLISH)) + * </pre> + * + * <p>In case you are parsing the content of a {@link Model}, you can use the {@link #fromQueryModel(Model)} factory for + * convenience.</p> + * + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + * @since 5.1.4 + */ +public final class Parsable { + + private final Set<String> sourceList = new HashSet<>(); + private final Set<String> restrictList = new HashSet<>(); + private String query; + private String filter; + private String defaultIndexName; + private Language language; + + public String getQuery() { + return query; + } + + public Parsable setQuery(String query) { + this.query = query; + return this; + } + + public String getFilter() { + return filter; + } + + public Parsable setFilter(String filter) { + this.filter = filter; + return this; + } + + public String getDefaultIndexName() { + return defaultIndexName; + } + + public Parsable setDefaultIndexName(String defaultIndexName) { + this.defaultIndexName = defaultIndexName; + return this; + } + + public Language getLanguage() { + return language; + } + + public Parsable setLanguage(Language language) { + this.language = language; + return this; + } + + public Set<String> getSources() { + return sourceList; + } + + public Parsable addSource(String sourceName) { + sourceList.add(sourceName); + return this; + } + + public Parsable addSources(Collection<String> sourceNames) { + sourceList.addAll(sourceNames); + return this; + } + + public Set<String> getRestrict() { + return restrictList; + } + + public Parsable addRestrict(String restrictName) { + restrictList.add(restrictName); + return this; + } + + public Parsable addRestricts(Collection<String> restrictNames) { + restrictList.addAll(restrictNames); + return this; + } + + public static Parsable fromQueryModel(Model model) { + return new Parsable() + .setQuery(model.getQueryString()) + .setFilter(model.getFilter()) + .setLanguage(model.getParsingLanguage()) + .setDefaultIndexName(model.getDefaultIndex()) + .addSources(model.getSources()) + .addRestricts(model.getRestrict()); + } + +} diff --git a/container-search/src/main/java/com/yahoo/search/query/parser/Parser.java b/container-search/src/main/java/com/yahoo/search/query/parser/Parser.java new file mode 100644 index 00000000000..3822b9b67d8 --- /dev/null +++ b/container-search/src/main/java/com/yahoo/search/query/parser/Parser.java @@ -0,0 +1,24 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.query.parser; + +import com.yahoo.search.query.QueryTree; + +/** + * Defines the interface of a query parser. To construct an instance of this class, use the {@link ParserFactory}. + * + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + */ +public interface Parser { + + /** + * Parser the given {@link Parsable}, and returns a corresponding + * {@link QueryTree}. If parsing fails without an exception, the contained + * root will be an instance of {@link com.yahoo.prelude.query.NullItem}. + * + * @param query + * the Parsable to parse + * @return the parsed QueryTree, never null + */ + QueryTree parse(Parsable query); + +} diff --git a/container-search/src/main/java/com/yahoo/search/query/parser/ParserEnvironment.java b/container-search/src/main/java/com/yahoo/search/query/parser/ParserEnvironment.java new file mode 100644 index 00000000000..b00afa27bf6 --- /dev/null +++ b/container-search/src/main/java/com/yahoo/search/query/parser/ParserEnvironment.java @@ -0,0 +1,76 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.query.parser; + +import com.yahoo.language.Linguistics; +import com.yahoo.language.simple.SimpleLinguistics; +import com.yahoo.prelude.IndexFacts; +import com.yahoo.prelude.query.parser.SpecialTokenRegistry; +import com.yahoo.prelude.query.parser.SpecialTokens; +import com.yahoo.search.Searcher; +import com.yahoo.search.searchchain.Execution; + +/** + * This class encapsulates the environment of a {@link Parser}. In case you are creating a parser from within a + * {@link Searcher}, you can use the {@link #fromExecutionContext(Execution.Context)} factory for convenience. + * + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + * @since 5.1.4 + */ +public final class ParserEnvironment { + + private IndexFacts indexFacts = new IndexFacts(); + private Linguistics linguistics = new SimpleLinguistics(); + private SpecialTokens specialTokens = new SpecialTokens(); + + public IndexFacts getIndexFacts() { + return indexFacts; + } + + public ParserEnvironment setIndexFacts(IndexFacts indexFacts) { + this.indexFacts = indexFacts; + return this; + } + + public Linguistics getLinguistics() { + return linguistics; + } + + public ParserEnvironment setLinguistics(Linguistics linguistics) { + this.linguistics = linguistics; + return this; + } + + public SpecialTokens getSpecialTokens() { + return specialTokens; + } + + public ParserEnvironment setSpecialTokens(SpecialTokens specialTokens) { + this.specialTokens = specialTokens; + return this; + } + + public static ParserEnvironment fromExecutionContext(Execution.Context context) { + ParserEnvironment env = new ParserEnvironment(); + if (context == null) { + return env; + } + if (context.getIndexFacts() != null) { + env.setIndexFacts(context.getIndexFacts()); + } + if (context.getLinguistics() != null) { + env.setLinguistics(context.getLinguistics()); + } + SpecialTokenRegistry registry = context.getTokenRegistry(); + if (registry != null) { + env.setSpecialTokens(registry.getSpecialTokens("default")); + } + return env; + } + + public static ParserEnvironment fromParserEnvironment(ParserEnvironment environment) { + return new ParserEnvironment() + .setIndexFacts(environment.indexFacts) + .setLinguistics(environment.linguistics) + .setSpecialTokens(environment.specialTokens); + } +} diff --git a/container-search/src/main/java/com/yahoo/search/query/parser/ParserFactory.java b/container-search/src/main/java/com/yahoo/search/query/parser/ParserFactory.java new file mode 100644 index 00000000000..e0a3338fec2 --- /dev/null +++ b/container-search/src/main/java/com/yahoo/search/query/parser/ParserFactory.java @@ -0,0 +1,48 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.query.parser; + +import com.yahoo.prelude.query.parser.*; +import com.yahoo.search.Query; +import com.yahoo.search.yql.YqlParser; + +/** + * <p>Implements a factory for {@link Parser}.</p> + * + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + * @since 5.1.4 + */ +public final class ParserFactory { + + private ParserFactory() { + // hide + } + + /** + * Creates a {@link Parser} appropriate for the given <tt>Query.Type</tt>, providing the Parser with access to + * the {@link ParserEnvironment} given. + * + * @param type the query type for which to create a Parser + * @param environment the environment settings to attach to the Parser + * @return the created Parser + */ + public static Parser newInstance(Query.Type type, ParserEnvironment environment) { + switch (type) { + case ALL: + return new AllParser(environment); + case ANY: + return new AnyParser(environment); + case PHRASE: + return new PhraseParser(environment); + case ADVANCED: + return new AdvancedParser(environment); + case WEB: + return new WebParser(environment); + case PROGRAMMATIC: + return new ProgrammaticParser(); + case YQL: + return new YqlParser(environment); + default: + throw new UnsupportedOperationException(type.toString()); + } + } +} diff --git a/container-search/src/main/java/com/yahoo/search/query/parser/package-info.java b/container-search/src/main/java/com/yahoo/search/query/parser/package-info.java new file mode 100644 index 00000000000..ddae3e83ddb --- /dev/null +++ b/container-search/src/main/java/com/yahoo/search/query/parser/package-info.java @@ -0,0 +1,10 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +/** + * Provides access to parsing query strings into queries + */ +@ExportPackage +@PublicApi +package com.yahoo.search.query.parser; + +import com.yahoo.api.annotations.PublicApi; +import com.yahoo.osgi.annotation.ExportPackage; |