This page documents an earlier version of the API client. For the latest version, see Create or replace a rule.
editSettings
Examples
Save a rule
Report incorrect code
Copy
Rule ruleToSave = new Rule
{
ObjectID = "a-rule-id",
Enabled = false,
Conditions = new List<Condition>
{
new Condition { Anchoring = "contains", Pattern = "smartphone" },
},
Consequence = new Consequence
{
Params = new ConsequenceParams
{
AutomaticFacetFilters = new List<AutomaticFacetFilter>
{
new AutomaticFacetFilter { Facet = "category = 1" }
}
}
},
Validity = new List<TimeRange>
{
new TimeRange
{
From = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(),
Until = new DateTimeOffset(DateTime.UtcNow.AddDays(10)).ToUnixTimeSeconds()
}
}
};
index.SaveRule(rule, forwardToReplicas: false);
// Asynchronous
await index.SaveRuleAsync(rule, forwardToReplicas: false);
Save a rule with alternatives enabled
Report incorrect code
Copy
Rule ruleToSave = new Rule
{
ObjectID = "a-rule-id",
Conditions = new List<Condition>
{
new Condition { Anchoring = "contains", Pattern = "smartphone", Alternatives = true },
}
Consequence = new Consequence
{
Params = new ConsequenceParams
{
Filters = "category = 1"
}
},
};
index.SaveRule(rule);
Save a rule with a context-based condition
Report incorrect code
Copy
Rule ruleToSave = new Rule
{
ObjectID = "a-rule-id",
Conditions = new List<Condition>
{
new Condition { Context = "mobile" }
},
Consequence = new Consequence
{
Params = new ConsequenceParams
{
AutomaticFacetFilters = new List<AutomaticFacetFilter>
{
new AutomaticFacetFilter { Facet = "release_date >= 1568498400" }
}
}
}
};
index.SaveRule(rule, forwardToReplicas: false);
Parameters
The rule object with conditions and consequences.
For a complete JSON rule object, see the
saveRule
HTTP API reference.Show child attributes
Show child attributes
Action to perform when this rule is triggered.
For more information, see Consquences.
Show child attributes
Show child attributes
Determines whether promoted records must also match active filters for the consequence to apply.This ensures user-applied filters take priority and irrelevant matches aren’t shown.
For example, if you promote a record with color: red but the user filters for color: blue,
the “red” record won’t be shown.
List of objects with
objectID
properties for records you want to hide.Example:JSON
Report incorrect code
Copy
{
"consequence": {
"hide": [
{
"objectID": "test-record-123"
}
]
}
}
Fix positions of records in the search results.
You can promote up to 300 records per rule.
Show child attributes
Show child attributes
Object ID of the record to promote.
Either
objectID
or objectIDs
is required.Object IDs of a group of records to promote.
A group can have up to 100 records.
Either
objectID
or objectIDs
is required.The records are placed as a group at position
.
For example, if you want to promote four records to position 0,
they will be the first four search results.Position in the search results where you want to show the promoted records,
starting with 0.
Parameters to apply to this search.
You can use all search parameters,
plus the special fields
query
, automaticFacetFilters
, and automaticOptionalFacetFilters
.Show child attributes
Show child attributes
Facet filters to apply to this search.
You can use this to respond to search queries that match a facet value.
For example, if users search for “comedy”, which matches a facet value of the “genre” facet,
you can filter the results to show the top-ranked comedy movies.
Show child attributes
Show child attributes
Facet name. It must match the placeholder used in the
patterns
parameter of the rule’s condition.
For example, with pattern: {facet:genre}
, automaticFacetFilters.facet
must be genre
.Whether multiple matching filter conditions should be combined with
OR
.
By default, multiple matching filter conditions are combined with AND
.Filter scores to give different weights to individual filters.
Optional filters to apply to this search.
This is the same as
automaticFacetFilters
,
but with optionalFilters
:
records that don’t match the filters will be ranked below matching records.If
query
is a string, it replaces the entire search query.
If query
is an object, it describes the incremental edits made to the query.Show child attributes
Show child attributes
Words to delete from the query.
Changes to make to the query.
-
Remove words or patterns from the query:
(Example: remove “red” from the query)
JSONReport incorrect codeCopy
{ "query": { "edits": [ { "type": "remove", "delete": "red" } ] } }
-
Replace words or patterns in the query:
(Example: replace “red” with “blue”)
JSONReport incorrect codeCopy
{ "query": { "edits": [ { "type": "replace", "delete": "red", "insert": "blue" } ] } }
A JSON object with custom data that will be appended to the
userData
array in the search response.
This object isn’t interpreted by the API and is limited to 1 kB of minified JSON.Unique identifier of the rule object.
List of conditions that should trigger this rule.
You can add up to 25 conditions per rule.
Show child attributes
Show child attributes
Whether the pattern should match plurals, synonyms, and typos.
For example, if
pattern
is shoe
, and alternatives
is true,
the rule is also triggered when the query is shoes
, or sheo
(typo).For each word in pattern
, Algolia checks up to 10 alternatives.If the total number of alternatives is more than 10,
Algolia may not check some or even any of the words.
Which part of the query pattern must match to trigger the rule:
contains
: the pattern can match anywhere in the queryis
: the pattern must exactly match the queryendsWith
: the pattern must match the end of the querystartsWith
: the pattern must match the beginning of the query
is
.An additional restriction that only triggers the rule, when the search has the same value as
ruleContexts
parameter.
For example, if context: "mobile"
,
the rule is only triggered when the search request has a matching ruleContexts: "mobile"
.
A rule context must only contain alphanumeric characters.Filters that trigger the rule.You can add filters using the syntax
facet:value
so that the rule is triggered, when the specific filter is selected.
You can use filters on its own or combine it with the pattern parameter.
You can’t combine multiple filters with OR
and you can’t use numeric filters.Query pattern that triggers the rule.You can use either a literal string, or a special pattern
{facet:ATTRIBUTE}
, where ATTRIBUTE
is a facet name.
The rule is triggered if the query matches the literal string or a value of the specified facet.
For example, with pattern: {facet:genre}
,
the rule is triggered when users search for a genre, such as “comedy”.When specifying a query pattern, you must also specify an anchoring
.Descriptive text, that helps you find the rule when searching and understand its purpose.
Whether the rule is active.
If
false
, the rule is still added to the index but it’s not applied when searching.Whether to add or update the rule for all replicas of the index.
Response
Date at which the indexing job has been created.
Response as JSON
This section shows the JSON response returned by the API. Each API client wraps this response in language-specific objects, so the structure may vary. To view the response, use thegetLogs
method.
Don’t rely on the order of properties—JSON objects don’t preserve key order.
JSON
Report incorrect code
Copy
{
"updatedAt":"2013-01-18T15:33:13.556Z",
"taskID": 678
}