> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Search rules

> Search for rules matching various criteria.

export const Legacy = ({title, href}) => {
  return <Note>

    This page documents an earlier version of the API client.
    For the latest version, see <a href={href}>{title}</a>.

    </Note>;
};

<Legacy title="Search for rules" href="/doc/libraries/sdk/methods/search/search-rules" />

**Required ACL:** `settings`

## Examples

<CodeGroup>
  ```cs C# theme={"system"}
  index.SearchRules( new RuleQuery("query")
    {
      Anchoring = "is",
      Context = "something",
      Page = 1,
      HitsPerPage = 10
    }
  );

  // Asynchronous
  await index.SearchRulesAsync( new RuleQuery("query")
    {
      Anchoring = "is",
      Context = "something",
      Page = 1,
      HitsPerPage = 10
    }
  );
  ```

  ```go Go theme={"system"}
  opts := []interface{}{
  	opt.Anchoring("is"),
  	opt.RuleContexts("something"),
  	opt.Page(1),
  	opt.HitsPerPage(10),
  }

  res, err := index.SearchRules("query", opts...)
  ```

  ```java Java theme={"system"}
  // Sync
  SearchResult<Rule> results = index.searchRules(
    new RuleQuery("something")
  );

  // Async
  CompletableFuture<SearchResult<Rule>> results = index.searchRulesAsync(
    new RuleQuery("something")
  );
  ```

  ```js JavaScript theme={"system"}
  // Search for rules containing the word "smartphone".
  index.searchRules("smartphone").then(({ hits }) => {
    console.log(hits);
  });
  ```

  ```kotlin Kotlin theme={"system"}
  val rule = RuleQuery(
      anchoring = Anchoring.Is,
      page = 1,
      hitsPerPage = 10,
      context = "something"
  )

  index.searchRules(rule)
  ```

  ```php PHP theme={"system"}
  $response = $index->searchRules('something', [
    'enabled' => true
  ]);
  ```

  ```python Python theme={"system"}
  # Search for rules containing the word "something".
  results = index.search_rules("something")
  ```

  ```ruby Ruby theme={"system"}
  # Search for rules containing the word "something".
  results = index.search_rules("something")
  ```

  ```scala Scala theme={"system"}
  var results = client.execute {
    search rules in index "index_name" query QueryRules(query = "something")
  }
  ```

  ```swift Swift theme={"system"}
  let query = RuleQuery()
    .set(\.anchoring, to: .is)
    .set(\.page, to: 1)
    .set(\.hitsPerPage, to: 10)
    .set(\.context, to: "something")

  index.searchRules(query) { result in
    if case .success(let response) = result {
      print("Response: \(response)")
    }
  }
  ```
</CodeGroup>

## Parameters

<ParamField body="anchoring" type="enum<string>">
  When specified, restricts matches to rules with a specific anchoring type.
  When omitted, all anchoring types may match.
  One of:

  * `contains`
  * `endsWith`
  * `is`
  * `startsWith`
</ParamField>

<ParamField body="context" type="string">
  Restricts matches to contextual rules with a specific context (exact match).
</ParamField>

<ParamField body="enabled" type="boolean">
  When specified, restricts matches to rules with a specific `enabled` status.
  When absent (default), all rules are retrieved, regardless of their `enabled` status.
</ParamField>

<ParamField body="hitsPerPage" type="integer" default={20}>
  Maximum number of hits in a page. Minimum is 1, maximum is 1000.
</ParamField>

<ParamField body="page" type="integer" default={0}>
  Requested page (zero-based).
</ParamField>

<ParamField body="query" type="string">
  Search query
</ParamField>

<ParamField body="requestOptions" type="object">
  A mapping of request options to send along with the request.
</ParamField>

## Response

<ResponseField name="hits" type="object[]">
  Rule objects matched by the search query (see [`rule`](/doc/libraries/sdk/v1/methods/save-rule#param-rule)).
  Highlighting is provided through the standard mechanism (`_highlightResult` attribute inside every returned object)
</ResponseField>

<ResponseField name="nbHits" type="integer">
  Total number of rules matching the query.
</ResponseField>

<ResponseField name="nbPages" type="integer">
  Total number of pages.
</ResponseField>

<ResponseField name="page" type="integer">
  Returned page number (zero-based).
</ResponseField>

### 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.

```jsonc JSON icon=braces theme={"system"}
{
  "hits":[
    {
      "conditions":[{
        "pattern":"smartphone",
        "anchoring":"contains"
      }],
      "consequence":{
        "params":{
          "filters":"category = 1"
        }
      },
      "objectID":"a-rule-id",
      "_highlightResult":{
        "conditions":[{
          "pattern":{
            "value":"<b>s<\/b>martphone",
            "matchLevel":"full",
            "fullyHighlighted":false,
            "matchedWords":[
              "s"
            ]
          }],
          "anchoring":{
            "value":"contains",
            "matchLevel":"none",
            "matchedWords":[

            ]
          }
        },
        "consequence":{
          "params":{
            "filters":{
              "value":"category = 1",
              "matchLevel":"none",
              "matchedWords":[

              ]
            }
          }
        }
      }
    }
  ],
  "nbHits":1,
  "page":0,
  "nbPages":1
}
```
