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
@@ -340,4 +346,106 @@ public void testInvalidSourceDocument() {
340
346
.getSolrClient ()
341
347
.query (COLLECTION , new SolrQuery ("{!mlt qf=lowerfilt_u}999999" )));
342
348
}
349
+
350
+ @ Test
351
+ public void testUsesACopyFieldInQf_shouldReturnExpectResults () throws Exception {
352
+ // Verifies that when a copyField destination is used in the qf parameter, the field values are
353
+ // correctly retrieved from the source field(s) and the MLT query returns the expected results.
354
+ QueryResponse queryResponse =
355
+ cluster
356
+ .getSolrClient ()
357
+ .query (COLLECTION , new SolrQuery ("{!mlt qf=copyfield_dest mindf=0 mintf=1}33" ));
358
+ SolrDocumentList solrDocuments = queryResponse .getResults ();
359
+ int [] expectedIds = new int [] {34 , 35 };
360
+ int [] actualIds = new int [solrDocuments .size ()];
361
+ int i = 0 ;
362
+ for (SolrDocument solrDocument : solrDocuments ) {
363
+ actualIds [i ++] = Integer .parseInt (String .valueOf (solrDocument .getFieldValue ("id" )));
364
+ }
365
+
366
+ Arrays .sort (actualIds );
367
+ Arrays .sort (expectedIds );
368
+ assertArrayEquals (expectedIds , actualIds );
369
+ }
370
+
371
+ @ Test
372
+ public void testUsesACopyFieldInQf_shouldGenerateNonEmptyQuery () throws Exception {
373
+ // Verifies that the MLT query correctly uses the content of the source field(s) when a
374
+ // copyField destination is specified in the qf parameter.
375
+ QueryResponse queryResponse =
376
+ cluster
377
+ .getSolrClient ()
378
+ .query (
379
+ COLLECTION ,
380
+ new SolrQuery ("{!mlt qf=copyfield_dest mindf=0 mintf=1}33" ).setShowDebugInfo (true ));
381
+
382
+ NamedList <?> debugInfo = (NamedList <?>) queryResponse .getResponse ().get ("debug" );
383
+ // Extract the parsed query string
384
+ String parsedQuery = (String ) debugInfo .get ("parsedquery_toString" );
385
+ // Assert it matches the expected query string
386
+ assertEquals ("+(copyfield_dest:rock copyfield_dest:hard) -id:33" , parsedQuery );
387
+ // Assert it is not the incorrect fallback
388
+ assertNotEquals ("+() -id:33" , parsedQuery );
389
+ }
390
+
391
+ @ Test
392
+ public void testCopyFieldSourceMissing_shouldReturnNoResults () throws Exception {
393
+ // Ensures that no results are returned when the copyField source field is missing in the source
394
+ // document.
395
+ QueryResponse queryResponse =
396
+ cluster
397
+ .getSolrClient ()
398
+ .query (COLLECTION , new SolrQuery ("{!mlt qf=copyfield_dest mindf=0 mintf=1}30" ));
399
+ SolrDocumentList solrDocuments = queryResponse .getResults ();
400
+ assertEquals ("Expected no results if source field is missing" , 0 , solrDocuments .size ());
401
+ }
402
+
403
+ @ Test
404
+ public void testCopyFieldDestinMultipleSources_shouldReturnExpectResults () throws Exception {
405
+ // Validates that when multiple source fields map to a single copyField destination, their
406
+ // values are correctly combined and expected results are returned.
407
+ QueryResponse queryResponse =
408
+ cluster
409
+ .getSolrClient ()
410
+ .query (
411
+ COLLECTION ,
412
+ new SolrQuery ("{!mlt qf=copyfield_dest_multiple_sources mindf=0 mintf=1}33" ));
413
+ SolrDocumentList solrDocuments = queryResponse .getResults ();
414
+ int [] expectedIds = new int [] {34 , 35 };
415
+ int [] actualIds = new int [solrDocuments .size ()];
416
+ int i = 0 ;
417
+ for (SolrDocument solrDocument : solrDocuments ) {
418
+ actualIds [i ++] = Integer .parseInt (String .valueOf (solrDocument .getFieldValue ("id" )));
419
+ }
420
+
421
+ Arrays .sort (actualIds );
422
+ Arrays .sort (expectedIds );
423
+ assertArrayEquals (expectedIds , actualIds );
424
+ }
425
+
426
+ @ Test
427
+ public void
428
+ testCopyFieldDestinationMultipleSources_shouldGenerateQueryUsingMultipleSourcesValues ()
429
+ throws Exception {
430
+ // Validates that when multiple source fields map to a single copyField destination, their
431
+ // values are
432
+ // correctly combined and the resulting MLT query is properly constructed.
433
+ QueryResponse queryResponse =
434
+ cluster
435
+ .getSolrClient ()
436
+ .query (
437
+ COLLECTION ,
438
+ new SolrQuery ("{!mlt qf=copyfield_dest_multiple_sources mindf=0 mintf=1}33" )
439
+ .setShowDebugInfo (true ));
440
+
441
+ NamedList <?> debugInfo = (NamedList <?>) queryResponse .getResponse ().get ("debug" );
442
+ // Extract the parsed query string
443
+ String parsedQuery = (String ) debugInfo .get ("parsedquery_toString" );
444
+ // Assert it matches the expected query string
445
+ assertEquals (
446
+ "+(copyfield_dest_multiple_sources:rock copyfield_dest_multiple_sources:version copyfield_dest_multiple_sources:hard copyfield_dest_multiple_sources:instrumental) -id:33" ,
447
+ parsedQuery );
448
+ // Assert it is not the incorrect fallback
449
+ assertNotEquals ("+() -id:33" , parsedQuery );
450
+ }
343
451
}
0 commit comments