1

On my search page I have one searchbox, the search interface with filter defined. I do have a city facet that is based on eventcity, I need to add a dropdown to load up all cities so use can select a city in addition to type in a keyword in the searchbox before click the search button

How do I get all cities before with no keyword from all items that meet the filter defined in the search interface by programatically executing the query in javascript and get all unique cities (similiar those that would be loaded to the city facet)?

2 Answers 2

0

You can use Coveo.ExecuteQuery to fetch facet values for the field as below.

Coveo.executeQuery({
    q: '', // Optionally add search terms
    facets: [
        {
            field: '@yourFacetField',
            sortCriteria: 'occurrences',
            numberOfValues: 10
        }
    ]
}).then(function(result) {
    console.log(result.facets[0].values); // Logs the facet values for the specified field
});

Using above query, you can fetch the facet values and display them in the dropdown.

Hope this helps!!!

0

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!!

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.