Skip to content

Instantly share code, notes, and snippets.

@qrg
Created September 13, 2012 04:31
Show Gist options
  • Save qrg/3711857 to your computer and use it in GitHub Desktop.
Save qrg/3711857 to your computer and use it in GitHub Desktop.

4.1.1. データベースの作成

    $ groonga -n test_groonga.db
    > status
    [
        [0, 1347500074.58233, 5.86509704589844e-05],
        {
            "alloc_count":123,
            "starttime":1347500072,
            "uptime":2,
            "version":"2.0.6",
            "n_queries":0,
            "cache_hit_rate":0.0,
            "command_version":1,
            "default_command_version":1,
            "max_command_version":2
        }
    ]

4.1.5. テーブルの作成

> table_create --name Site --flags TABLE_HASH_KEY --key_type ShortText
[[0,1347500701.73932,0.00164556503295898],true]
> select --table Site
[
    [0, 1347500708.52882, 0.000185489654541016],
    [
        [
            [0],
            [
                ["_id", "UInt32"],
                ["_key", "ShortText"]
            ]
        ]
    ]
]
> column_create --table Site --name title --type ShortText
[[0,1347501558.51107,0.00177001953125],true]
> select --table Site
    [
        [0, 1347501581.51608, 0.000115394592285156],
        [
            [
                [0],
                [
                    ["_id", "UInt32"],
                    ["_key", "ShortText"],
                    ["title", "ShortText"]
                ]
            ]
        ]
    ]

4.1.8. データのロード

> load --table Site
    [
    {"_key":"http://example.org/","title":"This is test record 1!"},
    {"_key":"http://example.net/","title":"test record 2."},
    {"_key":"http://example.com/","title":"test test record three."},
    {"_key":"http://example.net/afr","title":"test record four."},
    {"_key":"http://example.org/aba","title":"test test test record five."},
    {"_key":"http://example.com/rab","title":"test test test test record six."},
    {"_key":"http://example.net/atv","title":"test test test record seven."},
    {"_key":"http://example.org/gat","title":"test test record eight."},
    {"_key":"http://example.com/vdw","title":"test test record nine."},
    ]
> select --table Site
[
    [0, 1347501796.50709, 0.000130176544189453],
    [
        [
            [9],
            [
                ["_id", "UInt32"],
                ["_key", "ShortText"],
                ["title", "ShortText"]
            ],
            [1, "http://example.org/", "This is test record 1!"],
            [2, "http://example.net/", "test record 2."],
            [3, "http://example.com/", "test test record three."],
            [4, "http://example.net/afr", "test record four."],
            [5, "http://example.org/aba", "test test test record five."],
            [6, "http://example.com/rab", "test test test test record six."],
            [7, "http://example.net/atv", "test test test record seven."],
            [8, "http://example.org/gat", "test test record eight."],
            [9, "http://example.com/vdw", "test test record nine."]
        ]
    ]
]

4.1.9. レコードの取得

> select --table Site --query _id:1
[
    [0, 1347502163.68553, 0.000391006469726562],
    [
        [
            [1],
            [
                ["_id", "UInt32"],
                ["_key", "ShortText"],
                ["title", "ShortText"]
            ],
            [1, "http://example.org/", "This is test record 1!"]
        ]
    ]
]
> select --table Site --query '_key:"http://example.org/"'
[
    [0, 1347502226.36438, 0.000367879867553711],
    [
        [
            [1],
            [
                ["_id", "UInt32"],
                ["_key", "ShortText"],
                ["title", "ShortText"]
            ],
            [1, "http://example.org/", "This is test record 1!"]
        ]
    ]
]

4.1.10. 全文検索用の語彙表の作成

> table_create --name Terms --flags TABLE_PAT_KEY|KEY_NORMALIZE --key_type ShortText --default_tokenizer TokenBigram
[[0,1347508153.43708,0.00155854225158691],true]

4.1.11. 全文検索用のインデックスカラムの作成

> column_create --table Terms --name blog_title --flags COLUMN_INDEX|WITH_POSITION --type Site --source title
[[0,1347508895.93488,0.011890172958374],true]
> select --table Terms
[
    [0, 1347509042.17153, 0.000188589096069336],
    [
        [
            [15],
            [
                ["_id", "UInt32"],
                ["_key", "ShortText"],
                ["blog_title", "Site"]
            ],
            [1, "!", 1],
            [2, ".", 2],
            [3, "1", 1],
            [4, "2", 1],
            [5, "eight", 1],
            [6, "five", 1],
            [7, "four", 1],
            [8, "is", 1],
            [9, "nine", 1],
            [10, "record", 2]
        ]
    ]
]

4.1.12. 全文検索

> select --table Site --query title:@this
[
    [0, 1347509278.61585, 0.000374317169189453],
    [
        [
            [1],
            [
                ["_id", "UInt32"],
                ["_key", "ShortText"],
                ["title", "ShortText"]
            ],
            [1, "http://example.org/", "This is test record 1!"]
        ]
    ]
]
> select --table Site --match_columns title --query this
[
    [0, 1347509328.40498, 0.000200748443603516],
    [
        [
            [1],
            [
                ["_id", "UInt32"],
                ["_key", "ShortText"],
                ["title", "ShortText"]
            ],
            [1, "http://example.org/", "This is test record 1!"]
        ]
    ]
]

ここから TokenMecab に変更して全文検索テスト

> table_remove --name Terms
[[0,1347510086.915,0.0014350414276123],true]

> table_create --name Terms --flags TABLE_PAT_KEY|KEY_NORMALIZE --key_type ShortText --default_tokenizer TokenMecab
[[0,1347510283.11351,0.002105712890625],true]

> column_create --table Terms --name blog_title --flags COLUMN_INDEX|WITH_POSITION --type Site --source title
[[0,1347510299.27291,0.00677371025085449],true]

> select --table Terms
[
    [0, 1347510420.35793, 6.36577606201172e-05],
    [
        [
            [16],
            [
                ["_id", "UInt32"],
                ["_key", "ShortText"],
                ["blog_title", "Site"]
            ],
            [1, "!", 1],
            [2, ".", 2],
            [3, "1", 1],
            [4, "2", 1],
            [5, "eight", 1],
            [6, "five", 1],
            [7, "four", 1],
            [8, "i", 1],
            [9, "nine", 1],
            [10, "record", 2]
        ]
    ]
]

全文検索

> select --table Site --query title:@this
[
    [0, 1347510545.72137, 0.000308990478515625],
    [
        [
            [1],
            [
                ["_id", "UInt32"],
                ["_key", "ShortText"],
                ["title", "ShortText"]
            ],
            [1, "http://example.org/", "This is test record 1!"]
        ]
    ]
]
> select --table Site --match_columns title --query this
[
    [0, 1347510622.211, 0.000203132629394531],
    [
        [
            [1],
            [
                ["_id", "UInt32"],
                ["_key", "ShortText"],
                ["title", "ShortText"]
            ],
            [1, "http://example.org/", "This is test record 1!"]
        ]
    ]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment