Guides / Managing results / Optimize search results / Override engine defaults

Match queries in the middle or end of words

Algolia matches queries from the beginning of words (prefix matches), but it doesn’t match queries that appear in the middle (infix) or at the end (suffix). For example, searching for “note” matches “notepad” and “notebook”, but not “keynote”. This can cause issues when users search for a product reference without its internal prefix, such as searching “ABCDEF” for an item stored as “XXXABCDEF” in your index.

To support infix and suffix matching, generate every possible suffix of a string (for example, “ABCDEF”, “BCDEF”, “CDEF”, “DEF”, “EF”, “F”).

Create suffix attributes

In the following product dataset, the product_reference attribute has a numeric prefix, used for internal purposes.

1
2
3
4
5
6
7
8
9
10
[
  {
    "name": "Apple iPhone 16",
    "product_reference": "002ABCDEF"
  },
  {
    "name": "Apple iPhone 16 Plus",
    "product_reference": "001GHIJKL"
  }
]

The function shown below generates an attribute that contains all possible suffixes for product_reference, as an array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$objects = json_decode(file_get_contents('products.json'), true);

$objects = array_map(function ($record) {
  $reference = $record['product_reference'];

  $record['product_reference_suffixes'] = [];

  while (mb_strlen($reference) > 1) {
    $reference = substr($reference, 1);

    $record['product_reference_suffixes'][] = $reference;
  }

  return $record;
}, $objects);


$index->saveObjects($objects, ['autoGenerateObjectIDIfNotExist' => true]);

The processed records include a new attribute, product_reference_suffixes:

1
2
3
4
5
6
7
8
9
10
11
12
[
  {
    "name": "Apple iPhone 16",
    "product_reference": "002ABCDEF",
    "product_reference_suffixes": ["02ABCDEF", "2ABCDEF", "ABCDEF", "BCDEF", "CDEF", "DEF", "EF", "F"]
  },
  {
    "name": "Apple iPhone 16 Max",
    "product_reference": "001GHIJKL",
    "product_reference_suffixes": ["01GHIJKL", "1GHIJKL", "GHIJKL", "HIJKL", "IJKL", "JKL", "KL", "L"]
  }
]

Limitations

Generating a list of suffixes for each record can increase index size and slow performance, It may also reduce relevance for short suffixes such as “EF” or “F”, because these can match unrelated records with similar character sequences. Use this approach only if you genuinely need to query in the middle or the end of words.

To reduce the impact of suffix generation, consider the following strategies:

  • Generate suffixes only for strings longer than a defined length, such as 4 or more characters.
  • Limit suffix generation to a specific number per record, such as 5.
  • Apply suffix generation only to structured fields, such as product codes or identifiers.

Update your searchable attributes

Add product_reference_suffixes to your searchable attributes to make the suffixes searchable.

1
2
3
4
5
6
7
8
9
10
11
12
var response = await client.SetSettingsAsync(
  "ALGOLIA_INDEX_NAME",
  new IndexSettings
  {
    SearchableAttributes = new List<string>
    {
      "name",
      "product_reference",
      "product_reference_suffixes",
    },
  }
);

In your list of searchable attributes, place product_reference_suffixes below product_reference to exact matches rank higher than partial matches.

See also

Did you find this page helpful?