15

Quite new to Azure Search, and just wondering what is the difference between facet and filter?

Say I have a product table, and it has category and subcategories. If I want to return things under one category, should I use $filter or use facet? What is the difference between these two?

Many Thanks

2 Answers 2

23

Filter is used to restrict which documents are returned by a query. Faceting is used to produce summaries of field values across those documents. Details are here and you can find more general info about how to use faceting and filter together here.

For your specific example, to retrieve documents for one category, use a filter. If you then want to see how many of those documents there are for each sub-category of that category (for example), use facets.

7

If you compare with SQL, filter equals Where clause and facets are like groupBy, although not exactly.

Below is an example:

enter image description here

Now if you want to know count of each country in the list you can query as below.

{
    search: '*',
    queryType: 'full',
    facets: [
      'country',
    ],
    count: true,
    searchMode: 'any',
    }

It will return complete data along with facets data like:

{ "count": 2, "value": "India" }

else to get data of only country as India, you can query as below.

{
    search: '*',
    queryType: 'full',
    filter: "country:India",
    count: true,
    searchMode: 'any',
    }

and you get only 2 data in return.

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.