When dealing with data that you want to import into elasticsearch, one of the challenges is to store the data in a uniform way. This will make it easier for you to query your data. To store data in a uniform way you often need to transform the data. In this serie of blog posts we will discuss libraries that make you’re life easier when transformations are needed.
Parsing single phone number
The first thing that you need to do is parse the phone number. This results in a PhoneNumber object, which can be used to retrieve or remove information like country code. We need to provide a default region to the parse method. This will only be used if the number being parsed is not written in international format. The next code block shows an example of the parsing.
The phone number in the example is formatted correctly, but the library is smart enough to recognise other formats as well. So it doesn’t matter if you use white spaces, the country code (with or without plus sign) or any other combination.
Another option that this library provides is extracting phone numbers from text. Also for this method you need to provide a default region.
The returned iterable
phoneNumberMatches contains in this example two
PhoneNumberMatch object has a
PhoneNumber object which can be used for further actions.
With the created PhoneNumber object we can validate if this phone number is a correct phone number.
When the provided phone number does not contain the country code and is not recognised as a phone number from the specified default region, the
isValidNumber() method will return
Now that we have identified that we have a valid phone number, we can format it in a way we want to store it.
The outcome of the formatter will be
+31 88 586 4670.
The google phone number library contains a few more functionalities like matching numbers or provide geographical information related to a phone number. For more information check out there github page.
That’s it for the first part of libraries that can help you transform your data. Hopefully this can be handy in your project. See you next time!