Last active
October 10, 2016 22:10
-
-
Save adamlepkowski/a5b01e7f8a2ea3d6cf5eb7a8f5f45bc8 to your computer and use it in GitHub Desktop.
Elasticsearch phone numer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELETE /phone_index | |
PUT /phone_index | |
{ | |
"settings": { | |
"number_of_shards": 1, | |
"analysis": { | |
"filter": { | |
"whitespace_remover": { | |
"type": "pattern_replace", | |
"pattern": " ", | |
"replacement": "" | |
}, | |
"punctuation_sign_remover": { | |
"type": "pattern_replace", | |
"pattern": "\\p{Punct}", | |
"replacement": "" | |
}, | |
"ngram_4_to_10_filter": { | |
"type": "nGram", | |
"min_gram": 4, | |
"max_gram": 10 | |
} | |
}, | |
"analyzer": { | |
"phone_storing_analyzer": { | |
"type": "custom", | |
"filter": [ | |
"punctuation_sign_remover", | |
"whitespace_remover", | |
"ngram_4_to_10_filter" | |
], | |
"tokenizer": "keyword" | |
}, | |
"phone_search_analyzer": { | |
"type": "custom", | |
"filter": [ | |
"punctuation_sign_remover", | |
"whitespace_remover" | |
], | |
"tokenizer": "keyword" | |
} | |
} | |
} | |
}, | |
"mappings": { | |
"phone": { | |
"properties": { | |
"number": { | |
"type": "string", | |
"analyzer": "phone_storing_analyzer", | |
"search_analyzer": "phone_search_analyzer" | |
} | |
} | |
} | |
} | |
} | |
# verify how phone_search_analyzer works | |
POST /phone_index/_analyze | |
{ | |
"analyzer": "phone_search_analyzer", | |
"text": "+44 (207) 86" | |
} | |
# verify how phone_storing_analyzer works | |
POST /phone_index/_analyze | |
{ | |
"analyzer": "phone_storing_analyzer", | |
"text": "+44 (207) 86" | |
} | |
# create test data | |
POST /phone_index/phone/_bulk | |
{ "index": { "_id": 1 }} | |
{ "number": "+44 207 8611417" } | |
{ "index": { "_id": 2 }} | |
{ "number": "+44 207 861 1417" } | |
{ "index": { "_id": 3 }} | |
{ "number": "+44 20 7861 1417" } | |
{ "index": { "_id": 4 }} | |
{ "number": "+(44) 20 7 8 6 1 1 4 1 7" } | |
# search phone numbers containing part of the number | |
GET /phone_index/phone/_search | |
{ | |
"query": { | |
"match": { | |
"number": "2 0 7 8" | |
} | |
} | |
} | |
# search by end of a number | |
GET /phone_index/phone/_search | |
{ | |
"query": { | |
"match": { | |
"number": "11417" | |
} | |
} | |
} | |
# search by beginning of a number | |
GET /phone_index/phone/_search | |
{ | |
"query": { | |
"match": { | |
"number": "+44 (20) 7" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment