Created
October 26, 2017 06:57
-
-
Save mathewjosephh/b84140f8a441bef4dd753dd899110493 to your computer and use it in GitHub Desktop.
Index Mapping ElasticSearch
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
function create_update_delete_index($arr_input) { | |
$options = get_option('post_types'); | |
foreach($this->post_types as $key => $value){ | |
$options[$key] = (isset($arr_input[$key]) && !empty($arr_input[$key])) ? 1: 0; | |
$index_params = array(); | |
$index_names = array(); | |
$index_names = array('index' => $key); | |
$index_params = array('index' => $key, | |
'body' => [ | |
'settings' => [ | |
'number_of_shards' => 3, | |
'number_of_replicas' => 1, | |
'analysis' => [ | |
"analyzer"=> [ | |
"index_analyzer"=> [ | |
"tokenizer"=> "standard", | |
"filter"=> ["standard", "my_delimiter", "lowercase", "stop", "asciifolding", "porter_stem"] | |
], | |
"search_analyzer"=> [ | |
"tokenizer"=> "standard", | |
"filter"=> ["standard", "lowercase", "stop", "asciifolding", "porter_stem"] | |
] | |
], | |
"filter"=> [ | |
"my_delimiter"=> [ | |
"type"=> "word_delimiter", | |
"generate_word_parts"=> true, | |
"catenate_words"=> true, | |
"catenate_numbers"=> true, | |
"catenate_all"=> true, | |
"split_on_case_change"=> true, | |
"preserve_original"=> true, | |
"split_on_numerics"=> true, | |
"stem_english_possessive"=> true | |
] | |
] | |
] | |
], | |
'mappings' => [ | |
$key => [ | |
'_source' => [ | |
'enabled' => true | |
], | |
'properties' => [ | |
'name' => [ | |
'type' => 'string', | |
'analyzer' => 'standard' | |
], | |
'description' => [ | |
'type' => 'string', | |
], | |
'slug' => [ | |
'type' => 'string', | |
], | |
'link' => [ | |
'type' => 'string', | |
], | |
'id' => [ | |
'type' => 'integer' | |
] | |
] | |
] | |
] | |
] | |
); | |
//Only If Checked. Add The Index | |
if($options[$key] == 1) { | |
// be sure that the index doesn't already exist | |
if ( !$this->client->indices()->exists($index_names) ) { | |
// create index of post type | |
$response = $this->client->indices()->create($index_params); | |
} | |
} | |
//If Not checked. Remove the Index | |
if($options[$key] == 0) { | |
// be sure that the index already exist | |
if ( $this->client->indices()->exists($index_names) ) { | |
// delete index of post type | |
$response = $this->client->indices()->delete($index_names); | |
} | |
} | |
} | |
return $options; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment