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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.processing;
import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.document.CollectionDataType;
import com.yahoo.document.DataType;
import com.yahoo.searchdefinition.RankProfileRegistry;
import com.yahoo.searchdefinition.Schema;
import com.yahoo.searchdefinition.document.Matching;
import com.yahoo.searchdefinition.document.MatchType;
import com.yahoo.searchdefinition.document.SDField;
import com.yahoo.searchdefinition.document.Stemming;
import com.yahoo.vespa.indexinglanguage.expressions.*;
import com.yahoo.vespa.model.container.search.QueryProfiles;
/**
* The implementation of "gram" matching - splitting the incoming text and the queries into
* n-grams for matching. This will also validate the gram settings.
*
* @author bratseth
*/
public class NGramMatch extends Processor {
public static final int DEFAULT_GRAM_SIZE = 2;
public NGramMatch(Schema schema, DeployLogger deployLogger, RankProfileRegistry rankProfileRegistry, QueryProfiles queryProfiles) {
super(schema, deployLogger, rankProfileRegistry, queryProfiles);
}
@Override
public void process(boolean validate, boolean documentsOnly) {
for (SDField field : schema.allConcreteFields()) {
if (field.getMatching().getType().equals(MatchType.GRAM))
implementGramMatch(schema, field, validate);
else if (validate && field.getMatching().getGramSize() >= 0)
throw new IllegalArgumentException("gram-size can only be set when the matching mode is 'gram'");
}
}
private void implementGramMatch(Schema schema, SDField field, boolean validate) {
if (validate && field.doesAttributing() && ! field.doesIndexing())
throw new IllegalArgumentException("gram matching is not supported with attributes, use 'index' in indexing");
int n = field.getMatching().getGramSize();
if (n < 0)
n = DEFAULT_GRAM_SIZE; // not set - use default gram size
if (validate && n == 0)
throw new IllegalArgumentException("Illegal gram size in " + field + ": Must be at least 1");
field.getNormalizing().inferCodepoint();
field.setStemming(Stemming.NONE); // not compatible with stemming and normalizing
field.addQueryCommand("ngram " + n);
field.setIndexingScript((ScriptExpression)new MyProvider(schema, n).convert(field.getIndexingScript()));
}
private static class MyProvider extends TypedTransformProvider {
final int ngram;
MyProvider(Schema schema, int ngram) {
super(NGramExpression.class, schema);
this.ngram = ngram;
}
@Override
protected boolean requiresTransform(Expression exp, DataType fieldType) {
return exp instanceof OutputExpression;
}
@Override
protected Expression newTransform(DataType fieldType) {
Expression exp = new NGramExpression(null, ngram);
if (fieldType instanceof CollectionDataType)
exp = new ForEachExpression(exp);
return exp;
}
}
}
|