def create_new_index(self, index):
        """Create user index and setups mappings."""
        log.warn('Creating new index {}'.format(index))

        try:
            self.es_client.indices.create(
                index=index,
                timeout='30s',
                body={
                    "settings": {
                        "index": {
                            "number_of_shards": 3,
                        },
                        "analysis": {
                            "analyzer": {
                                "text_analyzer": {
                                    "type": "custom",
                                    "tokenizer": "lowercase",
                                    "filter": [
                                        "ascii_folding"
                                    ]
                                },
                                "email_analyzer": {
                                    "type": "custom",
                                    "tokenizer": "email_tokenizer",
                                    "filter": [
                                        "ascii_folding"
                                    ]
                                }
                            },
                            "filter": {
                                "ascii_folding": {
                                    "type": "asciifolding",
                                    "preserve_original": True
                                }
                            },
                            "tokenizer": {
                                "email_tokenizer": {
                                    "type": "ngram",
                                    "min_gram": 3,
                                    "max_gram": 25
                                }
                            }
                        }
                    }
                })
        except Exception as exc:
            log.error("failed to create index {} : {}".format(index, exc))
            raise exc

        # PUT mappings for each type
        for kls in self.types:
            kls._index_class().create_mapping(index)

def
create_new_index
(
self
,
index
)
:
log
.
warn
(
'Creating new index {}'
.
format
(
index
)
)
try
:
self
.
es_client
.
indices
.
create
(
index
=
index
,
timeout
=
'30s'
,
body
=
{
"settings"
:
{
"index"
:
{
"number_of_shards"
:
3
,
}
,
"analysis"
:
{
"analyzer"
:
{
"text_analyzer"
:
{
"type"
:
"custom"
,
"tokenizer"
:
"lowercase"
,
"filter"
:
[
"ascii_folding"
]
}
,
"email_analyzer"
:
{
"type"
:
"custom"
,
"tokenizer"
:
"email_tokenizer"
,
"filter"
:
[
"ascii_folding"
]
}
}
,
"filter"
:
{
"ascii_folding"
:
{
"type"
:
"asciifolding"
,
"preserve_original"
:
True
}
}
,
"tokenizer"
:
{
"email_tokenizer"
:
{
"type"
:
"ngram"
,
"min_gram"
:
3
,
"max_gram"
:
25
}
}
}
}
}
)
except
Exception
as
exc
:
log
.
error
(
"failed to create index {} : {}".
f
ormat(
i
ndex,
xc)
)
raise
exc
# PUT mappings for each type
for
kls
in
self
.
types
:
kls
.
_index_class
(
)
.
create_mapping
(
index
)


def valid_uuid(uuid):
    """Validate uuid value using regex."""
    regex = re.compile('^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab]'
                       '[a-f0-9]{3}-?[a-f0-9]{12}\Z', re.I)
    match = regex.match(uuid)
    return bool(match)


Anonymise email field using an hash function .

def anonymise_email(email):
    """Anonymise email field using an hash function."""
    # XXX do a better email formatting, even if it's supposed to be clean ...
    assert '@' in email, 'Invalid email {0}'.format(email)
    local_part, domain = email.split('@')
    hash_local = hashlib.sha256(local_part.encode('utf-8')).hexdigest()
    hash_domain = hashlib.sha256(domain.encode('utf-8')).hexdigest()
    return '{0}@{1}'.format(hash_local, hash_domain)