aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/searchdefinition/processing/NGramTestCase.java
blob: 0219d86e182dc860f467c17178cf6258e59fdbdb (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.processing;

import com.yahoo.searchdefinition.Search;
import com.yahoo.searchdefinition.SearchBuilder;
import com.yahoo.searchdefinition.SearchDefinitionTestCase;
import com.yahoo.searchdefinition.document.Matching;
import com.yahoo.searchdefinition.document.SDField;
import com.yahoo.searchdefinition.document.Stemming;
import com.yahoo.searchdefinition.parser.ParseException;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

import static org.junit.Assert.*;
/**
 * @author bratseth
 */
public class NGramTestCase extends SearchDefinitionTestCase {

    @Test
    public void testNGram() throws IOException, ParseException {
        Search search = SearchBuilder.buildFromFile("src/test/examples/ngram.sd");
        assertNotNull(search);

        SDField gram1=search.getConcreteField("gram_1");
        assertEquals(Matching.Type.GRAM,gram1.getMatching().getType());
        assertEquals(1,gram1.getMatching().getGramSize());

        SDField gram2=search.getConcreteField("gram_2");
        assertEquals(Matching.Type.GRAM,gram2.getMatching().getType());
        assertEquals(-1,gram2.getMatching().getGramSize()); // Not set explicitly

        SDField gram3=search.getConcreteField("gram_3");
        assertEquals(Matching.Type.GRAM,gram3.getMatching().getType());
        assertEquals(3,gram3.getMatching().getGramSize());

        assertEquals("input gram_1 | ngram 1 | index gram_1 | summary gram_1",gram1.getIndexingScript().iterator().next().toString());
        assertEquals("input gram_2 | ngram 2 | index gram_2",gram2.getIndexingScript().iterator().next().toString());
        assertEquals("input gram_3 | ngram 3 | index gram_3",gram3.getIndexingScript().iterator().next().toString());

        assertFalse(gram1.getNormalizing().doRemoveAccents());
        assertEquals(Stemming.NONE,gram1.getStemming());

        List<String> queryCommands=gram1.getQueryCommands();
        assertEquals(1,queryCommands.size());
        assertEquals("ngram 1",queryCommands.get(0));
    }

    @Test
    public void testInvalidNGramSetting1() throws IOException, ParseException {
        try {
            Search search = SearchBuilder.buildFromFile("src/test/examples/invalidngram1.sd");
            fail("Should cause an exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("gram-size can only be set when the matching mode is 'gram'",e.getMessage());
        }
    }

    @Test
    public void testInvalidNGramSetting2() throws IOException, ParseException {
        try {
            Search search = SearchBuilder.buildFromFile("src/test/examples/invalidngram2.sd");
            fail("Should cause an exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("gram-size can only be set when the matching mode is 'gram'",e.getMessage());
        }
    }

    @Test
    public void testInvalidNGramSetting3() throws IOException, ParseException {
        try {
            Search search = SearchBuilder.buildFromFile("src/test/examples/invalidngram3.sd");
            fail("Should cause an exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("gram matching is not supported with attributes, use 'index' not 'attribute' in indexing",e.getMessage());
        }
    }

}