@@ -341,111 +341,20 @@ private function getSearchResponse($results) {
341
341
// Loop through each item in the result set.
342
342
foreach ($ result_items as $ id => &$ item ) {
343
343
// Load the response document into the search response array.
344
- $ search_response ['SearchAPIDocument ' ][] = $ this ->loadResponseDocument ($ item );
344
+ $ document ['item ' ] = $ item ->getFields ();
345
+ $ document ['index_id ' ] = $ this ->index ->id ();;
346
+ $ document ['type ' ] = str_replace ("_ " , "" , ucwords ($ this ->index ->id () . "Doc " , '_ ' ));
347
+
348
+ $ search_response ['SearchAPIDocument ' ][] = $ document ;
345
349
}
346
350
347
351
// Extract facets from the result set.
348
352
$ facets = $ results ->getExtraData ('search_api_facets ' );
349
353
350
354
if ($ facets ) {
351
- // Loop through each facet in the result set.
352
- foreach ($ facets as $ facet_id => $ facet_values ) {
353
- // Load the response facet in the response array.
354
- $ search_response ['facets ' ][] = $ this ->loadResponseFacet ($ facet_id , $ facet_values );
355
- }
355
+ $ search_response ['facets ' ] = $ facets ;
356
356
}
357
357
return $ search_response ;
358
358
}
359
359
360
- /**
361
- * Loads a Facet into a structured response array.
362
- *
363
- * @facet_id
364
- * The id of the facet to be added.
365
- * @facet_values
366
- * The values for the facet to be added.
367
- */
368
- private function loadResponseFacet ($ facet_id , $ facet_values ) {
369
-
370
- // Initialise variables.
371
- $ response_facet = [];
372
-
373
- // Config the facet response.
374
- $ response_facet ['type ' ] = 'SearchAPIFacet ' ;
375
- $ response_facet ['name ' ] = $ facet_id ;
376
-
377
- // Loop through the facet values and load them into the response.
378
- foreach ($ facet_values as $ facet_value ) {
379
- $ response_facet_value = [];
380
- $ response_facet_value ['type ' ] = 'SearchAPIFacetValue ' ;
381
- $ response_facet_value ['filter ' ] = trim ($ facet_value ['filter ' ], '" ' );
382
- $ response_facet_value ['count ' ] = $ facet_value ['count ' ];
383
- $ response_facet ['solrFacetValues ' ][] = $ response_facet_value ;
384
- }
385
-
386
- return $ response_facet ;
387
- }
388
-
389
- /**
390
- * Loads a Document into a structured response array.
391
- *
392
- * @result_item
393
- * The result item that contains the document information.
394
- */
395
- private function loadResponseDocument ($ result_item ) {
396
-
397
- // Initialise a response document.
398
- $ response_document = [];
399
-
400
- // Loop through all fields in the result item.
401
- foreach ($ result_item ->getFields () as $ field_id => $ field ) {
402
-
403
- // Initialise a values.
404
- $ value = NULL ;
405
- $ field_values = $ field ->getValues ();
406
- $ field_type = $ field ->getType ();
407
- $ field_cardinality = count ($ field_values );
408
-
409
- // Fulltext multivalue fields have a different format.
410
- if ($ field_type == 'text ' ) {
411
- // Multivalue fields.
412
- if ($ field_cardinality > 1 ) {
413
- // Create a new array with text values instead of objects.
414
- foreach ($ field_values as $ field_value ) {
415
- $ value [] = $ field_value ->getText ();
416
- }
417
- }
418
- // Singlevalue fields.
419
- elseif (!empty ($ field_values )) {
420
- $ value = $ field_values [0 ]->getText ();
421
- }
422
- }
423
- // For other types of fields we can just grab contents from the array.
424
- else {
425
- // Multivalue fields.
426
- if ($ field_cardinality > 1 ) {
427
- $ value = $ field_values ;
428
- }
429
- // Single value fields.
430
- else {
431
- if (!empty ($ field_values )) {
432
- $ value = $ field_values [0 ];
433
- }
434
- }
435
- }
436
- // Load the value in the response document.
437
- if (!empty ($ value )) {
438
- $ response_document [$ field_id ] = $ value ;
439
- }
440
- }
441
-
442
- // Set the index id in the response document.
443
- $ response_document ['index_id ' ] = $ this ->index ->id ();
444
-
445
- // Append the response to the correct index document.
446
- $ response_document ['type ' ] = str_replace ("_ " , "" , ucwords ($ this ->index ->id () . "Doc " , '_ ' ));
447
-
448
- return $ response_document ;
449
- }
450
-
451
360
}
0 commit comments