summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/query/parser/NoGrammarParser.java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2022-02-23 18:40:54 +0100
committerJon Bratseth <bratseth@gmail.com>2022-02-23 18:40:54 +0100
commitb234d9e77c39c1df25830bb53b248a7bce56e3f4 (patch)
treee33261c01ceba400377d335d66e3a32f8c338775 /container-search/src/main/java/com/yahoo/prelude/query/parser/NoGrammarParser.java
parent3be99cd2b3c48f5a457c9647ffb87a20b9f88332 (diff)
Add grammar:none
Diffstat (limited to 'container-search/src/main/java/com/yahoo/prelude/query/parser/NoGrammarParser.java')
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/query/parser/NoGrammarParser.java51
1 files changed, 51 insertions, 0 deletions
diff --git a/container-search/src/main/java/com/yahoo/prelude/query/parser/NoGrammarParser.java b/container-search/src/main/java/com/yahoo/prelude/query/parser/NoGrammarParser.java
new file mode 100644
index 00000000000..70e5ba66c6a
--- /dev/null
+++ b/container-search/src/main/java/com/yahoo/prelude/query/parser/NoGrammarParser.java
@@ -0,0 +1,51 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.prelude.query.parser;
+
+import com.yahoo.prelude.query.BlockItem;
+import com.yahoo.prelude.query.IntItem;
+import com.yahoo.prelude.query.Item;
+import com.yahoo.prelude.query.Substring;
+import com.yahoo.prelude.query.WeakAndItem;
+import com.yahoo.prelude.query.WordItem;
+import com.yahoo.search.query.parser.ParserEnvironment;
+
+import static com.yahoo.prelude.query.parser.Token.Kind.MINUS;
+import static com.yahoo.prelude.query.parser.Token.Kind.NUMBER;
+import static com.yahoo.prelude.query.parser.Token.Kind.UNDERSCORE;
+import static com.yahoo.prelude.query.parser.Token.Kind.WORD;
+
+/**
+ * A parser which turns contiguous searchable character into tokens and filters out other characters.
+ * The resulting tokens are collected into a WeakAnd item.
+ *
+ * @author bratseth
+ */
+public final class NoGrammarParser extends AbstractParser {
+
+ public NoGrammarParser(ParserEnvironment environment) {
+ super(environment);
+ }
+
+ @Override
+ protected Item parseItems() {
+ WeakAndItem weakAnd = new WeakAndItem();
+ Token token;
+ while (null != (token = tokens.next())) {
+ Item termItem = toTerm(token);
+ if (termItem != null)
+ weakAnd.addItem(termItem);
+ }
+ return weakAnd;
+ }
+
+ /** Returns the item representing this token if it is searchable, and null otherwise */
+ private Item toTerm(Token token) {
+ if (token.kind == WORD)
+ return segment("", token, false);
+ else if (token.kind == NUMBER)
+ return new IntItem(token.image);
+ else
+ return null;
+ }
+
+}