daxu
daxu

Reputation: 4084

What is the difference between $filter and facet in Azure Search

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

Upvotes: 17

Views: 12872

Answers (2)

Bibek Panda
Bibek Panda

Reputation: 619

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.

Upvotes: 7

Bruce Johnston
Bruce Johnston

Reputation: 8634

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.

Upvotes: 25

Related Questions