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

# Detect keywords

> Detect keywords and reserved words in the query using Algolia Rules

export const SearchQuery = () => <Tooltip tip="The text users enter into a search box. In the Search API, this corresponds to the query parameter. A search query is often used with filters, facets, and other parameters, but these aren't part of the query text itself.">
    search query
  </Tooltip>;

export const Index = () => <Tooltip tip="An Algolia index is a searchable dataset that consists of records and configuration settings. These settings define how the records are searched and ranked.">
    index
  </Tooltip>;

You can use rules to detect a specific word and modify a <SearchQuery /> parameter because of this word.

## Limit the search to only a subset of attributes

A good example of altering a query is with an online document library that allows keyword searches inside documents.
If a user types in the word "article ref21", they're probably looking for an article whose title or ID contains "ref21".\_

### Rule

*If query = "article ref21" then remove article and search for an article whose title or ID contains 'ref21'*

### API

To run the code examples on this page, [install the latest API client](/doc/libraries/sdk/install).

<CodeGroup>
  ```cs C# theme={"system"}
  var response = await client.SaveRuleAsync(
    "INDEX_NAME",
    "article-rule",
    new Rule
    {
      ObjectID = "article-rule",
      Conditions = new List<Condition>
      {
        new Condition { Pattern = "article", Anchoring = Enum.Parse<Anchoring>("StartsWith") },
      },
      Consequence = new Consequence
      {
        Params = new ConsequenceParams
        {
          Query = new ConsequenceQuery(
            new ConsequenceQueryObject
            {
              Edits = new List<Edit>
              {
                new Edit { Type = Enum.Parse<EditType>("Remove"), Delete = "article" },
              },
            }
          ),
          RestrictSearchableAttributes = new List<string> { "title", "book_id" },
        },
      },
    }
  );
  ```

  ```dart Dart theme={"system"}
  final response = await client.saveRule(
    indexName: "INDEX_NAME",
    objectID: "article-rule",
    rule: Rule(
      objectID: "article-rule",
      conditions: [
        Condition(
          pattern: "article",
          anchoring: Anchoring.fromJson("startsWith"),
        ),
      ],
      consequence: Consequence(
        params: ConsequenceParams(
          query: ConsequenceQueryObject(
            edits: [
              Edit(
                type: EditType.fromJson("remove"),
                delete: "article",
              ),
            ],
          ),
          restrictSearchableAttributes: [
            "title",
            "book_id",
          ],
        ),
      ),
    ),
  );
  ```

  ```go Go theme={"system"}
  response, err := client.SaveRule(client.NewApiSaveRuleRequest(
    "INDEX_NAME", "article-rule",
    search.NewEmptyRule().SetObjectID("article-rule").SetConditions(
      []search.Condition{*search.NewEmptyCondition().SetPattern("article").SetAnchoring(search.Anchoring("startsWith"))}).SetConsequence(
      search.NewEmptyConsequence().SetParams(
        search.NewEmptyConsequenceParams().SetQuery(search.ConsequenceQueryObjectAsConsequenceQuery(
          search.NewEmptyConsequenceQueryObject().SetEdits(
            []search.Edit{
              *search.NewEmptyEdit().SetType(search.EditType("remove")).SetDelete("article"),
            }),
        )).SetRestrictSearchableAttributes(
          []string{"title", "book_id"})))))
  if err != nil {
    // handle the eventual error
    panic(err)
  }
  ```

  ```java Java theme={"system"}
  UpdatedAtResponse response = client.saveRule(
    "INDEX_NAME",
    "article-rule",
    new Rule()
      .setObjectID("article-rule")
      .setConditions(Arrays.asList(new Condition().setPattern("article").setAnchoring(Anchoring.STARTS_WITH)))
      .setConsequence(
        new Consequence().setParams(
          new ConsequenceParams()
            .setQuery(new ConsequenceQueryObject().setEdits(Arrays.asList(new Edit().setType(EditType.REMOVE).setDelete("article"))))
            .setRestrictSearchableAttributes(Arrays.asList("title", "book_id"))
        )
      )
  );
  ```

  ```js JavaScript theme={"system"}
  const response = await client.saveRule({
    indexName: 'indexName',
    objectID: 'article-rule',
    rule: {
      objectID: 'article-rule',
      conditions: [{ pattern: 'article', anchoring: 'startsWith' }],
      consequence: {
        params: {
          query: { edits: [{ type: 'remove', delete: 'article' }] },
          restrictSearchableAttributes: ['title', 'book_id'],
        },
      },
    },
  });
  ```

  ```kotlin Kotlin theme={"system"}
  var response =
    client.saveRule(
      indexName = "INDEX_NAME",
      objectID = "article-rule",
      rule =
        Rule(
          objectID = "article-rule",
          conditions =
            listOf(
              Condition(
                pattern = "article",
                anchoring = Anchoring.entries.first { it.value == "startsWith" },
              )
            ),
          consequence =
            Consequence(
              params =
                ConsequenceParams(
                  query =
                    ConsequenceQueryObject(
                      edits =
                        listOf(
                          Edit(
                            type = EditType.entries.first { it.value == "remove" },
                            delete = "article",
                          )
                        )
                    ),
                  restrictSearchableAttributes = listOf("title", "book_id"),
                )
            ),
        ),
    )
  ```

  ```php PHP theme={"system"}
  $response = $client->saveRule(
      'INDEX_NAME',
      'article-rule',
      ['objectID' => 'article-rule',
          'conditions' => [
              ['pattern' => 'article',
                  'anchoring' => 'startsWith',
              ],
          ],
          'consequence' => ['params' => ['query' => ['edits' => [
              ['type' => 'remove',
                  'delete' => 'article',
              ],
          ],
          ],
              'restrictSearchableAttributes' => [
                  'title',

                  'book_id',
              ],
          ],
          ],
      ],
  );
  ```

  ```python Python theme={"system"}
  response = client.save_rule(
      index_name="INDEX_NAME",
      object_id="article-rule",
      rule={
          "objectID": "article-rule",
          "conditions": [
              {
                  "pattern": "article",
                  "anchoring": "startsWith",
              },
          ],
          "consequence": {
              "params": {
                  "query": {
                      "edits": [
                          {
                              "type": "remove",
                              "delete": "article",
                          },
                      ],
                  },
                  "restrictSearchableAttributes": [
                      "title",
                      "book_id",
                  ],
              },
          },
      },
  )
  ```

  ```ruby Ruby theme={"system"}
  response = client.save_rule(
    "INDEX_NAME",
    "article-rule",
    Algolia::Search::Rule.new(
      algolia_object_id: "article-rule",
      conditions: [Algolia::Search::Condition.new(pattern: "article", anchoring: "startsWith")],
      consequence: Algolia::Search::Consequence.new(
        params: Algolia::Search::ConsequenceParams.new(
          query: Algolia::Search::ConsequenceQueryObject.new(
            edits: [Algolia::Search::Edit.new(type: "remove", delete: "article")]
          ),
          restrict_searchable_attributes: ["title", "book_id"]
        )
      )
    )
  )
  ```

  ```scala Scala theme={"system"}
  val response = Await.result(
    client.saveRule(
      indexName = "INDEX_NAME",
      objectID = "article-rule",
      rule = Rule(
        objectID = "article-rule",
        conditions = Some(
          Seq(
            Condition(
              pattern = Some("article"),
              anchoring = Some(Anchoring.withName("startsWith"))
            )
          )
        ),
        consequence = Consequence(
          params = Some(
            ConsequenceParams(
              query = Some(
                ConsequenceQueryObject(
                  edits = Some(
                    Seq(
                      Edit(
                        `type` = Some(EditType.withName("remove")),
                        delete = Some("article")
                      )
                    )
                  )
                )
              ),
              restrictSearchableAttributes = Some(Seq("title", "book_id"))
            )
          )
        )
      )
    ),
    Duration(100, "sec")
  )
  ```

  ```swift Swift theme={"system"}
  let response = try await client.saveRule(
      indexName: "INDEX_NAME",
      objectID: "article-rule",
      rule: Rule(
          objectID: "article-rule",
          conditions: [SearchCondition(pattern: "article", anchoring: SearchAnchoring.startsWith)],
          consequence: SearchConsequence(params: SearchConsequenceParams(
              restrictSearchableAttributes: ["title", "book_id"],
              query: SearchConsequenceQuery
                  .searchConsequenceQueryObject(SearchConsequenceQueryObject(edits: [SearchEdit(
                      type: SearchEditType.remove,
                      delete: "article"
                  )]))
          ))
      )
  )
  ```
</CodeGroup>

### Using the dashboard

You can also add your rules in your Algolia dashboard.

1. Select the **Search** product icon on your dashboard and then select your <Index />.
2. Select the **Rules** section from the left sidebar menu in the [Algolia dashboard](https://dashboard.algolia.com/rules).
3. Under the heading **Rules**, select the index you are adding a Rule to.
4. Click **New rule**.
5. Select **Create your first rule** or **New rule**. In the drop-down menu, click **Manual Editor**.
6. In the **Condition(s)** section, keep **Query** toggled on, select **Contains** in the drop-down menu, and enter "article" in the input field.
7. In the **Consequence(s)** section:

   * Click the **Add consequence** button and select **Add Query Parameter**.
   * In the input field that appears, enter the JSON search parameter you want to add. For example, `{ "restrictSearchableAttributes": ["title","id"] }`.
   * Click the **Add consequence** button again and select **Remove Word**.
   * Type or select  "article" in the input field.
8. Save your changes.
