1

I am new to magento 2. I have to develop Rest api search service list with categories filter option like web application. Is that possible kindly help me anyone

enter image description here

1
  • Please guide me anyone
    – jeni
    Nov 14, 2016 at 12:30

1 Answer 1

2

You can based on the native Magento 2 search API : \Magento\Framework\Search\Search::search

The API definition : vendor/magento/module-search/etc/webapi.xml

<route url="/V1/search" method="GET">
    <service class="Magento\Search\Api\SearchInterface" method="search"/>
    <resources>
        <resource ref="anonymous" />
    </resources>
</route>

Then build your request such as :

http://{domainName}/index.php/rest/V1/search/?searchCriteria[requestName]=configurator_quick_search_container&searchCriteria[filterGroups][0][filters][0][field]=search_term&searchCriteria[filterGroups][0][filters][0][value]=searchTermValueExample&searchCriteria[filterGroups][0][filters][0][field]=category_ids&searchCriteria[filterGroups][0][filters][0][value]=2

You can specify price aggregation algorithm and price filters :

http://192.168.56.4/index.php/rest/V1/search/?searchCriteria[requestName]=configurator_quick_search_container&searchCriteria[filterGroups][0][filters][0][field]=search_term&searchCriteria[filterGroups][0][filters][0][value]=&searchCriteria[filterGroups][0][filters][1][field]=category_ids&searchCriteria[filterGroups][0][filters][1][value]=2&searchCriteria[filterGroups][0][filters][2][field]=price.from&searchCriteria[filterGroups][0][filters][2][value]=1&searchCriteria[filterGroups][0][filters][3][field]=price.to&searchCriteria[filterGroups][0][filters][3][value]=10000&searchCriteria[filterGroups][0][filters][4][field]=price_dynamic_algorithm&searchCriteria[filterGroups][0][filters][4][value]=auto

The definition of the search request can be found here : vendor/magento/module-catalog-search/etc/search_request.xml

<request query="quick_search_container" index="catalogsearch_fulltext">
    <dimensions>
        <dimension name="scope" value="default"/>
    </dimensions>
    <queries>
        <query xsi:type="boolQuery" name="quick_search_container" boost="1">
            <queryReference clause="should" ref="search" />
            <queryReference clause="must" ref="category"/>
            <queryReference clause="must" ref="price"/>
            <queryReference clause="must" ref="visibility"/>
        </query>
        <query xsi:type="matchQuery" value="$search_term$" name="search">
            <match field="sku"/>
            <match field="*"/>
        </query>
        <query xsi:type="filteredQuery" name="category">
            <filterReference clause="must" ref="category_filter"/>
        </query>
        <query xsi:type="filteredQuery" name="price">
            <filterReference clause="must" ref="price_filter"/>
        </query>
        <query xsi:type="filteredQuery" name="visibility">
            <filterReference clause="must" ref="visibility_filter"/>
        </query>
    </queries>
    <filters>
        <filter xsi:type="termFilter" name="category_filter" field="category_ids" value="$category_ids$"/>
        <filter xsi:type="rangeFilter" name="price_filter" field="price" from="$price.from$" to="$price.to$"/>
        <filter xsi:type="termFilter" name="visibility_filter" field="visibility" value="$visibility$"/>
    </filters>
    <aggregations>
        <bucket name="price_bucket" field="price" xsi:type="dynamicBucket" method="$price_dynamic_algorithm$">
            <metrics>
                <metric type="count"/>
            </metrics>
        </bucket>
        <bucket name="category_bucket" field="category_ids" xsi:type="termBucket">
            <metrics>
                <metric type="count"/>
            </metrics>
        </bucket>
    </aggregations>
    <from>0</from>
    <size>10000</size>
</request>

You can create your own search_request based on this file.

Response example :

