LoginSignup
0

More than 5 years have passed since last update.

Groongaのbetween()関数の改善点

Last updated at Posted at 2016-12-15

Groonga Advent Calendar 2016 14日目は、Groonga 6.1.1でのbetween()関数の改善点を取り上げます。

between()とは?

between()関数はその名の通り、値の範囲を示します。selectコマンドの--filterオプションで指定する検索条件に記述すると、第1引数に指定されたカラムの値が第2引数以降で指定された範囲内に収まっているレコードだけを取り出すことができます。

Groonga 6.1.0までのバージョンではbetween()の第1引数には_key以外の任意のカラムを指定する必要があり、_keyに対する範囲指定での検索はできない仕様でした。Groonga 6.1.1ではこの点が改善され、_keyが整数または日時のテーブルであれば、_keyも対象のカラムに指定できるようになりました。

準備

実際の動作を見てみましょう。実験用のテーブルとして、主キーが時刻であるテーブル(ここでは例として、Webページの閲覧履歴を想定したテーブルにしてみました)を用意します。

$ cat dump.grn
table_create History TABLE_PAT_KEY Time
column_create History url COLUMN_SCALAR ShortText
column_create History title COLUMN_SCALAR ShortText
table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
load --table History
[
["_key","url","title"],
[1481799732,"http://example.org/","This is test record 1!"],
[1481799755,"http://example.net/","test record 2."],
[1481799811,"http://example.com/","test test record three."],
[1481799915,"http://example.net/afr","test record four."],
[1481800003,"http://example.org/aba","test test test record five."],
[1481800013,"http://example.com/rab","test test test test record six."],
[1481800204,"http://example.net/atv","test test test record seven."],
[1481801705,"http://example.org/gat","test test record eight."],
[1481803448,"http://example.com/vdw","test test record nine."]
]
column_create Terms history_title COLUMN_INDEX|WITH_POSITION History title
column_create Terms history_url COLUMN_INDEX|WITH_POSITION History url
$ rm -rf /tmp/groonga-databases
$ mkdir -p /tmp/groonga-databases
$ groonga -n /tmp/groonga-databases/introduction.db quit
$ cat dump.grn | groonga /tmp/groonga-databases/introduction.db

実際の動作

それでは早速試してみましょう。--filterbetween(_key, 1481799800, "include", 1481800000, "include")という検索条件を指定して検索してみます。

$ groonga groonga-databases/introduction.db \
    select --table History \
           --filter '"between\(_key, 1481799800, \"include\", 1481800000, \"include\"\)"' |
    jq .
[
  [
    0,
    1481800116.779398,
    0.001013755798339844
  ],
  [
    [
      [
        2
      ],
      [
        [
          "_id",
          "UInt32"
        ],
        [
          "_key",
          "Time"
        ],
        [
          "title",Groonga 6.1.1での`between()`関数の改善点
          "ShortText"
        ],
        [
          "url",
          "ShortText"
        ]
      ],
      [
        3,
        1481799811,
        "test test record three.",
        "http://example.com/"
      ],
      [
        4,
        1481799915,
        "test record four.",
        "http://example.net/afr"
      ]
    ]
  ]
]

無事、主キーの時刻が指定の範囲内に収まっているレコードだけが検索されました。もちろん他の検索条件との併用も可能です。

以上、Groonga Advent Calendar 2016 14日目としてGroonga 6.1.1でのbetween()関数の改善点ついて述べました。引き続きGroonga Advent Calendarをお楽しみ下さい!

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0