forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes elastic#108. (cherry picked from commit 2c96550) (cherry picked from commit 440e534)
- Loading branch information
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
...org/elasticsearch/plugin/mapper/attachments/test/HighlightAttachmentIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.plugin.mapper.attachments.test; | ||
|
||
import org.elasticsearch.action.search.SearchResponse; | ||
import org.elasticsearch.common.settings.ImmutableSettings; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.text.Text; | ||
import org.elasticsearch.plugins.PluginsService; | ||
import org.elasticsearch.search.highlight.HighlightField; | ||
import org.elasticsearch.test.ElasticsearchIntegrationTest; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.elasticsearch.client.Requests.putMappingRequest; | ||
import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath; | ||
import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath; | ||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; | ||
import static org.elasticsearch.index.query.QueryBuilders.matchQuery; | ||
import static org.hamcrest.Matchers.*; | ||
|
||
/** | ||
* | ||
*/ | ||
@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE) | ||
public class HighlightAttachmentIntegrationTests extends ElasticsearchIntegrationTest { | ||
|
||
@Override | ||
protected Settings nodeSettings(int nodeOrdinal) { | ||
return ImmutableSettings.builder() | ||
.put(super.nodeSettings(nodeOrdinal)) | ||
.put("plugins." + PluginsService.LOAD_PLUGIN_FROM_CLASSPATH, true) | ||
.build(); | ||
} | ||
|
||
@Before | ||
public void createEmptyIndex() throws Exception { | ||
logger.info("creating index [test]"); | ||
createIndex("test"); | ||
} | ||
|
||
@Test | ||
public void testHighlightAttachment() throws Exception { | ||
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/xcontent/test-highlight-mapping.json"); | ||
byte[] html = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/xcontent/testXHTML.html"); | ||
|
||
client().admin().indices().putMapping(putMappingRequest("test").type("person").source(mapping)).actionGet(); | ||
|
||
index("test", "person", jsonBuilder().startObject().field("file", html).endObject()); | ||
refresh(); | ||
|
||
SearchResponse searchResponse = client().prepareSearch("test") | ||
.setQuery(matchQuery("file", "apache tika")) | ||
.addHighlightedField("file") | ||
.setNoFields().get(); | ||
|
||
logger.info("{}", searchResponse); | ||
assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); | ||
assertThat(searchResponse.getHits().getAt(0).getHighlightFields(), notNullValue()); | ||
assertThat(searchResponse.getHits().getAt(0).getHighlightFields().keySet(), contains("file")); | ||
searchResponse.getHits().getAt(0).getHighlightFields(); | ||
for (HighlightField highlightField : searchResponse.getHits().getAt(0).getHighlightFields().values()) { | ||
for (Text fragment : highlightField.getFragments()) { | ||
assertThat(fragment.string(), containsString("<em>Apache</em>")); | ||
assertThat(fragment.string(), containsString("<em>Tika</em>")); | ||
} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/test/resources/org/elasticsearch/index/mapper/xcontent/test-highlight-mapping.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"person":{ | ||
"properties":{ | ||
"file":{ | ||
"type":"attachment", | ||
"fields": { | ||
"file" : { | ||
"type": "string", | ||
"store" : "yes", | ||
"term_vector": "with_positions_offsets" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |