|
21 | 21 |
|
22 | 22 | import org.elasticsearch.action.search.SearchResponse; |
23 | 23 | import org.elasticsearch.common.settings.Settings; |
| 24 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 25 | +import org.elasticsearch.common.xcontent.XContentFactory; |
24 | 26 | import org.elasticsearch.index.IndexSettings; |
25 | 27 | import org.elasticsearch.index.query.Operator; |
26 | 28 | import org.elasticsearch.plugins.Plugin; |
| 29 | +import org.elasticsearch.search.builder.SearchSourceBuilder; |
27 | 30 | import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; |
28 | 31 | import org.elasticsearch.test.ESIntegTestCase; |
29 | 32 |
|
30 | 33 | import java.io.IOException; |
31 | 34 | import java.util.Arrays; |
32 | 35 | import java.util.Collection; |
33 | 36 |
|
| 37 | +import static org.elasticsearch.client.Requests.searchRequest; |
34 | 38 | import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
| 39 | +import static org.elasticsearch.index.query.QueryBuilders.boolQuery; |
| 40 | +import static org.elasticsearch.index.query.QueryBuilders.matchPhrasePrefixQuery; |
35 | 41 | import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery; |
36 | 42 | import static org.elasticsearch.index.query.QueryBuilders.matchQuery; |
| 43 | +import static org.elasticsearch.index.query.QueryBuilders.termQuery; |
| 44 | +import static org.elasticsearch.search.builder.SearchSourceBuilder.highlight; |
| 45 | +import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; |
37 | 46 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
38 | 47 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHighlight; |
| 48 | +import static org.hamcrest.Matchers.anyOf; |
39 | 49 | import static org.hamcrest.Matchers.equalTo; |
40 | 50 | import static org.hamcrest.Matchers.startsWith; |
41 | 51 |
|
@@ -153,4 +163,165 @@ public void testMultiPhraseCutoff() throws IOException { |
153 | 163 | + "<em>http://www.facebook.com</em> <em>http://elasticsearch.org</em> " |
154 | 164 | + "<em>http://xing.com</em> <em>http://cnn.com</em> http://quora.com")); |
155 | 165 | } |
| 166 | + |
| 167 | + public void testSynonyms() throws IOException { |
| 168 | + Settings.Builder builder = Settings.builder() |
| 169 | + .put(indexSettings()) |
| 170 | + .put("index.analysis.analyzer.synonym.tokenizer", "standard") |
| 171 | + .putList("index.analysis.analyzer.synonym.filter", "synonym", "lowercase") |
| 172 | + .put("index.analysis.filter.synonym.type", "synonym") |
| 173 | + .putList("index.analysis.filter.synonym.synonyms", "fast,quick"); |
| 174 | + |
| 175 | + assertAcked(prepareCreate("test").setSettings(builder.build()) |
| 176 | + .addMapping("type1", "field1", |
| 177 | + "type=text,term_vector=with_positions_offsets,search_analyzer=synonym," + |
| 178 | + "analyzer=standard,index_options=offsets")); |
| 179 | + ensureGreen(); |
| 180 | + |
| 181 | + client().prepareIndex("test", "type1", "0").setSource( |
| 182 | + "field1", "The quick brown fox jumps over the lazy dog").get(); |
| 183 | + refresh(); |
| 184 | + for (String highlighterType : new String[] {"plain", "fvh", "unified"}) { |
| 185 | + logger.info("--> highlighting (type=" + highlighterType + ") and searching on field1"); |
| 186 | + SearchSourceBuilder source = searchSource() |
| 187 | + .query(matchQuery("field1", "quick brown fox").operator(Operator.AND)) |
| 188 | + .highlighter( |
| 189 | + highlight() |
| 190 | + .field("field1") |
| 191 | + .order("score") |
| 192 | + .preTags("<x>") |
| 193 | + .postTags("</x>") |
| 194 | + .highlighterType(highlighterType)); |
| 195 | + SearchResponse searchResponse = client().search(searchRequest("test").source(source)).actionGet(); |
| 196 | + assertHighlight(searchResponse, 0, "field1", 0, 1, |
| 197 | + equalTo("The <x>quick</x> <x>brown</x> <x>fox</x> jumps over the lazy dog")); |
| 198 | + |
| 199 | + source = searchSource() |
| 200 | + .query(matchQuery("field1", "fast brown fox").operator(Operator.AND)) |
| 201 | + .highlighter(highlight().field("field1").order("score").preTags("<x>").postTags("</x>")); |
| 202 | + searchResponse = client().search(searchRequest("test").source(source)).actionGet(); |
| 203 | + assertHighlight(searchResponse, 0, "field1", 0, 1, |
| 204 | + equalTo("The <x>quick</x> <x>brown</x> <x>fox</x> jumps over the lazy dog")); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + public void testPhrasePrefix() throws IOException { |
| 209 | + Settings.Builder builder = Settings.builder() |
| 210 | + .put(indexSettings()) |
| 211 | + .put("index.analysis.analyzer.synonym.tokenizer", "standard") |
| 212 | + .putList("index.analysis.analyzer.synonym.filter", "synonym", "lowercase") |
| 213 | + .put("index.analysis.filter.synonym.type", "synonym") |
| 214 | + .putList("index.analysis.filter.synonym.synonyms", "quick => fast"); |
| 215 | + |
| 216 | + assertAcked(prepareCreate("first_test_index").setSettings(builder.build()).addMapping("type1", type1TermVectorMapping())); |
| 217 | + |
| 218 | + ensureGreen(); |
| 219 | + |
| 220 | + client().prepareIndex("first_test_index", "type1", "0").setSource( |
| 221 | + "field0", "The quick brown fox jumps over the lazy dog", |
| 222 | + "field1", "The quick brown fox jumps over the lazy dog").get(); |
| 223 | + client().prepareIndex("first_test_index", "type1", "1").setSource("field1", |
| 224 | + "The quick browse button is a fancy thing, right bro?").get(); |
| 225 | + refresh(); |
| 226 | + logger.info("--> highlighting and searching on field0"); |
| 227 | + |
| 228 | + SearchSourceBuilder source = searchSource() |
| 229 | + .query(matchPhrasePrefixQuery("field0", "bro")) |
| 230 | + .highlighter(highlight().field("field0").order("score").preTags("<x>").postTags("</x>")); |
| 231 | + SearchResponse searchResponse = client().search(searchRequest("first_test_index").source(source)).actionGet(); |
| 232 | + |
| 233 | + assertHighlight(searchResponse, 0, "field0", 0, 1, equalTo("The quick <x>brown</x> fox jumps over the lazy dog")); |
| 234 | + |
| 235 | + source = searchSource() |
| 236 | + .query(matchPhrasePrefixQuery("field0", "quick bro")) |
| 237 | + .highlighter(highlight().field("field0").order("score").preTags("<x>").postTags("</x>")); |
| 238 | + |
| 239 | + searchResponse = client().search(searchRequest("first_test_index").source(source)).actionGet(); |
| 240 | + assertHighlight(searchResponse, 0, "field0", 0, 1, |
| 241 | + equalTo("The <x>quick</x> <x>brown</x> fox jumps over the lazy dog")); |
| 242 | + |
| 243 | + logger.info("--> highlighting and searching on field1"); |
| 244 | + source = searchSource() |
| 245 | + .query(boolQuery() |
| 246 | + .should(matchPhrasePrefixQuery("field1", "test")) |
| 247 | + .should(matchPhrasePrefixQuery("field1", "bro")) |
| 248 | + ) |
| 249 | + .highlighter(highlight().field("field1").order("score").preTags("<x>").postTags("</x>")); |
| 250 | + |
| 251 | + searchResponse = client().search(searchRequest("first_test_index").source(source)).actionGet(); |
| 252 | + assertThat(searchResponse.getHits().totalHits, equalTo(2L)); |
| 253 | + for (int i = 0; i < 2; i++) { |
| 254 | + assertHighlight(searchResponse, i, "field1", 0, 1, anyOf( |
| 255 | + equalTo("The quick <x>browse</x> button is a fancy thing, right <x>bro</x>?"), |
| 256 | + equalTo("The quick <x>brown</x> fox jumps over the lazy dog"))); |
| 257 | + } |
| 258 | + |
| 259 | + source = searchSource() |
| 260 | + .query(matchPhrasePrefixQuery("field1", "quick bro")) |
| 261 | + .highlighter(highlight().field("field1").order("score").preTags("<x>").postTags("</x>")); |
| 262 | + |
| 263 | + searchResponse = client().search(searchRequest("first_test_index").source(source)).actionGet(); |
| 264 | + |
| 265 | + assertHighlight(searchResponse, 0, "field1", 0, 1, anyOf( |
| 266 | + equalTo("The <x>quick</x> <x>browse</x> button is a fancy thing, right bro?"), |
| 267 | + equalTo("The <x>quick</x> <x>brown</x> fox jumps over the lazy dog"))); |
| 268 | + assertHighlight(searchResponse, 1, "field1", 0, 1, anyOf( |
| 269 | + equalTo("The <x>quick</x> <x>browse</x> button is a fancy thing, right bro?"), |
| 270 | + equalTo("The <x>quick</x> <x>brown</x> fox jumps over the lazy dog"))); |
| 271 | + |
| 272 | + assertAcked(prepareCreate("second_test_index").setSettings(builder.build()).addMapping("doc", |
| 273 | + "field4", "type=text,term_vector=with_positions_offsets,analyzer=synonym", |
| 274 | + "field3", "type=text,analyzer=synonym")); |
| 275 | + // with synonyms |
| 276 | + client().prepareIndex("second_test_index", "doc", "0").setSource( |
| 277 | + "type", "type2", |
| 278 | + "field4", "The quick brown fox jumps over the lazy dog", |
| 279 | + "field3", "The quick brown fox jumps over the lazy dog").get(); |
| 280 | + client().prepareIndex("second_test_index", "doc", "1").setSource( |
| 281 | + "type", "type2", |
| 282 | + "field4", "The quick browse button is a fancy thing, right bro?").get(); |
| 283 | + client().prepareIndex("second_test_index", "doc", "2").setSource( |
| 284 | + "type", "type2", |
| 285 | + "field4", "a quick fast blue car").get(); |
| 286 | + refresh(); |
| 287 | + |
| 288 | + source = searchSource().postFilter(termQuery("type", "type2")).query(matchPhrasePrefixQuery("field3", "fast bro")) |
| 289 | + .highlighter(highlight().field("field3").order("score").preTags("<x>").postTags("</x>")); |
| 290 | + |
| 291 | + searchResponse = client().search(searchRequest("second_test_index").source(source)).actionGet(); |
| 292 | + |
| 293 | + assertHighlight(searchResponse, 0, "field3", 0, 1, |
| 294 | + equalTo("The <x>quick</x> <x>brown</x> fox jumps over the lazy dog")); |
| 295 | + |
| 296 | + logger.info("--> highlighting and searching on field4"); |
| 297 | + source = searchSource().postFilter(termQuery("type", "type2")).query(matchPhrasePrefixQuery("field4", "the fast bro")) |
| 298 | + .highlighter(highlight().field("field4").order("score").preTags("<x>").postTags("</x>")); |
| 299 | + searchResponse = client().search(searchRequest("second_test_index").source(source)).actionGet(); |
| 300 | + |
| 301 | + assertHighlight(searchResponse, 0, "field4", 0, 1, anyOf( |
| 302 | + equalTo("<x>The</x> <x>quick</x> <x>browse</x> button is a fancy thing, right bro?"), |
| 303 | + equalTo("<x>The</x> <x>quick</x> <x>brown</x> fox jumps over the lazy dog"))); |
| 304 | + assertHighlight(searchResponse, 1, "field4", 0, 1, anyOf( |
| 305 | + equalTo("<x>The</x> <x>quick</x> <x>browse</x> button is a fancy thing, right bro?"), |
| 306 | + equalTo("<x>The</x> <x>quick</x> <x>brown</x> fox jumps over the lazy dog"))); |
| 307 | + |
| 308 | + logger.info("--> highlighting and searching on field4"); |
| 309 | + source = searchSource().postFilter(termQuery("type", "type2")) |
| 310 | + .query(matchPhrasePrefixQuery("field4", "a fast quick blue ca")) |
| 311 | + .highlighter(highlight().field("field4").order("score").preTags("<x>").postTags("</x>")); |
| 312 | + searchResponse = client().search(searchRequest("second_test_index").source(source)).actionGet(); |
| 313 | + |
| 314 | + assertHighlight(searchResponse, 0, "field4", 0, 1, |
| 315 | + anyOf(equalTo("<x>a quick fast blue car</x>"), |
| 316 | + equalTo("<x>a</x> <x>quick</x> <x>fast</x> <x>blue</x> <x>car</x>"))); |
| 317 | + } |
| 318 | + |
| 319 | + public static XContentBuilder type1TermVectorMapping() throws IOException { |
| 320 | + return XContentFactory.jsonBuilder().startObject().startObject("type1") |
| 321 | + .startObject("properties") |
| 322 | + .startObject("field1").field("type", "text").field("term_vector", "with_positions_offsets").endObject() |
| 323 | + .startObject("field2").field("type", "text").field("term_vector", "with_positions_offsets").endObject() |
| 324 | + .endObject() |
| 325 | + .endObject().endObject(); |
| 326 | + } |
156 | 327 | } |
0 commit comments