17
17
* under the License.
18
18
*/
19
19
20
- package org .elasticsearch .plugin .mapper .attachments .test ;
20
+ package org .elasticsearch .index .mapper .attachment .test . integration ;
21
21
22
22
import org .elasticsearch .action .count .CountResponse ;
23
23
import org .elasticsearch .action .search .SearchResponse ;
24
24
import org .elasticsearch .common .settings .ImmutableSettings ;
25
25
import org .elasticsearch .common .settings .Settings ;
26
+ import org .elasticsearch .common .text .Text ;
26
27
import org .elasticsearch .index .mapper .MapperParsingException ;
27
28
import org .elasticsearch .plugins .PluginsService ;
29
+ import org .elasticsearch .search .highlight .HighlightField ;
28
30
import org .elasticsearch .test .ElasticsearchIntegrationTest ;
29
31
import org .junit .Before ;
30
32
import org .junit .Ignore ;
34
36
import static org .elasticsearch .common .io .Streams .copyToBytesFromClasspath ;
35
37
import static org .elasticsearch .common .io .Streams .copyToStringFromClasspath ;
36
38
import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
39
+ import static org .elasticsearch .index .query .QueryBuilders .matchQuery ;
37
40
import static org .elasticsearch .index .query .QueryBuilders .queryStringQuery ;
38
- import static org .hamcrest .Matchers .equalTo ;
39
- import static org .hamcrest .Matchers .is ;
41
+ import static org .hamcrest .Matchers .* ;
42
+ import static org .hamcrest .Matchers .containsString ;
40
43
41
44
/**
42
45
*
@@ -60,8 +63,8 @@ public void createEmptyIndex() throws Exception {
60
63
61
64
@ Test
62
65
public void testSimpleAttachment () throws Exception {
63
- String mapping = copyToStringFromClasspath ("/org/elasticsearch/index/mapper/xcontent /test-mapping.json" );
64
- byte [] html = copyToBytesFromClasspath ("/org/elasticsearch/index/mapper/xcontent /testXHTML.html" );
66
+ String mapping = copyToStringFromClasspath ("/org/elasticsearch/index/mapper/attachment/test/integration/simple /test-mapping.json" );
67
+ byte [] html = copyToBytesFromClasspath ("/org/elasticsearch/index/mapper/attachment/test/sample-files /testXHTML.html" );
65
68
66
69
client ().admin ().indices ().putMapping (putMappingRequest ("test" ).type ("person" ).source (mapping )).actionGet ();
67
70
@@ -77,8 +80,8 @@ public void testSimpleAttachment() throws Exception {
77
80
78
81
@ Test
79
82
public void testSimpleAttachmentContentLengthLimit () throws Exception {
80
- String mapping = copyToStringFromClasspath ("/org/elasticsearch/index/mapper/xcontent /test-mapping.json" );
81
- byte [] txt = copyToBytesFromClasspath ("/org/elasticsearch/index/mapper/xcontent /testContentLength.txt" );
83
+ String mapping = copyToStringFromClasspath ("/org/elasticsearch/index/mapper/attachment/test/integration/simple /test-mapping.json" );
84
+ byte [] txt = copyToBytesFromClasspath ("/org/elasticsearch/index/mapper/attachment/test/sample-files /testContentLength.txt" );
82
85
final int CONTENT_LENGTH_LIMIT = 20 ;
83
86
84
87
client ().admin ().indices ().putMapping (putMappingRequest ("test" ).type ("person" ).source (mapping )).actionGet ();
@@ -95,8 +98,8 @@ public void testSimpleAttachmentContentLengthLimit() throws Exception {
95
98
96
99
@ Test
97
100
public void testSimpleAttachmentNoContentLengthLimit () throws Exception {
98
- String mapping = copyToStringFromClasspath ("/org/elasticsearch/index/mapper/xcontent /test-mapping.json" );
99
- byte [] txt = copyToBytesFromClasspath ("/org/elasticsearch/index/mapper/xcontent /testContentLength.txt" );
101
+ String mapping = copyToStringFromClasspath ("/org/elasticsearch/index/mapper/attachment/test/integration/simple /test-mapping.json" );
102
+ byte [] txt = copyToBytesFromClasspath ("/org/elasticsearch/index/mapper/attachment/test/sample-files /testContentLength.txt" );
100
103
final int CONTENT_LENGTH_LIMIT = -1 ;
101
104
102
105
client ().admin ().indices ().putMapping (putMappingRequest ("test" ).type ("person" ).source (mapping )).actionGet ();
@@ -118,7 +121,7 @@ public void testSimpleAttachmentNoContentLengthLimit() throws Exception {
118
121
*/
119
122
@ Test (expected = MapperParsingException .class )
120
123
public void testNoContent () throws Exception {
121
- String mapping = copyToStringFromClasspath ("/org/elasticsearch/index/mapper/xcontent /test-mapping.json" );
124
+ String mapping = copyToStringFromClasspath ("/org/elasticsearch/index/mapper/attachment/test/integration/simple /test-mapping.json" );
122
125
123
126
client ().admin ().indices ().putMapping (putMappingRequest ("test" ).type ("person" ).source (mapping )).actionGet ();
124
127
@@ -129,8 +132,8 @@ public void testNoContent() throws Exception {
129
132
public void testContentTypeAndName () throws Exception {
130
133
String dummyContentType = "text/my-dummy-content-type" ;
131
134
String dummyName = "my-dummy-name-txt" ;
132
- String mapping = copyToStringFromClasspath ("/org/elasticsearch/index/mapper/xcontent /test-mapping-store-content-type.json" );
133
- byte [] txt = copyToBytesFromClasspath ("/org/elasticsearch/index/mapper/xcontent /testContentLength.txt" );
135
+ String mapping = copyToStringFromClasspath ("/org/elasticsearch/index/mapper/attachment/test/integration/simple /test-mapping-store-content-type.json" );
136
+ byte [] txt = copyToBytesFromClasspath ("/org/elasticsearch/index/mapper/attachment/test/sample-files /testContentLength.txt" );
134
137
135
138
client ().admin ().indices ().putMapping (putMappingRequest ("test" ).type ("person" ).source (mapping )).actionGet ();
136
139
@@ -156,8 +159,8 @@ public void testContentTypeAndName() throws Exception {
156
159
*/
157
160
@ Test @ Ignore
158
161
public void testCopyTo () throws Exception {
159
- String mapping = copyToStringFromClasspath ("/org/elasticsearch/index/mapper/copy-to /copy-to.json" );
160
- byte [] txt = copyToBytesFromClasspath ("/org/elasticsearch/index/mapper/xcontent /text-in-english.txt" );
162
+ String mapping = copyToStringFromClasspath ("/org/elasticsearch/index/mapper/attachment/test/integration/simple /copy-to.json" );
163
+ byte [] txt = copyToBytesFromClasspath ("/org/elasticsearch/index/mapper/attachment/test/sample-files /text-in-english.txt" );
161
164
162
165
client ().admin ().indices ().putMapping (putMappingRequest ("test" ).type ("person" ).source (mapping )).actionGet ();
163
166
@@ -173,8 +176,8 @@ public void testCopyTo() throws Exception {
173
176
174
177
@ Test
175
178
public void testCopyToSubField () throws Exception {
176
- String mapping = copyToStringFromClasspath ("/org/elasticsearch/index/mapper/copy-to /copy-to-subfield.json" );
177
- byte [] txt = copyToBytesFromClasspath ("/org/elasticsearch/index/mapper/xcontent /text-in-english.txt" );
179
+ String mapping = copyToStringFromClasspath ("/org/elasticsearch/index/mapper/attachment/test/integration/simple /copy-to-subfield.json" );
180
+ byte [] txt = copyToBytesFromClasspath ("/org/elasticsearch/index/mapper/attachment/test/sample-files /text-in-english.txt" );
178
181
179
182
client ().admin ().indices ().putMapping (putMappingRequest ("test" ).type ("person" ).source (mapping )).actionGet ();
180
183
@@ -188,4 +191,31 @@ public void testCopyToSubField() throws Exception {
188
191
assertThat (countResponse .getCount (), equalTo (1l ));
189
192
}
190
193
194
+ @ Test
195
+ public void testHighlightAttachment () throws Exception {
196
+ String mapping = copyToStringFromClasspath ("/org/elasticsearch/index/mapper/attachment/test/integration/simple/test-highlight-mapping.json" );
197
+ byte [] html = copyToBytesFromClasspath ("/org/elasticsearch/index/mapper/attachment/test/sample-files/testXHTML.html" );
198
+
199
+ client ().admin ().indices ().putMapping (putMappingRequest ("test" ).type ("person" ).source (mapping )).actionGet ();
200
+
201
+ index ("test" , "person" , jsonBuilder ().startObject ().field ("file" , html ).endObject ());
202
+ refresh ();
203
+
204
+ SearchResponse searchResponse = client ().prepareSearch ("test" )
205
+ .setQuery (matchQuery ("file" , "apache tika" ))
206
+ .addHighlightedField ("file" )
207
+ .setNoFields ().get ();
208
+
209
+ logger .info ("{}" , searchResponse );
210
+ assertThat (searchResponse .getHits ().getTotalHits (), equalTo (1l ));
211
+ assertThat (searchResponse .getHits ().getAt (0 ).getHighlightFields (), notNullValue ());
212
+ assertThat (searchResponse .getHits ().getAt (0 ).getHighlightFields ().keySet (), contains ("file" ));
213
+ searchResponse .getHits ().getAt (0 ).getHighlightFields ();
214
+ for (HighlightField highlightField : searchResponse .getHits ().getAt (0 ).getHighlightFields ().values ()) {
215
+ for (Text fragment : highlightField .getFragments ()) {
216
+ assertThat (fragment .string (), containsString ("<em>Apache</em>" ));
217
+ assertThat (fragment .string (), containsString ("<em>Tika</em>" ));
218
+ }
219
+ }
220
+ }
191
221
}
0 commit comments