4

I need to change the response of Search Rest API in Magento 2.

Request:rest/V1/search?searchCriteria[requestName]=quick_search_container &searchCriteria[filterGroups][0][filters][0][field]=search_term &searchCriteria[filterGroups][0][filters][0][value]=t-shirt &searchCriteria[filterGroups][1][filters][1][field]=sleeve_type &searchCriteria[filterGroups][1][filters][1][value]=173 &searchCriteria[filterGroups][2][filters][2][field]=neck &searchCriteria[filterGroups][2][filters][2][value]=105

Response:

"items": [
    {
        "id": 154,
        "custom_attributes": [
            {
                "attribute_code": "score",
                "value": "45.5335884094238300"
            }
        ]
    }
]        

need to add more product information like product name, sku, type, images etc..,

"items": [

    {
        "id": 154,
        "sku": "sample001",
        "name": "sample product 1",
        "type": "configurable",
        "image_url": "http://magentohost/pub/media/catalog/product/s/s/1.png",
        "custom_attributes": [
            {
                "attribute_code": "score",
                "value": "45.5335884094238300"
            }
        ]
    }
]  

but response have id and score only. How to add product information to search api response?

1
  • user54334 did you get any solution ? Apr 23, 2021 at 11:07

3 Answers 3

1

The beautiful thing about Magento is that you can take reference from the its core code. For answering the question I will took the example of the product API call to filter product from the "color" attribute code.

rest/all/V1/products-render-info? searchCriteria[filterGroups][0][filters][0][field]=color& searchCriteria[filterGroups][0][filters][0][value]=53& searchCriteria[filterGroups][0][filters][0][conditionType]=eq& searchCriteria[sortOrders][0][field]=size& searchCriteria[sortOrders][0][direction]=asc& storeId=1 &currencyCode=usd

From your code I assume that this is custom search criteria. Try to build search criteria in this way and if you are using the Magento code this can be helpful to you.

1

For get the product information in the Search API you have to use searchCriteria with filters in request URL. Use below API Request URL.

Request URL:

http://localhost/magentosample230/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=name&searchCriteria[filter_groups][0][filters][0][value]=%25Watch%25&searchCriteria[filter_groups][0][filters][0][condition_type]=like&searchCriteria[filter_groups][0][filters][1][field]=name&searchCriteria[filter_groups][0][filters][1][value]=%25Bag %25&searchCriteria[filter_groups][0][filters][1][condition_type]=like

Response:

    {
    "items": [
        {
            "id": 36,
            "sku": "24-MG04",
            "name": "Aim Analog Watch",
            "attribute_set_id": 11,
            "price": 45,
            "status": 1,
            "visibility": 4,
            "type_id": "simple",
            "created_at": "2019-08-30 07:14:59",
            "updated_at": "2019-08-30 07:14:59",
            "extension_attributes": {
                "website_ids": [
                    1
                ],
                "category_links": [
                    {
                        "position": 0,
                        "category_id": "3"
                    },
                    {
                        "position": 0,
                        "category_id": "6"
                    }
                ],
                "stock_item": {
                    "item_id": 36,
                    "product_id": 36,
                    "stock_id": 1,
                    "qty": 100,
                    "is_in_stock": true,
                    "is_qty_decimal": false,
                    "show_default_notification_message": false,
                    "use_config_min_qty": true,
                    "min_qty": 0,
                    "use_config_min_sale_qty": 1,
                    "min_sale_qty": 1,
                    "use_config_max_sale_qty": true,
                    "max_sale_qty": 10000,
                    "use_config_backorders": true,
                    "backorders": 0,
                    "use_config_notify_stock_qty": true,
                    "notify_stock_qty": 1,
                    "use_config_qty_increments": true,
                    "qty_increments": 0,
                    "use_config_enable_qty_inc": true,
                    "enable_qty_increments": false,
                    "use_config_manage_stock": true,
                    "manage_stock": true,
                    "low_stock_date": null,
                    "is_decimal_divided": false,
                    "stock_status_changed_auto": 0
                }
            },
            "product_links": [],
            "options": [],
            "media_gallery_entries": [
                {
                    "id": 41,
                    "media_type": "image",
                    "label": "Image",
                    "position": 1,
                    "disabled": false,
                    "types": [
                        "image",
                        "small_image",
                        "thumbnail"
                    ],
                    "file": "/m/g/mg04-bk-0.jpg"
                }
            ],
            "tier_prices": [],
            "custom_attributes": [
                {
                    "attribute_code": "image",
                    "value": "/m/g/mg04-bk-0.jpg"
                },
                {
                    "attribute_code": "small_image",
                    "value": "/m/g/mg04-bk-0.jpg"
                },
                {
                    "attribute_code": "thumbnail",
                    "value": "/m/g/mg04-bk-0.jpg"
                },
                {
                    "attribute_code": "options_container",
                    "value": "container2"
                },
                {
                    "attribute_code": "url_key",
                    "value": "aim-analog-watch"
                },
                {
                    "attribute_code": "required_options",
                    "value": "0"
                },
                {
                    "attribute_code": "has_options",
                    "value": "0"
                },
                {
                    "attribute_code": "tax_class_id",
                    "value": "2"
                },
                {
                    "attribute_code": "category_ids",
                    "value": [
                        "3",
                        "6"
                    ]
                },
                {
                    "attribute_code": "description",
                    "value": "<p>Stay light-years ahead of the competition with our Aim Analog Watch. The flexible, rubberized strap is contoured to conform to the shape of your wrist for a comfortable all-day fit. The face features three illuminated hands, a digital read-out of the current time, and stopwatch functions.</p>\n<ul>\n<li>Japanese quartz movement.</li>\n<li>Strap fits 7\" to 8.0\".</li>\n</ul>"
                },
                {
                    "attribute_code": "activity",
                    "value": "9,17,5,11"
                },
                {
                    "attribute_code": "material",
                    "value": "44,45"
                },
                {
                    "attribute_code": "gender",
                    "value": "80"
                },
                {
                    "attribute_code": "category_gear",
                    "value": "86,87,90"
                }
            ]
        },
        {
            "id": 37,
            "sku": "24-MG01",
            "name": "Endurance Watch",
            "attribute_set_id": 11,
            "price": 49,
            "status": 1,
            "visibility": 4,
            "type_id": "simple",
            "created_at": "2019-08-30 07:14:59",
            "updated_at": "2019-08-30 07:14:59",
            "extension_attributes": {
                "website_ids": [
                    1
                ],
                "category_links": [
                    {
                        "position": 0,
                        "category_id": "3"
                    },
                    {
                        "position": 0,
                        "category_id": "6"
                    }
                ],
                "stock_item": {
                    "item_id": 37,
                    "product_id": 37,
                    "stock_id": 1,
                    "qty": 100,
                    "is_in_stock": true,
                    "is_qty_decimal": false,
                    "show_default_notification_message": false,
                    "use_config_min_qty": true,
                    "min_qty": 0,
                    "use_config_min_sale_qty": 1,
                    "min_sale_qty": 1,
                    "use_config_max_sale_qty": true,
                    "max_sale_qty": 10000,
                    "use_config_backorders": true,
                    "backorders": 0,
                    "use_config_notify_stock_qty": true,
                    "notify_stock_qty": 1,
                    "use_config_qty_increments": true,
                    "qty_increments": 0,
                    "use_config_enable_qty_inc": true,
                    "enable_qty_increments": false,
                    "use_config_manage_stock": true,
                    "manage_stock": true,
                    "low_stock_date": null,
                    "is_decimal_divided": false,
                    "stock_status_changed_auto": 0
                }
            },
            "product_links": [],
            "options": [],
            "media_gallery_entries": [
                {
                    "id": 42,
                    "media_type": "image",
                    "label": "Image",
                    "position": 1,
                    "disabled": false,
                    "types": [
                        "image",
                        "small_image",
                        "thumbnail"
                    ],
                    "file": "/m/g/mg01-bk-0.jpg"
                }
            ],
            "tier_prices": [],
            "custom_attributes": [
                {
                    "attribute_code": "image",
                    "value": "/m/g/mg01-bk-0.jpg"
                },
                {
                    "attribute_code": "small_image",
                    "value": "/m/g/mg01-bk-0.jpg"
                },
                {
                    "attribute_code": "thumbnail",
                    "value": "/m/g/mg01-bk-0.jpg"
                },
                {
                    "attribute_code": "options_container",
                    "value": "container2"
                },
                {
                    "attribute_code": "url_key",
                    "value": "endurance-watch"
                },
                {
                    "attribute_code": "required_options",
                    "value": "0"
                },
                {
                    "attribute_code": "has_options",
                    "value": "0"
                },
                {
                    "attribute_code": "tax_class_id",
                    "value": "2"
                },
                {
                    "attribute_code": "category_ids",
                    "value": [
                        "3",
                        "6"
                    ]
                },
                {
                    "attribute_code": "description",
                    "value": "<p>It's easy to track and monitor your training progress with the Endurance Watch. You'll see standard info like time, date and day of the week, but it also functions for the serious high-mileage athete: lap counter, stopwatch, distance, heart rate, speed/pace, cadence and altitude.</p>\n<ul>\n<li>Digital display.</li>\n<li>LED backlight.</li>\n<li>Strap fits 7\" to 10\".</li>\n<li>1-year limited warranty.</li>\n<li>Comes with polished metal case.</li>\n</ul>"
                },
                {
                    "attribute_code": "activity",
                    "value": "9,16"
                },
                {
                    "attribute_code": "material",
                    "value": "43,45,48"
                },
                {
                    "attribute_code": "gender",
                    "value": "80"
                },
                {
                    "attribute_code": "category_gear",
                    "value": "86,87,90"
                }
            ]
        },
        {
            "id": 38,
            "sku": "24-MG03",
            "name": "Summit Watch",
            "attribute_set_id": 11,
            "price": 54,
            "status": 1,
            "visibility": 4,
            "type_id": "simple",
            "created_at": "2019-08-30 07:15:00",
            "updated_at": "2019-08-30 07:15:00",
            "extension_attributes": {
                "website_ids": [
                    1
                ],
                "category_links": [
                    {
                        "position": 0,
                        "category_id": "3"
                    },
                    {
                        "position": 0,
                        "category_id": "7"
                    },
                    {
                        "position": 0,
                        "category_id": "6"
                    },
                    {
                        "position": 0,
                        "category_id": "8"
                    }
                ],
                "stock_item": {
                    "item_id": 38,
                    "product_id": 38,
                    "stock_id": 1,
                    "qty": 100,
                    "is_in_stock": true,
                    "is_qty_decimal": false,
                    "show_default_notification_message": false,
                    "use_config_min_qty": true,
                    "min_qty": 0,
                    "use_config_min_sale_qty": 1,
                    "min_sale_qty": 1,
                    "use_config_max_sale_qty": true,
                    "max_sale_qty": 10000,
                    "use_config_backorders": true,
                    "backorders": 0,
                    "use_config_notify_stock_qty": true,
                    "notify_stock_qty": 1,
                    "use_config_qty_increments": true,
                    "qty_increments": 0,
                    "use_config_enable_qty_inc": true,
                    "enable_qty_increments": false,
                    "use_config_manage_stock": true,
                    "manage_stock": true,
                    "low_stock_date": null,
                    "is_decimal_divided": false,
                    "stock_status_changed_auto": 0
                }
            },
            "product_links": [],
            "options": [],
            "media_gallery_entries": [
                {
                    "id": 43,
                    "media_type": "image",
                    "label": "Image",
                    "position": 1,
                    "disabled": false,
                    "types": [
                        "image",
                        "small_image",
                        "thumbnail"
                    ],
                    "file": "/m/g/mg03-br-0.jpg"
                }
            ],
            "tier_prices": [],
            "custom_attributes": [
                {
                    "attribute_code": "image",
                    "value": "/m/g/mg03-br-0.jpg"
                },
                {
                    "attribute_code": "small_image",
                    "value": "/m/g/mg03-br-0.jpg"
                },
                {
                    "attribute_code": "thumbnail",
                    "value": "/m/g/mg03-br-0.jpg"
                },
                {
                    "attribute_code": "options_container",
                    "value": "container2"
                },
                {
                    "attribute_code": "url_key",
                    "value": "summit-watch"
                },
                {
                    "attribute_code": "required_options",
                    "value": "0"
                },
                {
                    "attribute_code": "has_options",
                    "value": "0"
                },
                {
                    "attribute_code": "tax_class_id",
                    "value": "2"
                },
                {
                    "attribute_code": "category_ids",
                    "value": [
                        "3",
                        "7",
                        "6",
                        "8"
                    ]
                },
                {
                    "attribute_code": "description",
                    "value": "<p>Trek high and low in the attractive Summit Watch, which features a digital LED display with time and date, stopwatch, lap counter, and 3-second backlight. It can also calculate the number of steps taken and calories burned.</p>\n<ul>\n<li>Brushed metal case.</li>\n<li>Water resistant (100 meters).</li>\n<li>Buckle clasp.</li>\n<li>Strap fits 7\" - 10\".</li>\n<li>1-year limited warranty.</li>\n</ul>"
                },
                {
                    "attribute_code": "activity",
                    "value": "9,16,17,5,11"
                },
                {
                    "attribute_code": "material",
                    "value": "43,44,48"
                },
                {
                    "attribute_code": "gender",
                    "value": "80,81,84"
                },
                {
                    "attribute_code": "category_gear",
                    "value": "86,87,90"
                },
                {
                    "attribute_code": "new",
                    "value": "1"
                }
            ]
        },
        {
            "id": 39,
            "sku": "24-MG05",
            "name": "Cruise Dual Analog Watch",
            "attribute_set_id": 11,
            "price": 55,
            "status": 1,
            "visibility": 4,
            "type_id": "simple",
            "created_at": "2019-08-30 07:15:00",
            "updated_at": "2019-08-30 07:15:00",
            "extension_attributes": {
                "website_ids": [
                    1
                ],
                "category_links": [
                    {
                        "position": 0,
                        "category_id": "3"
                    },
                    {
                        "position": 0,
                        "category_id": "7"
                    },
                    {
                        "position": 0,
                        "category_id": "6"
                    },
                    {
                        "position": 0,
                        "category_id": "8"
                    }
                ],
                "stock_item": {
                    "item_id": 39,
                    "product_id": 39,
                    "stock_id": 1,
                    "qty": 100,
                    "is_in_stock": true,
                    "is_qty_decimal": false,
                    "show_default_notification_message": false,
                    "use_config_min_qty": true,
                    "min_qty": 0,
                    "use_config_min_sale_qty": 1,
                    "min_sale_qty": 1,
                    "use_config_max_sale_qty": true,
                    "max_sale_qty": 10000,
                    "use_config_backorders": true,
                    "backorders": 0,
                    "use_config_notify_stock_qty": true,
                    "notify_stock_qty": 1,
                    "use_config_qty_increments": true,
                    "qty_increments": 0,
                    "use_config_enable_qty_inc": true,
                    "enable_qty_increments": false,
                    "use_config_manage_stock": true,
                    "manage_stock": true,
                    "low_stock_date": null,
                    "is_decimal_divided": false,
                    "stock_status_changed_auto": 0
                }
            },
            "product_links": [],
            "options": [],
            "media_gallery_entries": [
                {
                    "id": 44,
                    "media_type": "image",
                    "label": "Image",
                    "position": 1,
                    "disabled": false,
                    "types": [
                        "image",
                        "small_image",
                        "thumbnail"
                    ],
                    "file": "/m/g/mg05-br-0.jpg"
                }
            ],
            "tier_prices": [],
            "custom_attributes": [
                {
                    "attribute_code": "image",
                    "value": "/m/g/mg05-br-0.jpg"
                },
                {
                    "attribute_code": "small_image",
                    "value": "/m/g/mg05-br-0.jpg"
                },
                {
                    "attribute_code": "thumbnail",
                    "value": "/m/g/mg05-br-0.jpg"
                },
                {
                    "attribute_code": "options_container",
                    "value": "container2"
                },
                {
                    "attribute_code": "url_key",
                    "value": "cruise-dual-analog-watch"
                },
                {
                    "attribute_code": "required_options",
                    "value": "0"
                },
                {
                    "attribute_code": "has_options",
                    "value": "0"
                },
                {
                    "attribute_code": "tax_class_id",
                    "value": "2"
                },
                {
                    "attribute_code": "category_ids",
                    "value": [
                        "3",
                        "7",
                        "6",
                        "8"
                    ]
                },
                {
                    "attribute_code": "description",
                    "value": "<p>Whether you're traveling or wish you were, you'll never let time zones perplex you again with the Cruise Dual Analog Watch. The thick, adjustable band promises a comfortable, personalized fit to this classy, modern time piece.</p>\n<ul>\n<li>Two dials.</li>\n<li>Stainless steel case.</li>\n<li>Adjustable leather band.</li>\n</ul>"
                },
                {
                    "attribute_code": "activity",
                    "value": "9"
                },
                {
                    "attribute_code": "material",
                    "value": "35,44"
                },
                {
                    "attribute_code": "gender",
                    "value": "80"
                },
                {
                    "attribute_code": "category_gear",
                    "value": "86,88,90"
                },
                {
                    "attribute_code": "new",
                    "value": "1"
                }
            ]
        }
    ],
    "search_criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "name",
                        "value": "%Watch%",
                        "condition_type": "like"
                    }
                ]
            }
        ]
    },
    "total_count": 4
}
0

According to this question Magento2 Rest Api Search Criteria not working properly! you can add a "fields" parameter...

Not used it, but from the answer I guess it works...

EDIT: Actually, from what I can tell, "fields" is used for restricting the data returned, not adding more data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy