Create an index (advanced example)
editCreate an index (advanced example)
editThis is a more complicated example of creating an index, showing how to define analyzers, tokenizers, filters and index settings. Although essentially the same as the previous example, the more complicated example can be helpful for "real world" usage of the client, since this particular syntax is easy to mess up.
For brevity, the example is presented using PHP 5.4+ short array syntax ([]
as opposed to array()
).
$params = [ 'index' => 'reuters', 'body' => [ 'settings' => [ 'number_of_shards' => 1, 'number_of_replicas' => 0, 'analysis' => [ 'filter' => [ 'shingle' => [ 'type' => 'shingle' ] ], 'char_filter' => [ 'pre_negs' => [ 'type' => 'pattern_replace', 'pattern' => '(\\w+)\\s+((?i:never|no|nothing|nowhere|noone|none|not|havent|hasnt|hadnt|cant|couldnt|shouldnt|wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint))\\b', 'replacement' => '~$1 $2' ], 'post_negs' => [ 'type' => 'pattern_replace', 'pattern' => '\\b((?i:never|no|nothing|nowhere|noone|none|not|havent|hasnt|hadnt|cant|couldnt|shouldnt|wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint))\\s+(\\w+)', 'replacement' => '$1 ~$2' ] ], 'analyzer' => [ 'reuters' => [ 'type' => 'custom', 'tokenizer' => 'standard', 'filter' => ['lowercase', 'stop', 'kstem'] ] ] ] ], 'mappings' => [ '_default_' => [ 'properties' => [ 'title' => [ 'type' => 'string', 'analyzer' => 'reuters', 'term_vector' => 'yes', 'copy_to' => 'combined' ], 'body' => [ 'type' => 'string', 'analyzer' => 'reuters', 'term_vector' => 'yes', 'copy_to' => 'combined' ], 'combined' => [ 'type' => 'string', 'analyzer' => 'reuters', 'term_vector' => 'yes' ], 'topics' => [ 'type' => 'string', 'index' => 'not_analyzed' ], 'places' => [ 'type' => 'string', 'index' => 'not_analyzed' ] ] ], 'my_type' => [ 'properties' => [ 'my_field' => [ 'type' => 'string' ] ] ] ] ] ]; $client->indices()->create($params);
The top level |
|
|
|
|
|
The |
|
The |