To get all unique values for a facet (like eventcity)
on a Coveo search
page before any keyword search, you can run a query that fetches only the facet values using the Coveo JavaScript Search Framework
, without retrieving search results.
Use FacetRequest
to Get the Facet Values : To get facet values specifically for eventcity
, you can either submit a FacetRequest
or modify your search query. I'm adding here some code how to fetch and extract these facet values using the JavaScript API:
// Assuming you have initialized the Coveo search interface
var queryController = Coveo.get("searchInterface").queryController;
// The facet you want to retrieve (eventcity)
var facetField = "eventcity";
// Prepare the query to fetch facet values without returning regular search results
var query = Coveo.Query.create("");
// Add a facet request to the query
query.enableDebug = true;
query.addFacet(facetField);
// Ensure to include the filters already applied in the search interface
// This respects filters that might be set in your search interface
queryController.getEndpoint().search(query).then(function(response) {
// Extract the facet values from the response
var facet = response.facets.find(facet => facet.field === facetField);
if (facet) {
var facetValues = facet.values;
// Extract the city names from the facet values
var cityNames = facetValues.map(facetValue => facetValue.value);
console.log("Available cities:", cityNames);
// Now you can use the cityNames array to populate your dropdown
}
}).catch(function(error) {
console.error("Error fetching facet values", error);
});
Hope this help!!