Skip to content

Instantly share code, notes, and snippets.

@Yujiro3
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yujiro3/1ce4908b191cfaf77b5d to your computer and use it in GitHub Desktop.
Save Yujiro3/1ce4908b191cfaf77b5d to your computer and use it in GitHub Desktop.
マイクロブログサンプルをPHPでやってみた
<?php
exec('rm -rf ./db; mkdir ./db');
/* DB接続 */
$gdb = new Groonga('./db/test.db');
/* table_create --name Users --flags TABLE_HASH_KEY --key_type ShortText */
$gdb->table('Users')
->flags('TABLE_HASH_KEY')
->keyType('ShortText')
->create();
/* table_create --name Comments --flags TABLE_HASH_KEY --key_type ShortText */
$gdb->table('Comments')
->flags('TABLE_HASH_KEY')
->keyType('ShortText')
->create();
/* table_create --name HashTags --flags TABLE_HASH_KEY --key_type ShortText */
$gdb->table('HashTags')
->flags('TABLE_HASH_KEY')
->keyType('ShortText')
->create();
/* table_create --name Bigram --flags TABLE_PAT_KEY|KEY_NORMALIZE --key_type ShortText --default_tokenizer TokenBigram */
$gdb->table('Bigram')
->flags('TABLE_PAT_KEY|KEY_NORMALIZE')
->keyType('ShortText')
->defaultTokenizer('TokenBigram')
->create();
/* table_create --name GeoIndex --flags TABLE_PAT_KEY --key_type WGS84GeoPoint */
$gdb->table('GeoIndex')
->flags('TABLE_PAT_KEY')
->keyType('WGS84GeoPoint')
->create();
/* column_create --table Users --name name --flags COLUMN_SCALAR --type ShortText */
$gdb->table('Users')
->column('name')
->flags('COLUMN_SCALAR')
->type('ShortText')
->create();
/* column_create --table Users --name follower --flags COLUMN_VECTOR --type Users */
$gdb->table('Users')
->column('follower')
->flags('COLUMN_SCALAR')
->type('Users')
->create();
/* column_create --table Users --name favorites --flags COLUMN_VECTOR --type Comments */
$gdb->table('Users')
->column('favorites')
->flags('COLUMN_VECTOR')
->type('Comments')
->create();
/* column_create --table Users --name location --flags COLUMN_SCALAR --type WGS84GeoPoint */
$gdb->table('Users')
->column('location')
->flags('COLUMN_SCALAR')
->type('WGS84GeoPoint')
->create();
/* column_create --table Users --name location_str --flags COLUMN_SCALAR --type ShortText */
$gdb->table('Users')
->column('location_str')
->flags('COLUMN_SCALAR')
->type('ShortText')
->create();
/* column_create --table Users --name description --flags COLUMN_SCALAR --type ShortText */
$gdb->table('Users')
->column('description')
->flags('COLUMN_SCALAR')
->type('ShortText')
->create();
/* column_create --table Users --name followee --flags COLUMN_INDEX --type Users --source follower */
$gdb->table('Users')
->column('followee')
->flags('COLUMN_INDEX')
->type('Users')
->source('follower')
->create();
/* column_create --table Comments --name comment --flags COLUMN_SCALAR --type ShortText */
$gdb->table('Comments')
->column('comment')
->flags('COLUMN_SCALAR')
->type('ShortText')
->create();
/* column_create --table Comments --name last_modified --flags COLUMN_SCALAR --type Time */
$gdb->table('Comments')
->column('last_modified')
->flags('COLUMN_SCALAR')
->type('Time')
->create();
/* column_create --table Comments --name replied_to --flags COLUMN_SCALAR --type Comments */
$gdb->table('Comments')
->column('replied_to')
->flags('COLUMN_SCALAR')
->type('Comments')
->create();
/* column_create --table Comments --name replied_users --flags COLUMN_VECTOR --type Users */
$gdb->table('Comments')
->column('replied_users')
->flags('COLUMN_VECTOR')
->type('Users')
->create();
/* column_create --table Comments --name hash_tags --flags COLUMN_VECTOR --type HashTags */
$gdb->table('Comments')
->column('hash_tags')
->flags('COLUMN_VECTOR')
->type('HashTags')
->create();
/* column_create --table Comments --name location --flags COLUMN_SCALAR --type WGS84GeoPoint */
$gdb->table('Comments')
->column('location')
->flags('COLUMN_SCALAR')
->type('WGS84GeoPoint')
->create();
/* column_create --table Comments --name posted_by --flags COLUMN_SCALAR --type Users */
$gdb->table('Comments')
->column('posted_by')
->flags('COLUMN_SCALAR')
->type('Users')
->create();
/* column_create --table Comments --name favorited_by --flags COLUMN_INDEX --type Users --source favorites */
$gdb->table('Comments')
->column('favorited_by')
->flags('COLUMN_INDEX')
->type('Users')
->source('favorites')
->create();
/* column_create --table HashTags --name hash_index --flags COLUMN_INDEX --type Comments --source hash_tags */
$gdb->table('HashTags')
->column('hash_index')
->flags('COLUMN_INDEX')
->type('Comments')
->source('hash_tags')
->create();
/* column_create --table Bigram --name users_index --flags COLUMN_INDEX|WITH_POSITION|WITH_SECTION --type Users --source name,location_str,description */
$gdb->table('Bigram')
->column('users_index')
->flags('COLUMN_INDEX|WITH_POSITION|WITH_SECTION')
->type('Users')
->source('name,location_str,description')
->create();
/* column_create --table Bigram --name comment_index --flags COLUMN_INDEX|WITH_POSITION --type Comments --source comment */
$gdb->table('Bigram')
->column('comment_index')
->flags('COLUMN_INDEX|WITH_POSITION')
->type('Comments')
->source('comment')
->create();
/* column_create --table GeoIndex --name users_location --type Users --flags COLUMN_INDEX --source location */
$gdb->table('GeoIndex')
->column('users_location')
->flags('COLUMN_INDEX')
->type('Users')
->source('location')
->create();
/* column_create --table GeoIndex --name comments_location --type Comments --flags COLUMN_INDEX --source location */
$gdb->table('GeoIndex')
->column('comments_location')
->flags('COLUMN_INDEX')
->type('Comments')
->source('location')
->create();
/* load --table Users --values [[...],[...],...] */
$values = <<< JSON
[
{
"_key": "alice",
"name": "Alice",
"follower": ["bob"],
"favorites": [],
"location": "152489000x-255829000",
"location_str": "Boston, Massachusetts",
"description": "Groonga developer"
},
{
"_key": "bob",
"name": "Bob",
"follower": ["alice","charlie"],
"favorites": ["alice:1","charlie:1"],
"location": "146249000x-266228000",
"location_str": "Brooklyn, New York City",
"description": ""
},
{
"_key": "charlie",
"name": "Charlie",
"follower": ["alice","bob"],
"favorites": ["alice:1","bob:1"],
"location": "146607190x-267021260",
"location_str": "Newark, New Jersey",
"description": "Hmm,Hmm"
}
]
JSON;
$gdb->table('Users')
->load($values);
/* $load --table Comments --values [[...],[...],...] */
$values = <<< JSON
[
{
"_key": "alice:1",
"comment": "I've created micro-blog!",
"last_modified": "2010/03/17 12:05:00",
"posted_by": "alice",
},
{
"_key": "bob:1",
"comment": "First post. test,test...",
"last_modified": "2010/03/17 12:00:00",
"posted_by": "bob",
},
{
"_key": "alice:2",
"comment": "@bob Welcome!!!",
"last_modified": "2010/03/17 12:05:00",
"replied_to": "bob:1",
"replied_users": ["bob"],
"posted_by": "alice",
},
{
"_key": "bob:2",
"comment": "@alice Thanks!",
"last_modified": "2010/03/17 13:00:00",
"replied_to": "alice:2",
"replied_users": ["alice"],
"posted_by": "bob",
},
{
"_key": "bob:3",
"comment": "I've just used 'Try-Groonga' now! #groonga",
"last_modified": "2010/03/17 14:00:00",
"hash_tags": ["groonga"],
"location": "146566000x-266422000",
"posted_by": "bob",
},
{
"_key": "bob:4",
"comment": "I'm come at city of New York for development camp! #groonga #travel",
"last_modified": "2010/03/17 14:05:00",
"hash_tags": ["groonga", "travel"],
"location": "146566000x-266422000",
"posted_by": "bob",
},
{
"_key": "charlie:1",
"comment": "@alice @bob I've tried to register!",
"last_modified": "2010/03/17 15:00:00",
"replied_users": ["alice", "bob"],
"location": "146607190x-267021260",
"posted_by": "charlie",
}
{
"_key": "charlie:2",
"comment": "I'm at the Museum of Modern Art in NY now!",
"last_modified": "2010/03/17 15:05:00",
"location": "146741340x-266319590",
"posted_by": "charlie",
}
]
JSON;
$gdb->table('Comments')
->load($values);
/* select --table Users --match_columns name,location_str,description --query "New York" --output_columns _key,name */
$result = $gdb->table('Users')
->select()
->matchColumns('name,location_str,description')
->query('"New York"')
->outputColumns('_key,name')
->exec(true);
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_BIGINT_AS_STRING);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment