Skip to content

Product Search with our Java client

Looking for setup prerequisites, authentication details, and package references? Start with the Java SDK landing page.

Start by making a Searcher instance:

java
Searcher searcher = new Searcher(
  "00000000-0000-0000-0000-000000000001",
  "your API Key",
  "the server URL for the dataset");

Before you proceed, read about handling different types of users here.

The following shows an example of how to perform a product search with 3 selected facets:

java
ProductSearchRequest productSearch = ProductSearchRequest.create(
    Language.create("da"),
    Currency.create("DKK"),
    getUser(),
    "Search overlay", // displayed at location
    "gaffel", // search term
    0, // skip
    10 // take
).setSettings(ProductSearchSettings.create()
    .setSelectedProductProperties(SelectedProductPropertiesSettings.create()
        .setDisplayName(true)
        .setCategoryPaths(true)
        .setBrand(true)
        .setDataKeys("Description", "ImagePath")
        .setAllVariants(true)
    )
).setFacets(ProductFacetQuery.create()
    .addToItems(BrandFacet.create()
        .setSelected("77", "93")
    )
    .addToItems(ProductDataStringValueFacet.create(
        DataSelectionStrategy.Product,
        "Environment",
        new ArrayList<>() { { add("Selection 1"); } },
        null
    ))
    .addToItems(
        ProductDataBooleanValueFacet.create(
            DataSelectionStrategy.Product,
            "InStock",
            new ArrayList<>() { { add(true); } },
            null
        )
    )
);

searcher.search(productSearch);

Using facets and selecting values

Add the following code to the request to return a facet with the different brands from the products matching the search.

java
productSearch.setFacets(
    ProductFacetQuery.create()
        .addToItems(BrandFacet.create())
);

When the user selects a brand, update the code to the following:

java
productSearch.setFacets(
    ProductFacetQuery.create()
        .addToItems(BrandFacet.create()
            .setSelected("93")
        )
);

You can learn more about facets here.

Batching Search Requests in Java

Similar to filtering, API requests to Relewise may be batched to reduce network traffic and ensure that related Search results are returned together.

The following example batches a Product Search Request and a Product Category Search Request:

java
User user = getUser();

ProductSearchRequest productSearch = ProductSearchRequest.create(
    Language.create("da"),
    Currency.create("DKK"),
    user,
    "Search Page",
    "shoe",
    0,
    10
).setSettings(
    ProductSearchSettings.create()
        .setSelectedProductProperties(
            SelectedProductPropertiesSettings.create()
                .setDisplayName(true)
        )
);

ProductCategorySearchRequest productCategorySearch = ProductCategorySearchRequest.create(
    Language.create("da"),
    Currency.create("DKK"),
    user,
    "Search Page",
    "shoe",
    0,
    10
).setSettings(
    ProductCategorySearchSettings.create()
        .setSelectedCategoryProperties(
            SelectedProductCategoryPropertiesSettings.create()
                .setDisplayName(true)
        )
);

SearchRequestCollection batchedRequest = SearchRequestCollection.create(
    productSearch,
    productCategorySearch
);

SearchResponseCollection response = searcher.batch(batchedRequest);

Collections that exceed the configured client-side batch size are automatically split into multiple API requests. The responses are combined in the same order as the original requests. The default batch size is 1,000, but you can configure a different maximum before sending the collection:

java
searcher.setBatchSize(500);

Don't know us? Don't worry - you can find more information about us, by visiting our main page www.relewise.com