aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/query/parser/PhraseParser.java
blob: 01b5b94382976089e662d1d33af97186881ee3c0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// 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.Item;
import com.yahoo.prelude.query.PhraseItem;
import com.yahoo.search.query.parser.ParserEnvironment;

/**
 * Parser for queries of type phrase.
 *
 * @author Steinar Knutsen
 */
public class PhraseParser extends AbstractParser {

    public PhraseParser(ParserEnvironment environment) {
        super(environment);
    }

    @Override
    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(null, token, true);

            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;
        }
    }

}