To add and configure the search by category functionality in Magento 2, you can follow these steps:
- Create a new category attribute: Go to Stores > Attributes > Product and create a new attribute for category. Make sure to set the attribute to “Visible on Catalog Pages on Frontend” and “Filterable with results” to Yes.
- Assign the attribute to the attribute set: Go to Stores > Attributes > Attribute Set, select the attribute set that you want to assign the attribute to, and drag and drop the new category attribute to the appropriate group.
- Assign categories to your products: Go to Catalog > Products and edit the products that you want to assign categories to. In the Categories section, select the appropriate category or categories.
- Configure the search settings: Go to Stores > Configuration > Catalog > Catalog Search and expand the “Search Engine Optimization” section. Set “Search Type” to “Like” and “Enable Search by Category” to “Yes”.
- Reindex the catalog: Go to System > Tools > Index Management and reindex the catalog.
Once you have completed these steps, the search by category functionality should be available on your Magento 2 store. Your customers will be able to search for products by category using the search bar on your site.