|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.plugin.mapper.attachments.test; |
| 21 | + |
| 22 | +import org.elasticsearch.action.search.SearchResponse; |
| 23 | +import org.elasticsearch.common.settings.ImmutableSettings; |
| 24 | +import org.elasticsearch.common.settings.Settings; |
| 25 | +import org.elasticsearch.common.text.Text; |
| 26 | +import org.elasticsearch.plugins.PluginsService; |
| 27 | +import org.elasticsearch.search.highlight.HighlightField; |
| 28 | +import org.elasticsearch.test.ElasticsearchIntegrationTest; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import static org.elasticsearch.client.Requests.putMappingRequest; |
| 33 | +import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath; |
| 34 | +import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath; |
| 35 | +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
| 36 | +import static org.elasticsearch.index.query.QueryBuilders.matchQuery; |
| 37 | +import static org.hamcrest.Matchers.*; |
| 38 | + |
| 39 | +/** |
| 40 | + * |
| 41 | + */ |
| 42 | +@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE) |
| 43 | +public class HighlightAttachmentIntegrationTests extends ElasticsearchIntegrationTest { |
| 44 | + |
| 45 | + @Override |
| 46 | + protected Settings nodeSettings(int nodeOrdinal) { |
| 47 | + return ImmutableSettings.builder() |
| 48 | + .put(super.nodeSettings(nodeOrdinal)) |
| 49 | + .put("plugins." + PluginsService.LOAD_PLUGIN_FROM_CLASSPATH, true) |
| 50 | + .build(); |
| 51 | + } |
| 52 | + |
| 53 | + @Before |
| 54 | + public void createEmptyIndex() throws Exception { |
| 55 | + logger.info("creating index [test]"); |
| 56 | + createIndex("test"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testHighlightAttachment() throws Exception { |
| 61 | + String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/xcontent/test-highlight-mapping.json"); |
| 62 | + byte[] html = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/xcontent/testXHTML.html"); |
| 63 | + |
| 64 | + client().admin().indices().putMapping(putMappingRequest("test").type("person").source(mapping)).actionGet(); |
| 65 | + |
| 66 | + index("test", "person", jsonBuilder().startObject().field("file", html).endObject()); |
| 67 | + refresh(); |
| 68 | + |
| 69 | + SearchResponse searchResponse = client().prepareSearch("test") |
| 70 | + .setQuery(matchQuery("file", "apache tika")) |
| 71 | + .addHighlightedField("file") |
| 72 | + .setNoFields().get(); |
| 73 | + |
| 74 | + logger.info("{}", searchResponse); |
| 75 | + assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); |
| 76 | + assertThat(searchResponse.getHits().getAt(0).getHighlightFields(), notNullValue()); |
| 77 | + assertThat(searchResponse.getHits().getAt(0).getHighlightFields().keySet(), contains("file")); |
| 78 | + searchResponse.getHits().getAt(0).getHighlightFields(); |
| 79 | + for (HighlightField highlightField : searchResponse.getHits().getAt(0).getHighlightFields().values()) { |
| 80 | + for (Text fragment : highlightField.getFragments()) { |
| 81 | + assertThat(fragment.string(), containsString("<em>Apache</em>")); |
| 82 | + assertThat(fragment.string(), containsString("<em>Tika</em>")); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments