diff options
Diffstat (limited to 'container-search/src/main/java/com/yahoo/prelude/query/parser/PhraseParser.java')
-rw-r--r-- | container-search/src/main/java/com/yahoo/prelude/query/parser/PhraseParser.java | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/container-search/src/main/java/com/yahoo/prelude/query/parser/PhraseParser.java b/container-search/src/main/java/com/yahoo/prelude/query/parser/PhraseParser.java new file mode 100644 index 00000000000..ba10b7b6ee1 --- /dev/null +++ b/container-search/src/main/java/com/yahoo/prelude/query/parser/PhraseParser.java @@ -0,0 +1,60 @@ +// Copyright 2016 Yahoo Inc. 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.Item; +import com.yahoo.prelude.query.PhraseItem; +import com.yahoo.search.query.parser.ParserEnvironment; + +/** + * Parser for queries of type phrase. + * + * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a> + */ +public class PhraseParser extends AbstractParser { + + public PhraseParser(ParserEnvironment environment) { + super(environment); + } + + protected Item parseItems() { + return forcedPhrase(); + } + + /** + * Ignores everything but words and numbers + * + * @return a phrase item if several words/numbers was found, + * a word item if only one was found + */ + private Item forcedPhrase() { + Item firstWord = null; + PhraseItem phrase = null; + + while (tokens.hasNext()) { + Token token = tokens.next(); + + if (token.kind != Token.Kind.WORD && token.kind != Token.Kind.NUMBER) { + continue; + } + // Note, this depends on segment never creating AndItems when quoted + // (the second argument) is true. + Item newWord = segment(token); + + if (firstWord == null) { // First pass + firstWord = newWord; + } else if (phrase == null) { // Second pass + phrase = new PhraseItem(); + phrase.addItem(firstWord); + phrase.addItem(newWord); + } else { // Following passes + phrase.addItem(newWord); + } + } + if (phrase != null) { + return phrase; + } else { + return firstWord; + } + } + +} |