26
26
import org .elasticsearch .Version ;
27
27
import org .elasticsearch .cluster .metadata .IndexMetadata ;
28
28
import org .elasticsearch .common .Strings ;
29
- import org .elasticsearch .common .bytes .BytesReference ;
30
29
import org .elasticsearch .common .collect .List ;
31
- import org .elasticsearch .common .compress .CompressedXContent ;
32
30
import org .elasticsearch .common .settings .Settings ;
33
- import org .elasticsearch .common .xcontent .XContentFactory ;
34
- import org .elasticsearch .common .xcontent .XContentType ;
35
- import org .elasticsearch .index .IndexService ;
31
+ import org .elasticsearch .common .xcontent .XContentBuilder ;
36
32
import org .elasticsearch .plugins .Plugin ;
37
33
import org .hamcrest .Matchers ;
38
34
import org .junit .Before ;
42
38
import java .util .Collection ;
43
39
import java .util .Set ;
44
40
45
- public class RankFeatureFieldMapperTests extends FieldMapperTestCase <RankFeatureFieldMapper .Builder > {
46
-
47
- IndexService indexService ;
48
- DocumentMapperParser parser ;
49
-
41
+ public class RankFeatureFieldMapperTests extends FieldMapperTestCase2 <RankFeatureFieldMapper .Builder > {
50
42
@ Override
51
43
protected Set <String > unsupportedProperties () {
52
44
return org .elasticsearch .common .collect .Set .of ("analyzer" , "similarity" , "store" , "doc_values" , "index" );
53
45
}
54
46
55
47
@ Before
56
48
public void setup () {
57
- indexService = createIndex ("test" );
58
- parser = indexService .mapperService ().documentMapperParser ();
59
49
addModifier ("positive_score_impact" , false , (a , b ) -> {
60
50
a .positiveScoreImpact (true );
61
51
b .positiveScoreImpact (false );
62
52
});
63
53
}
64
54
65
55
@ Override
66
- protected Collection <Class < ? extends Plugin > > getPlugins () {
67
- return pluginList ( MapperExtrasPlugin . class );
56
+ protected Collection <? extends Plugin > getPlugins () {
57
+ return List . of ( new MapperExtrasPlugin () );
68
58
}
69
59
70
60
static int getFrequency (TokenStream tk ) throws IOException {
@@ -81,34 +71,27 @@ protected RankFeatureFieldMapper.Builder newBuilder() {
81
71
return new RankFeatureFieldMapper .Builder ("rank-feature" );
82
72
}
83
73
84
- public void testDefaults () throws Exception {
85
- String mapping = Strings .toString (XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
86
- .startObject ("properties" ).startObject ("field" ).field ("type" , "rank_feature" ).endObject ().endObject ()
87
- .endObject ().endObject ());
88
-
89
- DocumentMapper mapper = parser .parse ("type" , new CompressedXContent (mapping ));
74
+ @ Override
75
+ protected void minimalMapping (XContentBuilder b ) throws IOException {
76
+ b .field ("type" , "rank_feature" );
77
+ }
90
78
91
- assertEquals (mapping , mapper .mappingSource ().toString ());
79
+ @ Override
80
+ protected boolean supportsMeta () {
81
+ return false ;
82
+ }
92
83
93
- ParsedDocument doc1 = mapper .parse (new SourceToParse ("test" , "type" , "1" , BytesReference
94
- .bytes (XContentFactory .jsonBuilder ()
95
- .startObject ()
96
- .field ("field" , 10 )
97
- .endObject ()),
98
- XContentType .JSON ));
84
+ public void testDefaults () throws Exception {
85
+ DocumentMapper mapper = createDocumentMapper (fieldMapping (this ::minimalMapping ));
86
+ assertEquals (Strings .toString (fieldMapping (this ::minimalMapping )), mapper .mappingSource ().toString ());
99
87
88
+ ParsedDocument doc1 = mapper .parse (source (b -> b .field ("field" , 10 )));
100
89
IndexableField [] fields = doc1 .rootDoc ().getFields ("_feature" );
101
90
assertEquals (1 , fields .length );
102
91
assertThat (fields [0 ], Matchers .instanceOf (FeatureField .class ));
103
92
FeatureField featureField1 = (FeatureField ) fields [0 ];
104
93
105
- ParsedDocument doc2 = mapper .parse (new SourceToParse ("test" , "type" , "1" , BytesReference
106
- .bytes (XContentFactory .jsonBuilder ()
107
- .startObject ()
108
- .field ("field" , 12 )
109
- .endObject ()),
110
- XContentType .JSON ));
111
-
94
+ ParsedDocument doc2 = mapper .parse (source (b -> b .field ("field" , 12 )));
112
95
FeatureField featureField2 = (FeatureField ) doc2 .rootDoc ().getFields ("_feature" )[0 ];
113
96
114
97
int freq1 = getFrequency (featureField1 .tokenStream (null , null ));
@@ -117,34 +100,17 @@ public void testDefaults() throws Exception {
117
100
}
118
101
119
102
public void testNegativeScoreImpact () throws Exception {
120
- String mapping = Strings .toString (XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
121
- .startObject ("properties" ).startObject ("field" ).field ("type" , "rank_feature" )
122
- .field ("positive_score_impact" , false ).endObject ().endObject ()
123
- .endObject ().endObject ());
124
-
125
- DocumentMapper mapper = parser .parse ("type" , new CompressedXContent (mapping ));
126
-
127
- assertEquals (mapping , mapper .mappingSource ().toString ());
128
-
129
- ParsedDocument doc1 = mapper .parse (new SourceToParse ("test" , "type" , "1" , BytesReference
130
- .bytes (XContentFactory .jsonBuilder ()
131
- .startObject ()
132
- .field ("field" , 10 )
133
- .endObject ()),
134
- XContentType .JSON ));
103
+ DocumentMapper mapper = createDocumentMapper (
104
+ fieldMapping (b -> b .field ("type" , "rank_feature" ).field ("positive_score_impact" , false ))
105
+ );
135
106
107
+ ParsedDocument doc1 = mapper .parse (source (b -> b .field ("field" , 10 )));
136
108
IndexableField [] fields = doc1 .rootDoc ().getFields ("_feature" );
137
109
assertEquals (1 , fields .length );
138
110
assertThat (fields [0 ], Matchers .instanceOf (FeatureField .class ));
139
111
FeatureField featureField1 = (FeatureField ) fields [0 ];
140
112
141
- ParsedDocument doc2 = mapper .parse (new SourceToParse ("test" , "type" , "1" , BytesReference
142
- .bytes (XContentFactory .jsonBuilder ()
143
- .startObject ()
144
- .field ("field" , 12 )
145
- .endObject ()),
146
- XContentType .JSON ));
147
-
113
+ ParsedDocument doc2 = mapper .parse (source (b -> b .field ("field" , 12 )));
148
114
FeatureField featureField2 = (FeatureField ) doc2 .rootDoc ().getFields ("_feature" )[0 ];
149
115
150
116
int freq1 = getFrequency (featureField1 .tokenStream (null , null ));
@@ -153,39 +119,30 @@ public void testNegativeScoreImpact() throws Exception {
153
119
}
154
120
155
121
public void testRejectMultiValuedFields () throws MapperParsingException , IOException {
156
- String mapping = Strings .toString (XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
157
- .startObject ("properties" ).startObject ("field" ).field ("type" , "rank_feature" ).endObject ().startObject ("foo" )
158
- .startObject ("properties" ).startObject ("field" ).field ("type" , "rank_feature" ).endObject ().endObject ()
159
- .endObject ().endObject ().endObject ().endObject ());
160
-
161
- DocumentMapper mapper = parser .parse ("type" , new CompressedXContent (mapping ));
162
-
163
- assertEquals (mapping , mapper .mappingSource ().toString ());
164
-
165
- MapperParsingException e = expectThrows (MapperParsingException .class ,
166
- () -> mapper .parse (new SourceToParse ("test" , "type" , "1" , BytesReference
167
- .bytes (XContentFactory .jsonBuilder ()
168
- .startObject ()
169
- .field ("field" , Arrays .asList (10 , 20 ))
170
- .endObject ()),
171
- XContentType .JSON )));
122
+ DocumentMapper mapper = createDocumentMapper (mapping (b -> {
123
+ b .startObject ("field" ).field ("type" , "rank_feature" ).endObject ();
124
+ b .startObject ("foo" ).startObject ("properties" );
125
+ {
126
+ b .startObject ("field" ).field ("type" , "rank_feature" ).endObject ();
127
+ }
128
+ b .endObject ().endObject ();
129
+ }));
130
+
131
+ MapperParsingException e = expectThrows (
132
+ MapperParsingException .class ,
133
+ () -> mapper .parse (source (b -> b .field ("field" , Arrays .asList (10 , 20 ))))
134
+ );
172
135
assertEquals ("[rank_feature] fields do not support indexing multiple values for the same field [field] in the same document" ,
173
136
e .getCause ().getMessage ());
174
137
175
- e = expectThrows (MapperParsingException .class ,
176
- () -> mapper .parse (new SourceToParse ("test" , "type" , "1" , BytesReference
177
- .bytes (XContentFactory .jsonBuilder ()
178
- .startObject ()
179
- .startArray ("foo" )
180
- .startObject ()
181
- .field ("field" , 10 )
182
- .endObject ()
183
- .startObject ()
184
- .field ("field" , 20 )
185
- .endObject ()
186
- .endArray ()
187
- .endObject ()),
188
- XContentType .JSON )));
138
+ e = expectThrows (MapperParsingException .class , () -> mapper .parse (source (b -> {
139
+ b .startArray ("foo" );
140
+ {
141
+ b .startObject ().field ("field" , 10 ).endObject ();
142
+ b .startObject ().field ("field" , 20 ).endObject ();
143
+ }
144
+ b .endArray ();
145
+ })));
189
146
assertEquals ("[rank_feature] fields do not support indexing multiple values for the same field [foo.field] in the same document" ,
190
147
e .getCause ().getMessage ());
191
148
}
0 commit comments