Skip to content

Instantly share code, notes, and snippets.

@opencubicles
Created February 3, 2017 07:13
Show Gist options
  • Save opencubicles/ccb65849b426235feb14e5e3de44cd91 to your computer and use it in GitHub Desktop.
Save opencubicles/ccb65849b426235feb14e5e3de44cd91 to your computer and use it in GitHub Desktop.
elastic search - hunspell example
<?php
require 'vendor/autoload.php';
use Elasticsearch;
use Elasticsearch\ClientBuilder;
use Elasticsearch\Connections\Connection;
use Faker;
$client = Elasticsearch\ClientBuilder::create()->setHosts(['http://ec2-35-154-104-170.ap-south-1.compute.amazonaws.com:9200'])->build();
$faker = Faker\Factory::create();
$deleteParams = [
'index' => 'my_index2'
];
$response = $client->indices()->delete($deleteParams);
$params = [
'index' => 'my_index2',
'body' => [
'mappings' => [
'my_type2' => [
'_source' => [
'enabled' => true
],
'_all' => ['analyzer' => "my_analyzer"],
'properties' => [
'cliptexts' => [
'type' => 'nested',
"include_in_parent" => true,
'properties' => [
'content' => ['type' => 'string', "analyzer" => "my_analyzer"],
'cliptextid' => ['type' => 'string']
]
],
'uid' => [
'type' => 'integer'
],
'text' => [
"type" => "string", "analyzer" => "my_analyzer"
]
]
]
],
"settings"=> [
"analysis"=> [
"analyzer"=> [
"my_analyzer"=> [
"type"=> "custom",
"tokenizer"=> "standard",
"filter"=> ["lowercase", "en_US", "my_stopwords"]
]
],
"filter"=> [
"my_stopwords"=> [
"type"=> "stop",
"stopwords"=> "they,this,to,was,will,with"
]
]
]
]
]
];
// Create the index with mappings and settings now
$response = $client->indices()->create($params);
for ($j=0; $j < 100; $j++) {
$uids = array(27, 29, 31, 12, 19);
$uid = $uids[array_rand($uids,1)];
$id = $j;
$params = [
'index' => 'my_index2',
'type' => 'my_type2',
'id' => 'my_id'.$id,
'body' => ['uid' => $uid ]
];
$rand1 = rand(1,14);
$rand2 = rand(1,14);
for ($i=0; $i < 15; $i++) {
$content = $faker->sentence(12, true);
if($i == $rand1)
$content = $content." carry eat";
if($i == $rand2 && $rand1 != $rand2)
$content = $content." carries meeeet eat ";
$cliptextid = $i;
$params['body']['cliptexts'][$i] = array('content' => $content." women", 'cliptextid' => $cliptextid );
}
$params['body']['text'] = $content." women";
$response = $client->index($params);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment