Skip to main content
This page documents an earlier version of the API client. For the latest version, see Create or replace a rule.
Required ACL: editSettings

Examples

Save a rule

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

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

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

rule
object
required
The rule object with conditions and consequences. For a complete JSON rule object, see the saveRule HTTP API reference.
forwardToReplicas
boolean
default:false
Whether to add or update the rule for all replicas of the index.

Response

taskID
integer
The task ID used with the waitTask method.
updatedAt
string
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 the getLogs method. Don’t rely on the order of properties—JSON objects don’t preserve key order.
JSON
{
  "updatedAt":"2013-01-18T15:33:13.556Z",
  "taskID": 678
}
I