27
27
import org .apache .solr .common .SolrDocument ;
28
28
import org .apache .solr .common .SolrDocumentList ;
29
29
import org .apache .solr .common .SolrException ;
30
+ import org .apache .solr .common .util .NamedList ;
30
31
import org .junit .After ;
31
32
import org .junit .Before ;
32
33
import org .junit .Test ;
@@ -50,6 +51,8 @@ static void indexDocs()
50
51
String id = "id" ;
51
52
String FIELD1 = "lowerfilt_u" ;
52
53
String FIELD2 = "lowerfilt1_u" ;
54
+ String FIELD3 = "copyfield_source" ;
55
+ String FIELD4 = "copyfield_source_2" ;
53
56
54
57
new UpdateRequest ()
55
58
.add (sdoc (id , "1" , FIELD1 , "toyota" ))
@@ -119,6 +122,9 @@ static void indexDocs()
119
122
"The slim red fox jumped over the lazy brown dogs." ,
120
123
FIELD2 ,
121
124
"yellow white black" ))
125
+ .add (sdoc (id , "33" , FIELD3 , "hard rock" , FIELD4 , "instrumental version" ))
126
+ .add (sdoc (id , "34" , FIELD3 , "hard rock" , FIELD4 , "instrumental version" ))
127
+ .add (sdoc (id , "35" , FIELD3 , "pop rock" ))
122
128
.commit (client , COLLECTION );
123
129
}
124
130
@@ -339,4 +345,106 @@ public void testInvalidSourceDocument() {
339
345
.getSolrClient ()
340
346
.query (COLLECTION , new SolrQuery ("{!mlt qf=lowerfilt_u}999999" )));
341
347
}
348
+
349
+ @ Test
350
+ public void testUsesACopyFieldInQf_shouldReturnExpectResults () throws Exception {
351
+ // Verifies that when a copyField destination is used in the qf parameter, the field values are
352
+ // correctly retrieved from the source field(s) and the MLT query returns the expected results.
353
+ QueryResponse queryResponse =
354
+ cluster
355
+ .getSolrClient ()
356
+ .query (COLLECTION , new SolrQuery ("{!mlt qf=copyfield_dest mindf=0 mintf=1}33" ));
357
+ SolrDocumentList solrDocuments = queryResponse .getResults ();
358
+ int [] expectedIds = new int [] {34 , 35 };
359
+ int [] actualIds = new int [solrDocuments .size ()];
360
+ int i = 0 ;
361
+ for (SolrDocument solrDocument : solrDocuments ) {
362
+ actualIds [i ++] = Integer .parseInt (String .valueOf (solrDocument .getFieldValue ("id" )));
363
+ }
364
+
365
+ Arrays .sort (actualIds );
366
+ Arrays .sort (expectedIds );
367
+ assertArrayEquals (expectedIds , actualIds );
368
+ }
369
+
370
+ @ Test
371
+ public void testUsesACopyFieldInQf_shouldGenerateNonEmptyQuery () throws Exception {
372
+ // Verifies that the MLT query correctly uses the content of the source field(s) when a
373
+ // copyField destination is specified in the qf parameter.
374
+ QueryResponse queryResponse =
375
+ cluster
376
+ .getSolrClient ()
377
+ .query (
378
+ COLLECTION ,
379
+ new SolrQuery ("{!mlt qf=copyfield_dest mindf=0 mintf=1}33" ).setShowDebugInfo (true ));
380
+
381
+ NamedList <?> debugInfo = (NamedList <?>) queryResponse .getResponse ().get ("debug" );
382
+ // Extract the parsed query string
383
+ String parsedQuery = (String ) debugInfo .get ("parsedquery_toString" );
384
+ // Assert it matches the expected query string
385
+ assertEquals ("+(copyfield_dest:rock copyfield_dest:hard) -id:33" , parsedQuery );
386
+ // Assert it is not the incorrect fallback
387
+ assertNotEquals ("+() -id:33" , parsedQuery );
388
+ }
389
+
390
+ @ Test
391
+ public void testCopyFieldSourceMissing_shouldReturnNoResults () throws Exception {
392
+ // Ensures that no results are returned when the copyField source field is missing in the source
393
+ // document.
394
+ QueryResponse queryResponse =
395
+ cluster
396
+ .getSolrClient ()
397
+ .query (COLLECTION , new SolrQuery ("{!mlt qf=copyfield_dest mindf=0 mintf=1}30" ));
398
+ SolrDocumentList solrDocuments = queryResponse .getResults ();
399
+ assertEquals ("Expected no results if source field is missing" , 0 , solrDocuments .size ());
400
+ }
401
+
402
+ @ Test
403
+ public void testCopyFieldDestinMultipleSources_shouldReturnExpectResults () throws Exception {
404
+ // Validates that when multiple source fields map to a single copyField destination, their
405
+ // values are correctly combined and expected results are returned.
406
+ QueryResponse queryResponse =
407
+ cluster
408
+ .getSolrClient ()
409
+ .query (
410
+ COLLECTION ,
411
+ new SolrQuery ("{!mlt qf=copyfield_dest_multiple_sources mindf=0 mintf=1}33" ));
412
+ SolrDocumentList solrDocuments = queryResponse .getResults ();
413
+ int [] expectedIds = new int [] {34 , 35 };
414
+ int [] actualIds = new int [solrDocuments .size ()];
415
+ int i = 0 ;
416
+ for (SolrDocument solrDocument : solrDocuments ) {
417
+ actualIds [i ++] = Integer .parseInt (String .valueOf (solrDocument .getFieldValue ("id" )));
418
+ }
419
+
420
+ Arrays .sort (actualIds );
421
+ Arrays .sort (expectedIds );
422
+ assertArrayEquals (expectedIds , actualIds );
423
+ }
424
+
425
+ @ Test
426
+ public void
427
+ testCopyFieldDestinationMultipleSources_shouldGenerateQueryUsingMultipleSourcesValues ()
428
+ throws Exception {
429
+ // Validates that when multiple source fields map to a single copyField destination, their
430
+ // values are
431
+ // correctly combined and the resulting MLT query is properly constructed.
432
+ QueryResponse queryResponse =
433
+ cluster
434
+ .getSolrClient ()
435
+ .query (
436
+ COLLECTION ,
437
+ new SolrQuery ("{!mlt qf=copyfield_dest_multiple_sources mindf=0 mintf=1}33" )
438
+ .setShowDebugInfo (true ));
439
+
440
+ NamedList <?> debugInfo = (NamedList <?>) queryResponse .getResponse ().get ("debug" );
441
+ // Extract the parsed query string
442
+ String parsedQuery = (String ) debugInfo .get ("parsedquery_toString" );
443
+ // Assert it matches the expected query string
444
+ assertEquals (
445
+ "+(copyfield_dest_multiple_sources:rock copyfield_dest_multiple_sources:version copyfield_dest_multiple_sources:hard copyfield_dest_multiple_sources:instrumental) -id:33" ,
446
+ parsedQuery );
447
+ // Assert it is not the incorrect fallback
448
+ assertNotEquals ("+() -id:33" , parsedQuery );
449
+ }
342
450
}
0 commit comments