Hello Community.
My client is building their Answers UI on their own environment. Answers Back end is built on our platform.
To do so, they are using Yext Live API to call the results from our system.
While most of it is working so far, we have encountered 2 roadblocks.
They are trying to call, the “difficulty” field as a facet, as well as the “keywords” field.
I have checked their live API url as well as the json call and it seems that the 2 fields are not being called in.
Is there a way to add these 2 parameters in the Live API url?
Thank you.
Lucas B.
Hi Lucas,
To make a call to the Vertical Search: Query endpoint with a facet option checked, you’ll have to include the facetFilters
param in the API call. More information on this parameter can be found in the link above.
This param needs to be url-encoded from it’s original JSON format. For example, let’s say you want to initiate a vertical search with “Expert” checked as a facet on the c_difficulty
field. The facetFilters param in json would look like this:
{
"c_difficulty": [
{
"c_difficulty": {
"$eq":"Expert"
}
}
]
}
To include this as a query param, it could be encoded as such:
%7B%22c_difficulty%22%3A%5B%7B%22c_difficulty%22%3A%7B%22%24eq%22%3A%22Expert%22%7D%7D%5D%7D
You can also set the retrieveFacets
param to “true” in order to see the state of your facets in the response.
For example, a GET request to the vertical search endpoint for the query “recipe” would look something like the following.
https://liveapi.yext.com/v2/accounts/me/answers/vertical/query?v=20180204&experienceKey={experienceKey}&input=recipe&locale={locale}&api_key={apiKey}&facetFilters={"c_difficulty"%3A[{"c_difficulty"%3A{"%24eq"%3A"Expert"}}]}&retrieveFacets=true&filters={}&verticalKey={verticalKey}
If you can provide more context around the issue with the keywords field, I’d be happy to try to help with that as well.
Best,
DJ
Hi DJ.
Thank you, your answer was what I was looking for.
Now, to make it a bit more ellaborate, if I wanted to combine 2 parameter as AND should it be:
https://liveapi-sandbox.yext.com/v2/accounts/me/answers/vertical/query?v=20180204&experienceKey=sainsbury-answers&input=recipe&locale=en_GB&api_key=a47fa776a5a426f65a3a446584fdfe7f&facetFilters={{“c_difficulty”%3A[{“c_difficulty”%3A{“%24eq”%3A"Expert"}}]}&{“c_keywords”: [{“c_keywords”: { “$eq”:“Roasts”}}]}}&retrieveFacets=true&filters={}&verticalKey=recipe
If I try this syntax I get an error
On the otherhand, what would be the syntax if I wanted to have 1 attribute OR another attribute.
Thank you.
Lucas B
Hey Lucas,
Before looking at the details of the call, a few fundamental things to understand about current Facet behavior:
- Within a single facet, like Difficulty for example, selecting two options will always treat it as “OR”. You cannot choose to treat two options within a single facet as “AND”.
- When you have multiple fields being used as facets and a user were to select an option for both facets, it will always be treated as “AND”. There is not currently a way to change that behavior to “OR”.
To illustrate, let’s say a user checks “Not too tricky” for the Difficulty, and also checks both “Chicken” and “Roasts” for the Keywords facet. This will show any results that have a Difficulty of “Not too tricky” AND have keyword values of “Chicken” OR “Roasts”.
For reference, the API call for the above example would look like this:
https://liveapi-sandbox.yext.com/v2/accounts/me/answers/vertical/query?v=20180204&experienceKey=sainsbury-answers&input=recipe&locale=en_GB&api_key=a47fa776a5a426f65a3a446584fdfe7f&facetFilters={"c_keywords"%3A[{"c_keywords"%3A{"%24eq"%3A"Roasts"}}%2C{"c_keywords"%3A{"%24eq"%3A"Chicken"}}]%2C"c_difficulty"%3A[{"c_difficulty"%3A{"%24eq"%3A"Not%20too%20tricky"}}]}&retrieveFacets=true&filters={}&verticalKey=recipe
This leads to a single result, because only 1 “not too tricky” recipe has “chicken” or “roasts” as a keyword. The facetFilters
param is equal to an encoded version of this JSON:
{"c_keywords":[{"c_keywords":{"$eq":"Roasts"}},{"c_keywords":{"$eq":"Chicken"}}],"c_difficulty":[{"c_difficulty":{"$eq":"Not too tricky"}}]}
Best,
DJ