How you store phone numbers in your index is tricky, because they can be queried in many different ways. For example:

  • 805-555-5555
  • 8055555555
  • +18055555555

We’ll take a look at two areas that can help you handle this problem.

  • Formatting the numbers
  • Configure the relevance

What we say about phone numbers can be equally applied to SKU, serial numbers, ISBN, and other such identifying attributes.

Formatting records

Use the most common format

You want to store all phone numbers in a format that users will use. How you store the number in your database will be different from how you format it in your index. Algolia is only concerned with users finding the record. If you’ve stored 805-555-5555 but all users are looking for 8055555555, you need to store 8055555555.

1
2
3
4
{
  [...],
  "phone_number": "8055555555"
}

Use all possible formats

We suggest, however, that you store all possible formats.

1
2
3
4
5
6
7
8
{
  [...],
  "phone_number": [
    "8055555555",
    "(805)-555-5555",
    "805-555-5555"
  ]
}

Note: Algolia cannot search numerics. Therefore, make sure that all phone numbers are stored as a string. The following record will not work.

1
2
3
4
{
  [...],
  "phone_number": 8055555555
}

One format for search and another for display

If users are searching in one format but want the number to display in another way, you need to store both forms in the record.

1
2
3
4
5
{
  [...],
  "phone_number": "8055555555",
  "phone_number_for_display": "(805)-555-5555"
}

Searching in the middle of phone numbers

You have two options.

1 - If you want users to search in part of the phone numbers, you can add space so every word can be search independently.

1
2
3
4
5
{
  [...],
  "phone_number": "805 555 5555",
  "phone_number_for_display": "(805)-555-5555"
}

2 - If you want to search every suffix, then you need to index all suffixes because algolia can only do prefix search.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  [...],
  "phone_number": "8051234567",
  "phone_number_suffixes": [
    "8051234567",
    "051234567",
    "1234567",
    "234567",
    "34567",
    "4567",
    "567",

  ]
}

Configuring the relevance

Configuring searchable attributes

We need to add the phone number attributes to the searchable attributes list.

1
2
3
4
5
6
7
index.setSettings({
  searchableAttributes: [
    ...,
    "phone_number",
    ...
  ]
})

Whether phone number is a string or an array of string this will work.

If you added alternative forms you should add it as well in the list:

1
2
3
4
5
6
7
8
index.setSettings({
  searchableAttributes: [
    ...,
    "phone_number",
    "phone_number_suffixes",
    ...
  ]
})

However, if you add a display format, this should not be added it to the list.

Phone numbers need to match exactly (without typos)

In that case we can disable typo tolerance on the phone number attribute.

1
2
3
4
5
index.set_settings({
  disableTypoToleranceOnAttributes: [
    'phone_number'
  ]
})

Phone numbers need to match entirely (without prefix)

1
2
3
4
5
index.set_settings({
  disablePrefixOnAttributes: [
    'phone_number'
  ]
})

Handling Signs

By default the dash (-) and plus (+) signs are ignored, because the engine treats them as separators. Whether they are in the query or in the index, they won’t be searched.

Therefore, searching for “+33” looks for all numbers in the index starting with “+33” or “33”, because the + is ignored.

To allow users to search with signs:

1
2
3
index.setSettings({
  separatorsToIndex: '-+'
})

Now, searching for “+33” will only look for phone numbers starting with “+33”. Numbers not containing “+33” won’t be found.

Did you find this page helpful?