summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/fieldoperation/IndexingOperation.java
blob: 87fa74b92fe4ff6a2bebd0009fa87b89611bb1f4 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.fieldoperation;

import com.yahoo.language.Linguistics;
import com.yahoo.language.process.Encoder;
import com.yahoo.language.simple.SimpleLinguistics;
import com.yahoo.searchdefinition.document.SDField;
import com.yahoo.searchdefinition.parser.ParseException;
import com.yahoo.searchdefinition.parser.SimpleCharStream;
import com.yahoo.vespa.indexinglanguage.ScriptParserContext;
import com.yahoo.vespa.indexinglanguage.expressions.ScriptExpression;
import com.yahoo.vespa.indexinglanguage.expressions.StatementExpression;
import com.yahoo.vespa.indexinglanguage.linguistics.AnnotatorConfig;
import com.yahoo.yolean.Exceptions;

/**
 * @author Einar M R Rosenvinge
 */
public class IndexingOperation implements FieldOperation {

    private final ScriptExpression script;

    public IndexingOperation(ScriptExpression script) {
        this.script = script;
    }

    public void apply(SDField field) {
        field.setIndexingScript(script);
    }

    /** Creates an indexing operation which will use the simple linguistics implementation suitable for testing */
    public static IndexingOperation fromStream(SimpleCharStream input, boolean multiLine) throws ParseException {
        return fromStream(input, multiLine, new SimpleLinguistics(), Encoder.throwsOnUse);
    }

    public static IndexingOperation fromStream(SimpleCharStream input, boolean multiLine,
                                               Linguistics linguistics, Encoder encoder)
            throws ParseException {
        ScriptParserContext config = new ScriptParserContext(linguistics, encoder);
        config.setAnnotatorConfig(new AnnotatorConfig());
        config.setInputStream(input);
        ScriptExpression exp;
        try {
            if (multiLine) {
                exp = ScriptExpression.newInstance(config);
            } else {
                exp = new ScriptExpression(StatementExpression.newInstance(config));
            }
        } catch (com.yahoo.vespa.indexinglanguage.parser.ParseException e) {
            ParseException t = new ParseException("Could not parse indexing statement: " + Exceptions.toMessageString(e));
            t.initCause(e);
            throw t;
        }
        return new IndexingOperation(exp);
    }

}