|
19 | 19 | import static org.hamcrest.Matchers.hasItem; |
20 | 20 | import static org.hamcrest.Matchers.hasSize; |
21 | 21 | import static org.hamcrest.Matchers.is; |
| 22 | +import static org.hamcrest.Matchers.notNullValue; |
22 | 23 | import static org.hamcrest.Matchers.nullValue; |
23 | 24 | import static org.mockito.ArgumentMatchers.eq; |
24 | 25 | import static org.mockito.Mockito.RETURNS_DEEP_STUBS; |
|
54 | 55 |
|
55 | 56 | public class DynamicSchemaResolverTest { |
56 | 57 |
|
57 | | - private static final int INITIAL_FIELDS_CACHE_COUNT = 5; |
| 58 | + private static final int INITIAL_FIELDS_CACHE_COUNT = 3; |
58 | 59 |
|
59 | 60 | private static final ObjectMapper METACARD_TYPE_MAPPER = |
60 | 61 | MetacardTypeMapperFactory.newObjectMapper(); |
@@ -180,7 +181,7 @@ public void testAnyTextFieldPropertyParsing() throws Exception { |
180 | 181 | // Perform Test |
181 | 182 | List<String> fields = resolver.anyTextFields().collect(Collectors.toList()); |
182 | 183 | Truth.assertThat(fields) |
183 | | - .containsExactly("metadata_txt", "title_txt", "description_txt", "ext.extracted.text_txt"); |
| 184 | + .containsExactly("metadata", "title", "description", "ext.extracted.text"); |
184 | 185 | } |
185 | 186 |
|
186 | 187 | @Test |
@@ -301,4 +302,202 @@ public void getFieldExactValue() { |
301 | 302 | dynamicSchemaResolver.getField("unknown", AttributeFormat.STRING, true, enabledFeatures), |
302 | 303 | is("unknown_txt")); |
303 | 304 | } |
| 305 | + |
| 306 | + @Test |
| 307 | + public void testAddFieldsWithXmlAttributeNotTokenized() throws Exception { |
| 308 | + // Setup - XML attribute that is NOT tokenized |
| 309 | + String metacardTypeName = "test"; |
| 310 | + Set<AttributeDescriptor> attributeDescriptors = new HashSet<>(1); |
| 311 | + String attributeName = "metadata"; |
| 312 | + boolean indexed = true; |
| 313 | + boolean stored = true; |
| 314 | + boolean tokenized = false; |
| 315 | + boolean multiValued = false; |
| 316 | + |
| 317 | + attributeDescriptors.add( |
| 318 | + new TestAttributeDescriptorImpl( |
| 319 | + attributeName, |
| 320 | + attributeName, |
| 321 | + indexed, |
| 322 | + stored, |
| 323 | + tokenized, |
| 324 | + multiValued, |
| 325 | + BasicTypes.XML_TYPE)); |
| 326 | + |
| 327 | + Attribute mockAttribute = mock(Attribute.class); |
| 328 | + when(mockAttribute.getValue()).thenReturn("<root>test</root>"); |
| 329 | + when(mockAttribute.getValues()).thenReturn(Collections.singletonList("<root>test</root>")); |
| 330 | + |
| 331 | + Metacard mockMetacard = mock(Metacard.class, RETURNS_DEEP_STUBS); |
| 332 | + when(mockMetacard.getMetacardType().getName()).thenReturn(metacardTypeName); |
| 333 | + when(mockMetacard.getMetacardType().getAttributeDescriptors()).thenReturn(attributeDescriptors); |
| 334 | + when(mockMetacard.getAttribute(attributeName)).thenReturn(mockAttribute); |
| 335 | + |
| 336 | + SolrInputDocument solrInputDocument = new SolrInputDocument(); |
| 337 | + DynamicSchemaResolver resolver = new DynamicSchemaResolver(); |
| 338 | + |
| 339 | + // Perform Test |
| 340 | + resolver.addFields(mockMetacard, solrInputDocument); |
| 341 | + |
| 342 | + // Verify - tokenized=false means the special string index should NOT be populated |
| 343 | + assertThat(solrInputDocument.getFieldValue("metadata_txt_tokenized"), is(nullValue())); |
| 344 | + } |
| 345 | + |
| 346 | + @Test |
| 347 | + public void testAddFieldsWithXmlAttributeTokenized() throws Exception { |
| 348 | + // Setup - XML attribute that IS tokenized |
| 349 | + String metacardTypeName = "test"; |
| 350 | + Set<AttributeDescriptor> attributeDescriptors = new HashSet<>(1); |
| 351 | + String attributeName = "metadata"; |
| 352 | + boolean indexed = true; |
| 353 | + boolean stored = true; |
| 354 | + boolean tokenized = true; |
| 355 | + boolean multiValued = false; |
| 356 | + |
| 357 | + attributeDescriptors.add( |
| 358 | + new TestAttributeDescriptorImpl( |
| 359 | + attributeName, |
| 360 | + attributeName, |
| 361 | + indexed, |
| 362 | + stored, |
| 363 | + tokenized, |
| 364 | + multiValued, |
| 365 | + BasicTypes.XML_TYPE)); |
| 366 | + |
| 367 | + Attribute mockAttribute = mock(Attribute.class); |
| 368 | + when(mockAttribute.getValue()).thenReturn("<root>test</root>"); |
| 369 | + when(mockAttribute.getValues()).thenReturn(Collections.singletonList("<root>test</root>")); |
| 370 | + |
| 371 | + Metacard mockMetacard = mock(Metacard.class, RETURNS_DEEP_STUBS); |
| 372 | + when(mockMetacard.getMetacardType().getName()).thenReturn(metacardTypeName); |
| 373 | + when(mockMetacard.getMetacardType().getAttributeDescriptors()).thenReturn(attributeDescriptors); |
| 374 | + when(mockMetacard.getAttribute(attributeName)).thenReturn(mockAttribute); |
| 375 | + |
| 376 | + SolrInputDocument solrInputDocument = new SolrInputDocument(); |
| 377 | + DynamicSchemaResolver resolver = new DynamicSchemaResolver(); |
| 378 | + |
| 379 | + // Perform Test |
| 380 | + resolver.addFields(mockMetacard, solrInputDocument); |
| 381 | + |
| 382 | + // Verify - tokenized=true means the special string index SHOULD be populated |
| 383 | + assertThat(solrInputDocument.getFieldValue("metadata_txt_tokenized"), notNullValue()); |
| 384 | + } |
| 385 | + |
| 386 | + @Test |
| 387 | + public void testAddFieldsWithStringAttributeNotTokenizedAddsTxtNotTokenizedField() |
| 388 | + throws Exception { |
| 389 | + // Setup - STRING attribute that is NOT tokenized |
| 390 | + String metacardTypeName = "test"; |
| 391 | + Set<AttributeDescriptor> attributeDescriptors = new HashSet<>(1); |
| 392 | + String attributeName = "title"; |
| 393 | + boolean indexed = true; |
| 394 | + boolean stored = true; |
| 395 | + boolean tokenized = false; |
| 396 | + boolean multiValued = false; |
| 397 | + |
| 398 | + attributeDescriptors.add( |
| 399 | + new TestAttributeDescriptorImpl( |
| 400 | + attributeName, |
| 401 | + attributeName, |
| 402 | + indexed, |
| 403 | + stored, |
| 404 | + tokenized, |
| 405 | + multiValued, |
| 406 | + BasicTypes.STRING_TYPE)); |
| 407 | + |
| 408 | + Attribute mockAttribute = mock(Attribute.class); |
| 409 | + when(mockAttribute.getValue()).thenReturn("test value"); |
| 410 | + when(mockAttribute.getValues()).thenReturn(Collections.singletonList("test value")); |
| 411 | + |
| 412 | + Metacard mockMetacard = mock(Metacard.class, RETURNS_DEEP_STUBS); |
| 413 | + when(mockMetacard.getMetacardType().getName()).thenReturn(metacardTypeName); |
| 414 | + when(mockMetacard.getMetacardType().getAttributeDescriptors()).thenReturn(attributeDescriptors); |
| 415 | + when(mockMetacard.getAttribute(attributeName)).thenReturn(mockAttribute); |
| 416 | + |
| 417 | + SolrInputDocument solrInputDocument = new SolrInputDocument(); |
| 418 | + DynamicSchemaResolver resolver = new DynamicSchemaResolver(); |
| 419 | + |
| 420 | + // Perform Test |
| 421 | + resolver.addFields(mockMetacard, solrInputDocument); |
| 422 | + |
| 423 | + // Verify - tokenized=false means the tokenized field should NOT be populated |
| 424 | + assertThat(solrInputDocument.getFieldValue("title_txt"), is("test value")); |
| 425 | + assertThat(solrInputDocument.getFieldValue("title_txt_tokenized"), is(nullValue())); |
| 426 | + } |
| 427 | + |
| 428 | + @Test |
| 429 | + public void testAddFieldsWithStringAttributeTokenizedAddsBothTxtAndTokenizedField() |
| 430 | + throws Exception { |
| 431 | + // Setup - STRING attribute that IS tokenized |
| 432 | + String metacardTypeName = "test"; |
| 433 | + Set<AttributeDescriptor> attributeDescriptors = new HashSet<>(1); |
| 434 | + String attributeName = "title"; |
| 435 | + boolean indexed = true; |
| 436 | + boolean stored = true; |
| 437 | + boolean tokenized = true; |
| 438 | + boolean multiValued = false; |
| 439 | + |
| 440 | + attributeDescriptors.add( |
| 441 | + new TestAttributeDescriptorImpl( |
| 442 | + attributeName, |
| 443 | + attributeName, |
| 444 | + indexed, |
| 445 | + stored, |
| 446 | + tokenized, |
| 447 | + multiValued, |
| 448 | + BasicTypes.STRING_TYPE)); |
| 449 | + |
| 450 | + Attribute mockAttribute = mock(Attribute.class); |
| 451 | + when(mockAttribute.getValue()).thenReturn("test value"); |
| 452 | + when(mockAttribute.getValues()).thenReturn(Collections.singletonList("test value")); |
| 453 | + |
| 454 | + Metacard mockMetacard = mock(Metacard.class, RETURNS_DEEP_STUBS); |
| 455 | + when(mockMetacard.getMetacardType().getName()).thenReturn(metacardTypeName); |
| 456 | + when(mockMetacard.getMetacardType().getAttributeDescriptors()).thenReturn(attributeDescriptors); |
| 457 | + when(mockMetacard.getAttribute(attributeName)).thenReturn(mockAttribute); |
| 458 | + |
| 459 | + SolrInputDocument solrInputDocument = new SolrInputDocument(); |
| 460 | + DynamicSchemaResolver resolver = new DynamicSchemaResolver(); |
| 461 | + |
| 462 | + // Perform Test |
| 463 | + resolver.addFields(mockMetacard, solrInputDocument); |
| 464 | + |
| 465 | + // Verify - tokenized=true means both fields SHOULD be populated |
| 466 | + assertThat(solrInputDocument.getFieldValue("title_txt"), is("test value")); |
| 467 | + assertThat(solrInputDocument.getFieldValue("title_txt_tokenized"), is("test value")); |
| 468 | + } |
| 469 | + |
| 470 | + @Test |
| 471 | + public void testGetFieldNumericalFallsBackToRequestedSuffix() { |
| 472 | + // When no numerical field exists in cache, should fall back to the requested suffix |
| 473 | + assertThat( |
| 474 | + dynamicSchemaResolver.getField( |
| 475 | + "unknown", AttributeFormat.INTEGER, false, Collections.emptyMap()), |
| 476 | + is("unknown_int")); |
| 477 | + assertThat( |
| 478 | + dynamicSchemaResolver.getField( |
| 479 | + "unknown", AttributeFormat.LONG, false, Collections.emptyMap()), |
| 480 | + is("unknown_lng")); |
| 481 | + assertThat( |
| 482 | + dynamicSchemaResolver.getField( |
| 483 | + "unknown", AttributeFormat.DOUBLE, false, Collections.emptyMap()), |
| 484 | + is("unknown_dbl")); |
| 485 | + assertThat( |
| 486 | + dynamicSchemaResolver.getField( |
| 487 | + "unknown", AttributeFormat.FLOAT, false, Collections.emptyMap()), |
| 488 | + is("unknown_flt")); |
| 489 | + assertThat( |
| 490 | + dynamicSchemaResolver.getField( |
| 491 | + "unknown", AttributeFormat.SHORT, false, Collections.emptyMap()), |
| 492 | + is("unknown_shr")); |
| 493 | + } |
| 494 | + |
| 495 | + @Test |
| 496 | + public void testGetFieldXmlReturnsTextPathSuffix() { |
| 497 | + // XML format should use TEXT_PATH (_tpt) suffix |
| 498 | + assertThat( |
| 499 | + dynamicSchemaResolver.getField( |
| 500 | + "metadata", AttributeFormat.XML, false, Collections.emptyMap()), |
| 501 | + is("metadata_xml_tpt")); |
| 502 | + } |
304 | 503 | } |
0 commit comments