Search code examples
azurecurlpostmanazure-cognitive-search

How to do faceted search with Azure cognitive search?


I am trying to do faceted search in Azure cognitive search. I am using postman (and curl) to test.

Got a field state_name which is facetable. Following https://learn.microsoft.com/en-us/rest/api/searchservice/search-documents, if I send the POST request payload as:

{
    "facets": "state_name"
}

it returns this error:

{
    "error": {
        "code": "",
        "message": "The request is invalid. Details: parameters : When trying to read a null collection parameter value in JSON Light, a node of type 'PrimitiveValue' with the value 'state_name' was read from the JSON reader; however, a primitive 'null' value was expected.\r\n"
    }
}

What's the correct way to do facet search?


UPDATE:

facets in payload should be an array:

{
    "facets": ["state_name"]
}

as shown in https://learn.microsoft.com/en-us/rest/api/searchservice/search-documents#bkmk_examples


Solution

  • You need to specify the facet field as part of the url:

    GET /indexes/hotels/docs?search=*&facet=state_name&api-version=2019-05-06

    more info: https://learn.microsoft.com/en-us/rest/api/searchservice/search-documents#bkmk_examples