Skip to content

Commit 67a8ae9

Browse files
committed
Add distinct count example
1 parent 1a70e85 commit 67a8ae9

File tree

1 file changed

+106
-9
lines changed

1 file changed

+106
-9
lines changed

articles/search/search-faceted-navigation.md

Lines changed: 106 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ms.date: 02/26/2025
1313

1414
# Add faceted navigation to a search app
1515

16-
Faceted navigation is used for self-directed drill-down filtering on query results in a search app, where your application offers form controls for scoping search to groups of documents (for example, categories or brands), and Azure AI Search provides the data structures and filters to back the experience.
16+
Faceted navigation is used for self-directed drill-down filtering on query results in a search app, where your application offers form controls for scoping search to groups of documents (for example, categories or brands), and Azure AI Search provides the data structures and filters to back the experience.
1717

1818
In this article, learn how to return a faceted navigation structure in Azure AI Search.
1919

@@ -175,9 +175,9 @@ Recall that facets are calculated from results in a query response. You only get
175175

176176
Interval facets on date time are computed based on the UTC time if `timeoffset` isn't specified. For example: for "facet=lastRenovationDate,interval:day", the day boundary starts at 00:00:00 UTC.
177177

178-
### Facet example
178+
### Basic facet example
179179

180-
The following example works against the hotels sample index. It shows three facets for "Category", "Tags", and "Rating", with a count override on "Tags" and explicit whole number values set on "Rating", which is otherwise a float value in the index.
180+
The following example works against the [hotels sample index](search-get-started-portal.md). You can use **JSON view** in Search Explorer to paste in the JSON query. This example shows three facets for "Category", "Tags", and "Rating", with a count override on "Tags" and explicit whole number values set on "Rating", which is otherwise a float value in the index.
181181

182182
```http
183183
POST https://{{service_name}}.search.windows.net/indexes/hotels/docs/search?api-version={{api_version}}
@@ -192,12 +192,109 @@ POST https://{{service_name}}.search.windows.net/indexes/hotels/docs/search?api-
192192
}
193193
```
194194

195-
For each faceted navigation tree, there's a default limit of the top 10 facet instances found in search results. This default makes sense for navigation structures because it keeps the values list to a manageable size. You can override the default by assigning a value to "count". For example, `"Tags,count:5"` reduces the number of tags under the Tags section to the top five.
195+
For each faceted navigation tree, there's a default limit of the top 10 facet instances found by the query. This default makes sense for navigation structures because it keeps the values list to a manageable size. You can override the default by assigning a value to "count". For example, `"Tags,count:5"` reduces the number of tags under the Tags section to the top five.
196196

197197
For Numeric and DateTime values only, you can explicitly set values on the facet field (for example, `facet=Rating,values:1|2|3|4|5`) to separate results into contiguous ranges (either ranges based on numeric values or time periods). Alternatively, you can add "interval", as in `facet=Rating,interval:1`.
198198

199199
Each range is built using 0 as a starting point, a value from the list as an endpoint, and then trimmed of the previous range to create discrete intervals.
200200

201+
### Distinct values example
202+
203+
You can formulate a query that returns a distinct value count for each facetable field. This example formulates a query that sets `top` to zero, returning just the counts, with no results.
204+
205+
For brevity, it includes just two fields marked as "facetable" in the hotels sample index.
206+
207+
```http
208+
POST https://{{service_name}}.search.windows.net/indexes/hotels/docs/search?api-version={{api_version}}
209+
{
210+
"search": "*",
211+
"count": true,
212+
"top": 0,
213+
"facets": [
214+
"Category", "Address/StateProvince""
215+
]
216+
}
217+
```
218+
219+
Results from this query are as follows:
220+
221+
```json
222+
{
223+
"@odata.count": 50,
224+
"@search.facets": {
225+
"Address/StateProvince": [
226+
{
227+
"count": 9,
228+
"value": "WA"
229+
},
230+
{
231+
"count": 6,
232+
"value": "CA "
233+
},
234+
{
235+
"count": 4,
236+
"value": "FL"
237+
},
238+
{
239+
"count": 3,
240+
"value": "NY"
241+
},
242+
{
243+
"count": 3,
244+
"value": "OR"
245+
},
246+
{
247+
"count": 3,
248+
"value": "TX"
249+
},
250+
{
251+
"count": 2,
252+
"value": "GA"
253+
},
254+
{
255+
"count": 2,
256+
"value": "MA"
257+
},
258+
{
259+
"count": 2,
260+
"value": "TN"
261+
},
262+
{
263+
"count": 1,
264+
"value": "AZ"
265+
}
266+
],
267+
"Category": [
268+
{
269+
"count": 13,
270+
"value": "Budget"
271+
},
272+
{
273+
"count": 12,
274+
"value": "Suite"
275+
},
276+
{
277+
"count": 7,
278+
"value": "Boutique"
279+
},
280+
{
281+
"count": 7,
282+
"value": "Resort and Spa"
283+
},
284+
{
285+
"count": 6,
286+
"value": "Extended-Stay"
287+
},
288+
{
289+
"count": 5,
290+
"value": "Luxury"
291+
}
292+
]
293+
},
294+
"value": []
295+
}
296+
```
297+
201298
## Best practices for working with facets
202299

203300
This section is a collection of tips and workarounds that are helpful for application development.
@@ -230,9 +327,11 @@ If you want a static set of facets alongside a dynamic drilldown experience, you
230327

231328
When you design the user experience, remember to add a mechanism for clearing facets. A common approach for clearing facets is issuing an empty search request to reset the page.
232329

233-
### Trim facet results with more filters
330+
### Handling large facet counts
234331

235-
Facet results are documents found in the search results that match a facet term. In the following example, in search results for *cloud computing*, 254 items also have *internal specification* as a content type. Items aren't necessarily mutually exclusive. If an item meets the criteria of both filters, it's counted in each one. This duplication is possible when faceting on `Collection(Edm.String)` fields, which are often used to implement document tagging.
332+
Search results and facet results that are too large can be trimmed by [adding filters](search-filters.md). In the following example, in the query for *cloud computing*, 254 items have *internal specification* as a content type. If results are too large, adding filters can help your users re-scope the query by adding more criteria.
333+
334+
Items aren't necessarily mutually exclusive. If an item meets the criteria of both filters, it's counted in each one. This duplication is possible when faceting on `Collection(Edm.String)` fields, which are often used to implement document tagging.
236335

237336
```output
238337
Search term: "cloud computing"
@@ -241,8 +340,6 @@ Content type
241340
Video (10)
242341
```
243342

244-
In general, if you find that facet results are consistently too large, we recommend that you add more filters to give users more options for narrowing the search.
245-
246343
## Next steps
247344

248-
We recommend the [C#: Add search to web apps](tutorial-csharp-overview.md) for an example of faceted navigation. The sample also includes filters, suggestions, and autocomplete. It uses JavaScript and React for the presentation layer.
345+
We recommend the [C#: Add search to web apps](tutorial-csharp-overview.md) for an example of faceted navigation that includes code for the presentation layer. The sample also includes filters, suggestions, and autocomplete. It uses JavaScript and React for the presentation layer.

0 commit comments

Comments
 (0)