-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
251 additions
and
17 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
website/docs/api-clients/guides/filtering-your-search.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
--- | ||
title: Filtering your search | ||
--- | ||
|
||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
Filtering is primarily used in the context of front-end search. We call this faceting, where filters are displayed on the search UI as clickable items, allowing users to select one or more filters. This enables a more refined, drilled-down search experience. | ||
|
||
## How to Filter Your Data | ||
|
||
### 1. Define attributes that need to be filterable (at indexing time) | ||
|
||
Initially, filter attributes must be defined as facets, using the `attributesForFaceting` parameter. This can be done using the `setSettings` method. | ||
|
||
<Tabs | ||
groupId="language" | ||
defaultValue="js" | ||
values={[ | ||
{ label: 'JavaScript', value: 'js', } | ||
] | ||
}> | ||
<TabItem value="js"> | ||
|
||
```js | ||
await client.setSettings({ | ||
indexName: '<YOUR_INDEX_NAME>', | ||
indexSettings: { | ||
attributesForFaceting: [ | ||
'actor', | ||
'filterOnly(category)', | ||
'searchable(publisher)', | ||
], | ||
}, | ||
}); | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
### 2. Filter by Attributes (at query time) | ||
|
||
The actual filtering of records is performed at query time, not at indexing time. For this, you need to use the filters parameter in your search code. | ||
|
||
#### Filtering by string using the `filters` field | ||
|
||
<Tabs | ||
groupId="language" | ||
defaultValue="js" | ||
values={[ | ||
{ label: 'JavaScript', value: 'js', } | ||
] | ||
}> | ||
<TabItem value="js"> | ||
|
||
```js | ||
// Only "Scarlett Johansson" actor | ||
await client.search({ | ||
indexName: '<YOUR_INDEX_NAME>', | ||
searchParams: { | ||
query: '<YOUR_QUERY>', | ||
filters: 'actor:Scarlett Johansson', | ||
}, | ||
}); | ||
|
||
// Only "Tom Cruise" or "Scarlett Johansson" actor | ||
await client.search({ | ||
indexName: '<YOUR_INDEX_NAME>', | ||
searchParams: { | ||
query: '<YOUR_QUERY>', | ||
filters: 'actor:Tom Cruise OR actor:Scarlett Johansson', | ||
}, | ||
}); | ||
|
||
// Everything but "Nicolas Cage" actor | ||
await client.search({ | ||
indexName: '<YOUR_INDEX_NAME>', | ||
searchParams: { | ||
query: '<YOUR_QUERY>', | ||
filters: 'NOT actor:Nicolas Cage', | ||
}, | ||
}); | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
#### Filtering by string using the `facetFilters` field | ||
|
||
<Tabs | ||
groupId="language" | ||
defaultValue="js" | ||
values={[ | ||
{ label: 'JavaScript', value: 'js', } | ||
] | ||
}> | ||
<TabItem value="js"> | ||
|
||
```js | ||
// Only "Scarlett Johansson" actor | ||
await client.search({ | ||
indexName: '<YOUR_INDEX_NAME>', | ||
searchParams: { | ||
query: '<YOUR_QUERY>', | ||
facetFilters: ['actor:Scarlett Johansson'], | ||
}, | ||
}); | ||
|
||
// Only "Tom Cruise" or "Scarlett Johansson" actor | ||
await client.search({ | ||
indexName: '<YOUR_INDEX_NAME>', | ||
searchParams: { | ||
query: '<YOUR_QUERY>', | ||
facetFilters: ['actor:Tom Cruise', 'actor:Scarlett Johansson'], | ||
}, | ||
}); | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
title: Retrieving facets | ||
--- | ||
|
||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
To retrieve facets and their respective counts as part of the JSON response, you must specify a list of facets in the facets parameter at query time. | ||
|
||
For example, you can retrieve your books' facets with the `search` method, and the `facets` parameter. | ||
|
||
> When the `facets` parameter is empty, the engine returns no facet information. | ||
<Tabs | ||
groupId="language" | ||
defaultValue="js" | ||
values={[ | ||
{ label: 'JavaScript', value: 'js', } | ||
] | ||
}> | ||
<TabItem value="js"> | ||
|
||
```js | ||
await client.search({ | ||
indexName: '<YOUR_INDEX_NAME>', | ||
searchParams: { | ||
query: '<YOUR_QUERY>', | ||
facets: ['author', 'genre'], | ||
}, | ||
}); | ||
``` | ||
|
||
To extract all facet information, you can use a wildcard (`*`). | ||
|
||
```js | ||
await client.search({ | ||
indexName: '<YOUR_INDEX_NAME>', | ||
searchParams: { | ||
query: '<YOUR_QUERY>', | ||
facets: ['*'], | ||
}, | ||
}); | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--- | ||
title: Send data to Algolia | ||
--- | ||
|
||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
Algolia doesn’t search directly into your own data source. For data to be searchable, you need to send it to Algolia’s servers. | ||
|
||
This happens right after retrieving your data from your data source and reformatting it. Once your data is ready, you can push it to Algolia using the `saveObjects` method. | ||
|
||
## Required credentials | ||
|
||
To push data to Algolia, you need an Application ID and a valid API key with the right access level. You can find them and create new ones [in the API keys page](https://www.algolia.com/account/api-keys/all?applicationId=QPBQ67WNIG). | ||
|
||
## Setting up the API client | ||
|
||
> [Make sure to also read the Installation page](/docs/api-clients/installation). | ||
<Tabs | ||
groupId="language" | ||
defaultValue="js" | ||
values={[ | ||
{ label: 'JavaScript', value: 'js', } | ||
] | ||
}> | ||
<TabItem value="js"> | ||
|
||
```js | ||
// for the default version | ||
import { algoliasearch } from '@experimental-api-clients-automation/algoliasearch'; | ||
|
||
// you can also import the lite version, with search only versions | ||
// import { algoliasearch } from '@experimental-api-clients-automation/algoliasearch-lite'; | ||
|
||
const client = algoliasearch('<YOUR_APP_ID>', '<YOUR_API_KEY>'); | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
## Fetching your data | ||
|
||
Before sending anything to Algolia, you need to retrieve your data. You can do this in several ways, in our case we will pick it from the source code directly. | ||
|
||
<Tabs | ||
groupId="language" | ||
defaultValue="js" | ||
values={[ | ||
{ label: 'JavaScript', value: 'js', } | ||
] | ||
}> | ||
<TabItem value="js"> | ||
|
||
```js | ||
const records = [{ name: 'Tom Cruise' }, { name: 'Scarlett Johansson' }]; | ||
|
||
client.saveObject({ | ||
indexName: '<YOUR_INDEX_NAME>', | ||
// Accepts a free form `Record<string, any>` object. | ||
body: records, | ||
}); | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters