docs-site/content/0.22.0/api/curation.md
While Typesense makes it really easy and intuitive to deliver great search results, sometimes you might want to promote certain documents over others. Or, you might want to exclude certain documents from a query's result set.
Using overrides, you can include or exclude specific documents for a given query.
In the following example, we will:
includes condition to place the documents with ids 422 and 54 in the first and second positions
respectively.excludes condition to remove the document with id 287 from the results.Typesense allows you to use both includes and excludes or just one of them for curation.
Note how we are applying these overrides to an exact match of the query apple. Instead, if we want to match all
queries containing the word apple, we will use the contains match instead.
override = {
"rule": {
"query": "apple",
"match": "exact"
},
"includes": [
{"id": "422", "position": 1},
{"id": "54", "position": 2}
],
"excludes": [
{"id": "287"}
]
}
// Creates/updates an override called `customize-apple` in the `companies` collection
client.collections('companies').overrides().upsert('customize-apple', override)
$override = [
"rule" => [
"query" => "apple",
"match" => "exact"
],
"includes" => [
["id" => "422", "position" => 1],
["id" => "54", "position" => 2]
],
"excludes" => [
["id" => "287"]
]
];
# Creates/updates an override called `customize-apple` in the `companies` collection
$client->collections['companies']->overrides->upsert('customize-apple', $override);
override = {
"rule": {
"query": "apple",
"match": "exact"
},
"includes": [
{"id": "422", "position": 1},
{"id": "54", "position": 2}
],
"excludes": [
{"id": "287"}
]
}
# Creates/updates an override called `customize-apple` in the `companies` collection
client.collections['companies'].overrides.upsert('customize-apple', override)
override = {
"rule" => {
"query" => "apple",
"match" => "exact"
},
"includes" => [
{"id" => "422", "position" => 1},
{"id" => "54", "position" => 2}
],
"excludes" => [
{"id" => "287"}
]
}
# Creates/updates an override called `customize-apple` in the `companies` collection
client.collections['companies'].overrides.upsert('customize-apple', override)
final override = {
"rule": {"query": "apple", "match": "exact"},
"includes": [
{"id": "422", "position": 1},
{"id": "54", "position": 2}
],
"excludes": [
{"id": "287"}
]
};
// Creates/updates an override called `customize-apple` in the `companies` collection
await client.collection('companies').overrides.upsert('customize-apple', override);
SearchOverrideSchema searchOverrideSchema = new SearchOverrideSchema();
searchOverrideSchema.addIncludesItem(new SearchOverrideInclude().id("422").position(1))
.addIncludesItem(new SearchOverrideInclude().id("54").position(2))
.addExcludesItem(new SearchOverrideExclude().id("287"))
.rule(new SearchOverrideRule().query("apple").match(SearchOverrideRule.MatchEnum.EXACT))
// Creates/updates an override called `customize-apple` in the `companies` collection
SearchOverride searchOverride = client.collections("companies").overrides().upsert("customize-apple", searchOverrideSchema);
curl "http://localhost:8108/collections/companies/overrides/customize-apple" -X PUT \
-H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" -d '{
"rule": {
"query": "apple",
"match": "exact"
},
"includes": [
{"id": "422", "position": 1},
{"id": "54", "position": 2}
],
"excludes": [
{"id": "287"}
]
}'
{
"id": "customize-apple",
"excludes": [
{
"id": "287"
}
],
"includes": [
{
"id": "422",
"position": 1
},
{
"id": "54",
"position": 2
}
],
"rule": {
"match": "exact",
"query": "apple"
}
}
In the following example, we will apply a filter dynamically to a query that matches a rule.
<Tabs :tabs="['JavaScript','PHP','Python','Ruby','Dart','Java','Shell']"> <template v-slot:JavaScript>override = {
"rule": {
"query": "{brand} phone",
"match": "contains"
},
"filter_by": "brand:={brand}",
"remove_matched_tokens": true
}
// Creates/updates an override called `brand-filter` in the `companies` collection
client.collections('companies').overrides().upsert('brand-filter', override)
$override = [
"rule" => [
"query" => "{brand} phone",
"match" => "contains"
],
"filter_by" => "brand:={brand}",
"remove_matched_tokens" => true
];
# Creates/updates an override called `brand-filter` in the `companies` collection
$client->collections['companies']->overrides->upsert('brand-filter', $override);
override = {
"rule": {
"query": "{brand} phone",
"match": "contains"
},
"filter_by": "brand:={brand}",
"remove_matched_tokens": True
}
# Creates/updates an override called `brand-filter` in the `companies` collection
client.collections['companies'].overrides.upsert('brand-filter', override)
override = {
"rule": {
"query": "{brand} phone",
"match": "contains"
},
"filter_by": "brand:={brand}",
"remove_matched_tokens": true
}
# Creates/updates an override called `brand-filter` in the `companies` collection
client.collections['companies'].overrides.upsert('brand-filter', override)
final override = {
"rule": {
"query": "{brand} phone",
"match": "contains"
},
"filter_by": "brand:={brand}",
"remove_matched_tokens": true
};
// Creates/updates an override called `brand-filter` in the `companies` collection
await client.collection('companies').overrides.upsert('brand-filter', override);
SearchOverrideSchema searchOverrideSchema = new SearchOverrideSchema();
searchOverrideSchema.rule(new SearchOverrideRule().query("{brand} phone")
.match(SearchOverrideRule.MatchEnum.CONTAINS))
// Creates/updates an override called `customize-apple` in the `companies` collection
SearchOverride searchOverride = client.collections("companies").overrides().upsert("brand-filter", searchOverrideSchema);
curl "http://localhost:8108/collections/companies/overrides/brand-filter" -X PUT \
-H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" -d '{
"rule": {
"query": "{brand} phone",
"match": "contains"
},
"filter_by": "brand:={brand}",
"remove_matched_tokens": true
}'
With this override in effect, any query that contains a brand will automatically be filtered on the matching brand directly. In addition, the brand will also be removed from the original query tokens, so that the search is made only on the remaining tokens.
For example, if someone searches for sony ericsson phone, the query will be re-written as phone and a
sony ericsson brand filter will be directly applied. If you don't wish to remove matching tokens from the query,
set remove_matched_tokens to false. By default, this parameter is set to true.
PUT ${TYPESENSE_HOST}/collections/:collection/overrides/:id
| Parameter | Required | Description |
|---|---|---|
| rule.query | yes | Indicates what search queries should be overridden. |
| rule.match | yes | Indicates whether the match on the query term should be exact or contains. |
| excludes | no | List of document ids that should be excluded from the search results. |
| includes | no | List of document ids that should be included in the search results with their corresponding positions. |
| filter_by | no | A filter by clause that is applied to any search query that matches the override rule. |
| remove_matched_tokens | no | Indicates whether search query tokens that exist in the override's rule should be removed from the search query. |
Default: true.|
Listing all overrides associated with a given collection.
<Tabs :tabs="['JavaScript','PHP','Python','Ruby','Dart','Java','Shell']"> <template v-slot:JavaScript>client.collections('companies').overrides().retrieve()
$client->collections['companies']->overrides->retrieve();
client.collections['companies'].overrides.retrieve()
client.collections['companies'].overrides.retrieve
await client.collection('companies').overrides.retrieve();
SearchOverridesResponse searchOverridesResponse = client.collections("companies").overrides().retrieve();
curl -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
"http://localhost:8108/collections/companies/overrides"
{
"overrides":[
{
"id":"customize-apple",
"excludes":[
{
"id":"287"
}
],
"includes":[
{
"id":"422",
"position":1
},
{
"id":"54",
"position":2
}
],
"rule":{
"match":"exact",
"query":"apple"
}
}
]
}
GET ${TYPESENSE_HOST}/collections/:collection/overrides
Fetch an individual override associated with a collection.
<Tabs :tabs="['JavaScript','PHP','Python','Ruby','Dart','Java','Shell']"> <template v-slot:JavaScript>client.collections('companies').overrides('customize-apple').retrieve()
$client->collections['companies']->overrides['customize-apple']->retrieve();
client.collections['companies'].overrides['customize-apple'].retrieve()
client.collections['companies'].overrides['customize-apple'].retrieve
await client.collection('companies').override('customize-apple').retrieve();
SearchOverride searchOverride = client.collections("companies").overrides("customize-apple").retrieve();
curl "http://localhost:8108/collections/companies/overrides/customize-apple" -X GET \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}"
{
"id":"customize-apple",
"excludes":[
{
"id":"287"
}
],
"includes":[
{
"id":"422",
"position":1
},
{
"id":"54",
"position":2
}
],
"rule":{
"match":"exact",
"query":"apple"
}
}
GET ${TYPESENSE_HOST}/collections/:collection/overrides/:id
Deleting an override associated with a collection.
<Tabs :tabs="['JavaScript','PHP','Python','Ruby','Dart','Java','Shell']"> <template v-slot:JavaScript>client.collections('companies').overrides('customize-apple').delete()
$client->collections['companies']->overrides['customize-apple']->delete();
client.collections['companies'].overrides['customize-apple'].delete()
client.collections['companies'].overrides['customize-apple'].delete
await client.collection('companies').override('customize-apple').delete();
SearchOverride searchOverride = client.collections("companies").overrides("customize-apple").delete();
curl "http://localhost:8108/collections/companies/overrides/customize-apple" -X DELETE \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}"
{
"id": "customize-apple"
}
DELETE ${TYPESENSE_HOST}/collections/:collection/overrides/:id