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

2

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

Mitchelmitchell answered 16/11, 2016 at 16:51 Comment(0)
A
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.

Ahner answered 16/11, 2016 at 17:10 Comment(0)
I
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.

Iridescent answered 7/10, 2022 at 13:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.