<?xml version="1.0"?>
<response>
  <items>
    <item>
      <id>34</id>
      <custom_attributes>
        <item>
          <attribute_code>_score</attribute_code>
          <value>1.0875742</value>
        </item>
      </custom_attributes>
    </item>
    <item>
      <id>29</id>
      <custom_attributes>
        <item>
          <attribute_code>_score</attribute_code>
          <value>1.049452</value>
        </item>
      </custom_attributes>
    </item>
    <item>
      <id>44</id>
      <custom_attributes>
        <item>
          <attribute_code>_score</attribute_code>
          <value>1.049452</value>
        </item>
      </custom_attributes>
    </item>
    <item>
      <id>45</id>
      <custom_attributes>
        <item>
          <attribute_code>_score</attribute_code>
          <value>1.049452</value>
        </item>
      </custom_attributes>
    </item>
    <item>
      <id>13</id>
      <custom_attributes>
        <item>
          <attribute_code>_score</attribute_code>
          <value>1.0122644</value>
        </item>
      </custom_attributes>
    </item>
  </items>
  <aggregations>
    <buckets>
      <item>
        <name>price_bucket</name>
        <values>
          <item>
            <value>*_100</value>
            <metrics>
              <item>*_100</item>
              <item>1</item>
            </metrics>
          </item>
          <item>
            <value>100_200</value>
            <metrics>
              <item>100_200</item>
              <item>0</item>
            </metrics>
          </item>
          <item>
            <value>200_300</value>
            <metrics>
              <item>200_300</item>
              <item>0</item>
            </metrics>
          </item>
          <item>
            <value>300_400</value>
            <metrics>
              <item>300_400</item>
              <item>1</item>
            </metrics>
          </item>
          <item>
            <value>400_500</value>
            <metrics>
              <item>400_500</item>
              <item>1</item>
            </metrics>
          </item>
          <item>
            <value>500_*</value>
            <metrics>
              <item>500_*</item>
              <item>2</item>
            </metrics>
          </item>
        </values>
      </item>
      <item>
        <name>category_bucket</name>
        <values>
          <item>
            <value>2</value>
            <metrics>
              <item>2</item>
              <item>5</item>
            </metrics>
          </item>
          <item>
            <value>25</value>
            <metrics>
              <item>25</item>
              <item>4</item>
            </metrics>
          </item>
          <item>
            <value>24</value>
            <metrics>
              <item>24</item>
              <item>1</item>
            </metrics>
          </item>
        </values>
      </item>
    </buckets>
    <bucket_names>
      <item>price_bucket</item>
      <item>category_bucket</item>
    </bucket_names>
  </aggregations>
  <search_criteria>
    <request_name>configurator_quick_search_container</request_name>
    <filter_groups>
      <item>
        <filters>
          <item>
            <field>search_term</field>
            <value></value>
            <condition_type>eq</condition_type>
          </item>
          <item>
            <field>category_ids</field>
            <value>2</value>
            <condition_type>eq</condition_type>
          </item>
          <item>
            <field>price.from</field>
            <value>1</value>
            <condition_type>eq</condition_type>
          </item>
          <item>
            <field>price.to</field>
            <value>10000</value>
            <condition_type>eq</condition_type>
          </item>
          <item>
            <field>price_dynamic_algorithm</field>
            <value>auto</value>
            <condition_type>eq</condition_type>
          </item>
        </filters>
      </item>
    </filter_groups>
  </search_criteria>
  <total_count>5</total_count>
</response>

Then create your own logic to retrieve product collection based on the productIds.

You can based on the Magento 2 native logic.

Overview:

  1. Create the product collection
  2. Create a temporary table with search product ids results
  3. Join your product collection with the search results ids
  4. Retrieve data
14
  • I recommend you to use this layer. In the future if you use another search engine such as ElasticSearch, your code will be compatible Nov 14, 2016 at 12:55
  • If you need to return frontend image URL from your custom API, you can check my post : magento.stackexchange.com/a/144654/46873 Nov 14, 2016 at 13:05
  • Thanks, rest/V1/products/attributes/center_diamond_size/options service working meanwhile rest/V1/products/attributes/price/options not working. has any possible way to get price options . please guide me franck
    – jeni
    Nov 14, 2016 at 13:18
  • What do you mean by price option ? Is it related to configurable ? You API call is used to retrieve EAV attribute options. Nov 14, 2016 at 13:23
  • The filters are created dynamically based on the response. Try to add price filters on your request. Nov 14, 2016 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.