|
20 | 20 | package org.elasticsearch.test.integration.search.highlight; |
21 | 21 |
|
22 | 22 | import org.elasticsearch.ElasticSearchException; |
| 23 | +import org.elasticsearch.action.search.SearchPhaseExecutionException; |
23 | 24 | import org.elasticsearch.action.search.SearchResponse; |
24 | 25 | import org.elasticsearch.action.search.SearchType; |
25 | 26 | import org.elasticsearch.client.Client; |
26 | 27 | import org.elasticsearch.common.settings.ImmutableSettings; |
27 | 28 | import org.elasticsearch.common.xcontent.XContentBuilder; |
28 | 29 | import org.elasticsearch.common.xcontent.XContentFactory; |
| 30 | +import org.elasticsearch.index.query.MatchQueryBuilder; |
29 | 31 | import org.elasticsearch.index.query.QueryBuilders; |
30 | 32 | import org.elasticsearch.indices.IndexMissingException; |
| 33 | +import org.elasticsearch.rest.RestStatus; |
31 | 34 | import org.elasticsearch.search.SearchHit; |
32 | 35 | import org.elasticsearch.search.builder.SearchSourceBuilder; |
33 | 36 | import org.elasticsearch.search.highlight.HighlightBuilder; |
|
49 | 52 | import static org.hamcrest.MatcherAssert.assertThat; |
50 | 53 | import static org.hamcrest.Matchers.equalTo; |
51 | 54 | import static org.hamcrest.Matchers.instanceOf; |
| 55 | +import static org.testng.Assert.fail; |
52 | 56 |
|
53 | 57 | /** |
54 | 58 | * |
@@ -915,8 +919,60 @@ public void testFSHHighlightAllMvFragments() throws Exception { |
915 | 919 | .addHighlightedField("tags", -1, 0) |
916 | 920 | .execute().actionGet(); |
917 | 921 |
|
918 | | - assertThat(2, equalTo(response.hits().hits()[0].highlightFields().get("tags").fragments().length)); |
919 | | - assertThat("this is a really long <em>tag</em> i would like to highlight", equalTo(response.hits().hits()[0].highlightFields().get("tags").fragments()[0].string())); |
920 | | - assertThat("here is another one that is very long and has the <em>tag</em> token near the end", equalTo(response.hits().hits()[0].highlightFields().get("tags").fragments()[1].string())); |
| 922 | + assertThat(response.hits().hits()[0].highlightFields().get("tags").fragments().length, equalTo(2)); |
| 923 | + assertThat(response.hits().hits()[0].highlightFields().get("tags").fragments()[0].string(), equalTo("this is a really long <em>tag</em> i would like to highlight")); |
| 924 | + assertThat(response.hits().hits()[0].highlightFields().get("tags").fragments()[1].string(), equalTo("here is another one that is very long and has the <em>tag</em> token near the end")); |
921 | 925 | } |
| 926 | + |
| 927 | + @Test |
| 928 | + public void testPlainHighlightDifferentFragmenter() throws Exception { |
| 929 | + try { |
| 930 | + client.admin().indices().prepareDelete("test").execute().actionGet(); |
| 931 | + } catch (Exception e) { |
| 932 | + // ignore |
| 933 | + } |
| 934 | + |
| 935 | + client.admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder() |
| 936 | + .put("number_of_shards", 1).put("number_of_replicas", 0)) |
| 937 | + .addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties") |
| 938 | + .startObject("tags").field("type", "string").endObject() |
| 939 | + .endObject().endObject().endObject()) |
| 940 | + .execute().actionGet(); |
| 941 | + |
| 942 | + client.prepareIndex("test", "type1", "1") |
| 943 | + .setSource(jsonBuilder().startObject().field("tags", |
| 944 | + "this is a really long tag i would like to highlight", |
| 945 | + "here is another one that is very long tag and has the tag token near the end").endObject()) |
| 946 | + .setRefresh(true).execute().actionGet(); |
| 947 | + |
| 948 | + SearchResponse response = client.prepareSearch("test") |
| 949 | + .setQuery(QueryBuilders.matchQuery("tags", "long tag").type(MatchQueryBuilder.Type.PHRASE)) |
| 950 | + .addHighlightedField(new HighlightBuilder.Field("tags") |
| 951 | + .fragmentSize(-1).numOfFragments(2).fragmenter("simple")) |
| 952 | + .execute().actionGet(); |
| 953 | + assertThat(response.hits().hits()[0].highlightFields().get("tags").fragments().length, equalTo(2)); |
| 954 | + assertThat(response.hits().hits()[0].highlightFields().get("tags").fragments()[0].string(), equalTo("this is a really <em>long</em> <em>tag</em> i would like to highlight")); |
| 955 | + assertThat(response.hits().hits()[0].highlightFields().get("tags").fragments()[1].string(), equalTo("here is another one that is very <em>long</em> <em>tag</em> and has the tag token near the end")); |
| 956 | + |
| 957 | + response = client.prepareSearch("test") |
| 958 | + .setQuery(QueryBuilders.matchQuery("tags", "long tag").type(MatchQueryBuilder.Type.PHRASE)) |
| 959 | + .addHighlightedField(new HighlightBuilder.Field("tags") |
| 960 | + .fragmentSize(-1).numOfFragments(2).fragmenter("span")) |
| 961 | + .execute().actionGet(); |
| 962 | + assertThat(response.hits().hits()[0].highlightFields().get("tags").fragments().length, equalTo(2)); |
| 963 | + assertThat(response.hits().hits()[0].highlightFields().get("tags").fragments()[0].string(), equalTo("this is a really <em>long</em> <em>tag</em> i would like to highlight")); |
| 964 | + assertThat(response.hits().hits()[0].highlightFields().get("tags").fragments()[1].string(), equalTo("here is another one that is very <em>long</em> <em>tag</em> and has the tag token near the end")); |
| 965 | + |
| 966 | + try { |
| 967 | + client.prepareSearch("test") |
| 968 | + .setQuery(QueryBuilders.matchQuery("tags", "long tag").type(MatchQueryBuilder.Type.PHRASE)) |
| 969 | + .addHighlightedField(new HighlightBuilder.Field("tags") |
| 970 | + .fragmentSize(-1).numOfFragments(2).fragmenter("invalid")) |
| 971 | + .execute().actionGet(); |
| 972 | + fail("Shouldn't get here"); |
| 973 | + } catch (SearchPhaseExecutionException e) { |
| 974 | + assertThat(e.shardFailures()[0].status(), equalTo(RestStatus.BAD_REQUEST)); |
| 975 | + } |
| 976 | + } |
| 977 | + |
922 | 978 | } |
0 commit comments