Hi everyone,
I'm using Elasticsearch PHP API to index an address database with multiple fields. And I use it for an autocomplete search. I base it on a fa field with this as the value "1 AVE Avenue NE Northeast Northeast AIRDRIE AB Alberta T4B0R5 T4B 0R5"
I see providing the mapping and search codes. I want to know if there is a better way to do this.
Thank you for your help.
mapping code
$params = [
'index' => 'canada_posts',
'body' => [
'mappings' => [
'_source' => [
'enabled' => true
],
'properties' => [
'fa' => ['type' => 'text'],
'sn' => ['type' => 'text'],
'sdc' => ['type' => 'text', 'index' => false],
'stc' => ['type' => 'text', 'index' => false],
'mn' => ['type' => 'text', 'fielddata' => True],
'zip' => ['type' => 'keyword'],
'pc' => ['type' => 'text', 'fielddata' => True],
'sansfc' => ['type' => 'text', 'index' => false],
'sanstc' => ['type' => 'text', 'index' => false],
'safn' => ['type' => 'integer'],
'satn' => ['type' => 'integer'],
'sasc' => ['type' => 'integer', 'index' => false],
'prov' => ['type' => 'long']
]
]
]
];
return $this->client->indices()->create($params);
search code
// REMOVE CONSECUTIVE NUMBER FROM STRING (WILL KEEP ZIP CODES INTACT)
$strWords = trim($this->accent2ascii(preg_replace('/^(\d+)+\s/', '', $strUppercaseWords)));
$len_Words = strlen($strWords);
$nb_words = count(explode(' ', $strWords));
$params2 = [
'index' => 'canada_posts',
'body' => [
'size' => 10,
'query' => [
'bool' => [
'must' => [
[ 'bool' => [
'must' => [
[ 'match' => ['fa' => [
'query' => $strWords,
'fuzziness' => ($nb_words > 1)?"AUTO:$len_Words,$len_Words":"0",
'minimum_should_match'=> $nb_words
]
]
]
],
]
]
],
]
],
'fields' => [
'dn',
'fa',
'mn',
'pc',
'prov',
'safn',
'sansfc',
'sanstc',
'sasc',
'satn',
'sdc',
'sn',
'stc',
'zip'
],
'sort' => [
['_score' => ['order' => 'desc']],
['mn' => ['order' => 'asc']],
['pc' => ['order' => 'desc']]
]
]
];
$query = $this->client->search($params2);