aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/schema/processing/MatchedElementsOnlyResolverTestCase.java
blob: c401376ac3ae49cd653b7525b36597ab806592ea (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.processing;

import com.yahoo.schema.RankProfileRegistry;
import com.yahoo.schema.Schema;
import com.yahoo.schema.ApplicationBuilder;
import com.yahoo.schema.parser.ParseException;
import com.yahoo.vespa.documentmodel.SummaryField;
import com.yahoo.vespa.documentmodel.SummaryTransform;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static com.yahoo.config.model.test.TestUtil.joinLines;
import static org.junit.Assert.assertEquals;

/**
 * @author geirst
 */
public class MatchedElementsOnlyResolverTestCase {

    @SuppressWarnings("deprecation")
    @Rule
    public final ExpectedException exceptionRule = ExpectedException.none();

    @Test
    public void complex_field_with_some_struct_field_attributes_gets_default_transform() throws ParseException {
        assertSummaryField(joinLines("field my_field type map<string, string> {",
                "  indexing: summary",
                "  summary: matched-elements-only",
                "  struct-field key { indexing: attribute }",
                "}"),
                "my_field", SummaryTransform.MATCHED_ELEMENTS_FILTER);

        assertSummaryField(joinLines("field my_field type map<string, elem> {",
                "  indexing: summary",
                "  summary: matched-elements-only",
                "  struct-field key { indexing: attribute }",
                "}"),
                "my_field", SummaryTransform.MATCHED_ELEMENTS_FILTER);

        assertSummaryField(joinLines("field my_field type array<elem> {",
                "  indexing: summary",
                "  summary: matched-elements-only",
                "  struct-field name { indexing: attribute }",
                "}"),
                "my_field", SummaryTransform.MATCHED_ELEMENTS_FILTER);
    }

    @Test
    public void complex_field_with_only_struct_field_attributes_gets_attribute_transform() throws ParseException {
        assertSummaryField(joinLines("field my_field type map<string, string> {",
                "  indexing: summary",
                "  summary: matched-elements-only",
                "  struct-field key { indexing: attribute }",
                "  struct-field value { indexing: attribute }",
                "}"),
                "my_field", SummaryTransform.MATCHED_ATTRIBUTE_ELEMENTS_FILTER);

        assertSummaryField(joinLines("field my_field type map<string, elem> {",
                "  indexing: summary",
                "  summary: matched-elements-only",
                "  struct-field key { indexing: attribute }",
                "  struct-field value.name { indexing: attribute }",
                "  struct-field value.weight { indexing: attribute }",
                "}"),
                "my_field", SummaryTransform.MATCHED_ATTRIBUTE_ELEMENTS_FILTER);

        assertSummaryField(joinLines("field my_field type array<elem> {",
                "  indexing: summary",
                "  summary: matched-elements-only",
                "  struct-field name { indexing: attribute }",
                "  struct-field weight { indexing: attribute }",
                "}"),
                "my_field", SummaryTransform.MATCHED_ATTRIBUTE_ELEMENTS_FILTER);
    }

    @Test
    public void explicit_complex_summary_field_can_use_filter_transform_with_reference_to_source_field() throws ParseException {
        String documentSummary = joinLines("document-summary my_summary {",
                "  summary my_filter_field type map<string, string> {",
                "    source: my_field",
                "    matched-elements-only",
                "  }",
                "}");
        {
            var search = buildSearch(joinLines("field my_field type map<string, string> {",
                    "  indexing: summary",
                    "  struct-field key { indexing: attribute }",
                    "}"),
                    documentSummary);
            assertSummaryField(search.getSummaryField("my_filter_field"),
                    SummaryTransform.MATCHED_ELEMENTS_FILTER, "my_field");
            assertSummaryField(search.getSummaryField("my_field"),
                    SummaryTransform.NONE, "my_field");
        }
        {
            var search = buildSearch(joinLines("field my_field type map<string, string> {",
                    "  indexing: summary",
                    "  struct-field key { indexing: attribute }",
                    "  struct-field value { indexing: attribute }",
                    "}"),
                    documentSummary);
            assertSummaryField(search.getSummaryField("my_filter_field"),
                    SummaryTransform.MATCHED_ATTRIBUTE_ELEMENTS_FILTER, "my_field");
            assertSummaryField(search.getSummaryField("my_field"),
                    SummaryTransform.ATTRIBUTECOMBINER, "my_field");
        }
    }

    @Test
    public void primitive_array_attribute_field_gets_attribute_transform() throws ParseException {
        assertSummaryField(joinLines("field my_field type array<string> {",
                "  indexing: attribute | summary",
                "  summary: matched-elements-only",
                "}"),
                "my_field", SummaryTransform.MATCHED_ATTRIBUTE_ELEMENTS_FILTER);
    }

    @Test
    public void primitive_weighted_set_attribute_field_gets_attribute_transform() throws ParseException {
        assertSummaryField(joinLines("field my_field type weightedset<string> {",
                "  indexing: attribute | summary",
                "  summary: matched-elements-only",
                "}"),
                "my_field", SummaryTransform.MATCHED_ATTRIBUTE_ELEMENTS_FILTER);
    }

    @Test
    public void explicit_summary_field_can_use_filter_transform_with_reference_to_attribute_source_field() throws ParseException {
        String documentSummary = joinLines("document-summary my_summary {",
                "  summary my_filter_field type array<string> {",
                "    source: my_field",
                "    matched-elements-only",
                "  }",
                "}");

        var search = buildSearch(joinLines(
                "field my_field type array<string> {",
                "  indexing: attribute | summary",
                "}"),
                documentSummary);
        assertSummaryField(search.getSummaryField("my_filter_field"),
                SummaryTransform.MATCHED_ATTRIBUTE_ELEMENTS_FILTER, "my_field");
        assertSummaryField(search.getSummaryField("my_field"),
                SummaryTransform.ATTRIBUTE, "my_field");
    }

    @Test
    public void unsupported_field_type_throws() throws ParseException {
        exceptionRule.expect(IllegalArgumentException.class);
        exceptionRule.expectMessage("For schema 'test', document summary 'default', summary field 'my_field': " +
                "'matched-elements-only' is not supported for this field type. " +
                "Supported field types are: array of primitive, weighted set of primitive, " +
                "array of simple struct, map of primitive type to simple struct, " +
                "and map of primitive type to primitive type");
        buildSearch(joinLines("field my_field type string {",
                "  indexing: summary",
                "  summary: matched-elements-only",
                "}"));
    }

    private void assertSummaryField(String fieldContent, String fieldName, SummaryTransform expTransform) throws ParseException {
        var search = buildSearch(fieldContent);
        assertSummaryField(search.getSummaryField(fieldName), expTransform, fieldName);
    }

    private void assertSummaryField(SummaryField field, SummaryTransform expTransform, String expSourceField) {
        assertEquals(expTransform, field.getTransform());
        assertEquals(expSourceField, field.getSingleSource());
    }

    private Schema buildSearch(String field) throws ParseException {
        return buildSearch(field, "");
    }

    private Schema buildSearch(String field, String summary) throws ParseException {
        var builder = new ApplicationBuilder(new RankProfileRegistry());
        builder.addSchema(joinLines("search test {",
                                    "  document test {",
                                    "    struct elem {",
                                    "      field name type string {}",
                                    "      field weight type int {}",
                                    "    }",
                                    field,
                                    "  }",
                                    summary,
                                    "}"));
        builder.build(true);
        return builder.getSchema();
    }
}