diff --git a/README.md b/README.md index b5ce84d..857706c 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Add the following dependency to your pom file com.indix.api indix-api-java - 3.1.0 + 3.2.0 ``` @@ -108,6 +108,30 @@ It returns summary information for a product. indixApiClient.close(); } ``` +### Search Query with attribute facets and filters + +The following example shows how to filter products based on attributes. It retrieves a list of products matching a variety of +query parameters along with attributes facets with their offers and catalog info across stores + +```java + try { + Query searchQuery = QueryFactory.newSearchQuery() + .withQ("nike") + .withCountryCode("US") + .withAttrFacetBy("color"); + .withAttrFilter("color", colorList); + + UniversalSearchResult sr = indixApiClient.getProductsUniversal(searchQuery); + System.out.println(sr.getCount()); + System.out.println(sr.getProducts().size()); + System.out.println(sr.getFacets().size()); + System.out.println(sr.getAttrFacets().size()); + System.out.println(sr.getProducts().get(0).getMpid()); + } finally { + indixApiClient.close(); + } +``` + ### Bulk Products Query The following example shows how to request for a bulk search query which finds products against a list diff --git a/pom.xml b/pom.xml index dde423a..676e5d8 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.indix.api indix-api-java - 3.1.0 + 3.2.0 jar Indix API-V2 Java Client Java client which is used to access API V2 endpoints diff --git a/src/main/java/com/indix/models/searchResult/AttrFacet.java b/src/main/java/com/indix/models/searchResult/AttrFacet.java new file mode 100644 index 0000000..564d4e2 --- /dev/null +++ b/src/main/java/com/indix/models/searchResult/AttrFacet.java @@ -0,0 +1,15 @@ +package com.indix.models.searchResult; + +public class AttrFacet { + private String name; + private int count; + + public String getName() { + return name; + } + + public int getCount() { + return count; + } + +} diff --git a/src/main/java/com/indix/models/searchResult/SearchResult.java b/src/main/java/com/indix/models/searchResult/SearchResult.java index 73c4467..c2350e3 100644 --- a/src/main/java/com/indix/models/searchResult/SearchResult.java +++ b/src/main/java/com/indix/models/searchResult/SearchResult.java @@ -6,6 +6,7 @@ public class SearchResult { private int count; private Map> facets; + private Map> attrFacets; public int getCount() { return count; @@ -14,4 +15,8 @@ public int getCount() { public Map> getFacets() { return facets; } + + public Map> getAttrFacets() { + return attrFacets; + } } diff --git a/src/main/java/com/indix/query/SearchQuery.java b/src/main/java/com/indix/query/SearchQuery.java index db69a1f..ef450af 100644 --- a/src/main/java/com/indix/query/SearchQuery.java +++ b/src/main/java/com/indix/query/SearchQuery.java @@ -54,6 +54,23 @@ public SearchQuery withCategoryId(List categoryId) { return this; } + /** + * Combined with any of query/brand/category/store, limits results to products with specific attribute values + * NOTE: + * Examples of a few product attribute keys and values are: + * 1. color -> red / blue / green / black + * 2. size -> xs / s / m / l / xl + * 3. gender -> mens / womens + * A schema for the filterable keys and values isn't in place yet. + * When the standard schema is published, the filterable keys for a category and the list of values for the same will be available. + */ + public SearchQuery withAttrFilter(String attrFilterKey, List attrFilterValues) { + for (String attrValue: attrFilterValues) { + parameters.add(new BasicNameValuePair("attr."+attrFilterKey, attrValue)); + } + return this; + } + /** * Combined with end_price, limits results to products sold by at least one store at a price between start and end */ @@ -214,6 +231,16 @@ public SearchQuery withFacetBy(List facetBy) { return this; } + /** + * Facet by product attribute values + */ + public SearchQuery withAttrFacetBy(List attrFacetBy) { + for (String attrFacet : attrFacetBy) { + parameters.add(new BasicNameValuePair("attrFacetBy", "attr."+attrFacet)); + } + return this; + } + /** * Specifies the page number of the result set to return. 10 results per page. */ diff --git a/src/test/java/com/indix/query/SearchQueryTest.java b/src/test/java/com/indix/query/SearchQueryTest.java index 6d58e98..0b28c9c 100644 --- a/src/test/java/com/indix/query/SearchQueryTest.java +++ b/src/test/java/com/indix/query/SearchQueryTest.java @@ -41,6 +41,11 @@ public void testBasicQuery() { expectedValue.add(new BasicNameValuePair("sortBy", "PRICE_HIGH_TO_LOW")); expectedValue.add(new BasicNameValuePair("facetBy", "storeId")); expectedValue.add(new BasicNameValuePair("facetBy", "brandId")); + expectedValue.add(new BasicNameValuePair("attrFacetBy", "attr.color")); + expectedValue.add(new BasicNameValuePair("attrFacetBy", "attr.size")); + expectedValue.add(new BasicNameValuePair("attr.color", "black")); + expectedValue.add(new BasicNameValuePair("attr.color", "red")); + expectedValue.add(new BasicNameValuePair("attr.size", "xs")); expectedValue.add(new BasicNameValuePair("pageNumber", "5")); expectedValue.add(new BasicNameValuePair("pageSize", "55")); @@ -52,6 +57,9 @@ public void testBasicQuery() { .withSku("sku1") .withSortBy(SearchQuery.SortBy.PRICE_HIGH_TO_LOW) .withFacetBy(Arrays.asList("storeId", "brandId")) + .withAttrFacetBy(Arrays.asList("color", "size")) + .withAttrFilter("color", Arrays.asList("black", "red")) + .withAttrFilter("size", Arrays.asList("xs")) .withPageNumber(5) .withPageSize(55) .withCountryCode("US")