カラムストア機能付き全文検索エンジン Groonga

作成日 : 2019-03-10
最終更新日 :

前置き

全文検索エンジンとして Namazu や msearch を使ってきた。これらは WEB ページ専用であったが、 最近はテキスト全体を全文検索するツールが増えてきた。世界的に有名なのは elasticsearch であるが、 Java が必須である。私が使っているホスティングサービスでは Java が使えないので必然的に他のツールを探すことになる。 今回は Java を必要としない、C 言語で作成された、日本産の Groonga を使うことにした。 ちなみに、Groonga は「グルンガ」と読む。

Groonga(groonga.org)

チュートリアル「4.10. マイクロブログ検索システムの作成」の注釈

Groonga のインストール

ホスティングサーバで次をおこなった。ちなみに、このサーバは FreeBSD であるため、 Groonga が用意している固有の環境には当てはまらない。そのため、一般 UNIX の情報をもとにしている。 $HOME は私のホスティングサーバのホームがあるディレクトリである。

$[1] wget https://packages.groonga.org/source/groonga/groonga-9.0.0.tar.gz
$[2] tar xvzf groonga-9.0.0.tar.gz
$[3] cd groonga-9.0.0
$[4] ./configure --prefix=$HOME/local --localstatedir=$HOME/local/var --with-log-path=$HOME/local/var/log/groonga.log
$[5] make
$[6] make install

$[1] と $[2], $[3], $[4] は問題なく完了した。しかし、$[5] の段階で次の問題が発生した。

$[5] make
make all-recursive
Making all in build
Making all in cmake_modules
Making all in include
Making all in groonga
Making all in vendor
Making all in lz4
Making all in onigmo
cd ../onigmo-source && make all
make all-recursive
Making all in .
/bin/sh ./libtool --tag=CC --mode=compile cc -DHAVE_CONFIG_H -I. -I. -I$HOME/local/include -I./enc/unicode -Wall -g -O2 -MT regerror.lo -MD -MP -MF .deps/regerror.Tpo -c -o regerror.lo regerror.c
libtool: compile: cc -DHAVE_CONFIG_H -I. -I. -I$HOME/local/include -I./enc/unicode -Wall -g -O2 -MT regerror.lo -MD -MP -MF .deps/regerror.Tpo -c regerror.c -fPIC -DPIC -o regerror.o
mv -f .deps/regerror.Tpo .deps/regerror.Plo
/bin/sh ./libtool --tag=CC --mode=compile cc -DHAVE_CONFIG_H -I. -I. -I$HOME/local/include -I./enc/unicode -Wall -g -O2 -MT regparse.lo -MD -MP -MF .deps/regparse.Tpo -c -o regparse.lo regparse.c
libtool: compile: cc -DHAVE_CONFIG_H -I. -I. -I$HOME/local/include -I./enc/unicode -Wall -g -O2 -MT regparse.lo -MD -MP -MF .deps/regparse.Tpo -c regparse.c -fPIC -DPIC -o regparse.o
mv -f .deps/regparse.Tpo .deps/regparse.Plo
/bin/sh ./libtool --tag=CC --mode=compile cc -DHAVE_CONFIG_H -I. -I. -I$HOME/local/include -I./enc/unicode -Wall -g -O2 -MT regext.lo -MD -MP -MF .deps/regext.Tpo -c -o regext.lo regext.c
libtool: compile: cc -DHAVE_CONFIG_H -I. -I. -I$HOME/local/include -I./enc/unicode -Wall -g -O2 -MT regext.lo -MD -MP -MF .deps/regext.Tpo -c regext.c -fPIC -DPIC -o regext.o
mv -f .deps/regext.Tpo .deps/regext.Plo
/bin/sh ./libtool --tag=CC --mode=compile cc -DHAVE_CONFIG_H -I. -I. -I$HOME/local/include -I./enc/unicode -Wall -g -O2 -MT regcomp.lo -MD -MP -MF .deps/regcomp.Tpo -c -o regcomp.lo regcomp.c
libtool: compile: cc -DHAVE_CONFIG_H -I. -I. -I$HOME/local/include -I./enc/unicode -Wall -g -O2 -MT regcomp.lo -MD -MP -MF .deps/regcomp.Tpo -c regcomp.c -fPIC -DPIC -o regcomp.o
mv -f .deps/regcomp.Tpo .deps/regcomp.Plo
/bin/sh ./libtool --tag=CC --mode=compile cc -DHAVE_CONFIG_H -I. -I. -I$HOME/local/include -I./enc/unicode -Wall -g -O2 -MT regexec.lo -MD -MP -MF .deps/regexec.Tpo -c -o regexec.lo regexec.c
libtool: compile: cc -DHAVE_CONFIG_H -I. -I. -I$HOME/local/include -I./enc/unicode -Wall -g -O2 -MT regexec.lo -MD -MP -MF .deps/regexec.Tpo -c regexec.c -fPIC -DPIC -o regexec.o
cc: error: unable to execute command: Segmentation fault
cc: error: clang frontend command failed due to signal (use -v to see invocation)
FreeBSD clang version 6.0.0 (tags/RELEASE_600/final 326565) (based on LLVM 6.0.0)
Target: x86_64-unknown-freebsd11.2
Thread model: posix
InstalledDir: /usr/bin
cc: note: diagnostic msg: PLEASE submit a bug report to https://bugs.freebsd.org/submit/ and include the crash backtrace, preprocessed source, and associated run script.
cc: note: diagnostic msg:
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
cc: note: diagnostic msg: /tmp/regexec-19e25e.c
cc: note: diagnostic msg: /tmp/regexec-19e25e.sh
cc: note: diagnostic msg:

********************
*** Error code 1

Stop.
make[6]: stopped in $HOME/groonga-9.0.0/vendor/onigmo-source
*** Error code 1

Stop.
make[5]: stopped in $HOME/groonga-9.0.0/vendor/onigmo-source
*** Error code 1

Stop.
make[4]: stopped in $HOME/groonga-9.0.0/vendor/onigmo-source
*** Error code 1

Stop.
make[3]: stopped in $HOME/groonga-9.0.0/vendor/onigmo
*** Error code 1

Stop.
make[2]: stopped in $HOME/groonga-9.0.0/vendor
*** Error code 1

Stop.
make[1]: stopped in $HOME/groonga-9.0.0
*** Error code 1

Stop.
make: stopped in $HOME/groonga-9.0.0

上記の理由はよくわからない。おそらくは OS か C コンパイラのバージョンの問題と思われる。

もう一つ別の FreeBSD の環境があった。こちらにインストールした結果は、コンパイラのウォーニングがいくつかあったが、 インストールすることができたのだが、記録がない。9.0.6 をインストールしようとしたが、エラーが次のエラーが出た。

$[1] os uname -a
FreeBSD ***.*****.co.jp 12.0-RELEASE-p6 FreeBSD 12.0-RELEASE-p6 GENERIC  amd64
$[2] cc --version 
FreeBSD clang version 6.0.1 (tags/RELEASE_601/final 335540) (based on LLVM 6.0.1)
Target: x86_64-unknown-freebsd12.0
Thread model: posix
InstalledDir: /usr/bin
$[3] curl -O https://packages.groonga.org/source/groonga/groonga-9.0.6.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 15.2M  100 15.2M    0     0  10.3M      0  0:00:01  0:00:01 --:--:-- 10.3M
$[3] tar zxvf groonga-9.0.6
$[4] cd groonga-9.0.6
$[5] ./configure --prefix=$HOME/local --localstatedir=$HOME/local/var --with-log-path=$HOME/local/var/log/groonga.log
(中略)
groonga 9.0.6 configuration:
-----------------------
  Compiler:              gcc
  CFLAGS:                -g -O2 -Wall -Wno-unused-but-set-variable -Wno-pointer-sign -Wno-declaration-after-statement -Wformat -Wno-format-truncation -Wstrict-aliasing=2 -Wdisabled-optimization -Wfloat-equal -Wpointer-arith -Wbad-function-cast -Wwrite-strings -Wmissing-field-initializers
  CXXFLAGS:              -g -O2 -Wall -Wno-unused-but-set-variable -Wformat -Wno-format-truncation -Wstrict-aliasing=2 -Wdisabled-optimization -Wfloat-equal -Wpointer-arith -Wwrite-strings -Wmissing-field-initializers -fexceptions -fimplicit-templates
  Libraries:             -lz -lpthread -lexecinfo -lm
  Stack size:            1024
  Document:              yes
    buildable:           no
    built:               yes
  Munin plugins:         no
  Package platform:      no

Paths:
  Install path prefix:   $HOME/local
  Configuration file:    $HOME/local/etc/groonga/groonga.conf

Tokenizers:
  MeCab:                 yes
    CPPFLAGS:            -I/usr/local/include
    LDFLAGS:             -L/usr/local/lib
    LIBS:                -lmecab
  KyTea:                 no

Token filters:
  libstemmer:            no

Libraries:
  ZeroMQ:                no
  libevent:              yes
    CFLAGS:
    LIBS:                -levent
  MessagePack:           no
  LZ4:                   yes
    CFLAGS:              -I/usr/local/include
    LIBS:                -L/usr/local/lib -llz4
  Zstandard:             no
  LuaJIT:                no
  RapidJSON:             no
  mruby:                 no
  jemalloc:              no

groonga-httpd:
  enable:                yes
  default database path: $HOME/local/var/lib/groonga/db/db
  PCRE:                  yes
    CFLAGS:              -I/usr/local/include
    LIBS only -L:        -L/usr/local/lib
   SSL:                  no

Tools:
  Sphinx:
  lemon:                 none
  Ruby:
  Cutter:

For packages:
  rsync path:            packages@packages.groonga.org:public
  Launchpad PGP key:
  GPG UID:               (省略)


Now type 'make' to build groonga 9.0.6!


$[6] make -j4
(中略)
--- expr_executor.lo ---
gcc: fatal error: Killed signal terminated program cc1
compilation terminated.
--- libgroonga.la ---
  CXXLD    libgroonga.la
libtool:   error: 'expr_executor.lo' is not a valid libtool object
*** [libgroonga.la] Error code 1

ここで make を -j4 でコンパイルすることはあきらめた。時間がかかっても正しくコンパイルできることを確かめたい。

$[6] make 
(中略)
expr_executor.c:2424:1: warning: PRE disabled: 26116 basic blocks and 65511 registers; increase --param max-gcse-memory above 213942272 [-Wdisabled-optimizatio ]
expr_executor.c:2424:1: warning: const/copy propagation disabled: 26116 basic blocks and 65511 registers; increase --param max-gcse-memory above 213942272 [-Wdisabled-optimization]
expr_executor.c:2424:1: warning: const/copy propagation disabled: 23997 basic blocks and 77403 registers; increase --param max-gcse-memory above 232290960 [-Wdisabled-optimization]
gcc: fatal error: Killed signal terminated program cc1
compilation terminated.
*** Error code 1

どうも同じ現象だ。ソースからコンパイルして利用するのはあきらめる。

FreeBSD の pkg を利用する

ひょっとしたらすでにレンタルサーバに入っているのではないか。と思い pkg コマンドをたたいてみた。

$ pkg search groonga
groonga-9.0.4                  Open-source fulltext search engine and column store
p5-Net-Groonga-HTTP-0.03       Perl extension for Groonga httpd client library
p5-Test-Groonga-0.06_1         Perl extension for testing Groonga full-text search engine
pgroonga-2.2.0                 Open-source fulltext search engine for PostGRESQL

入っていた。現在(2019/08/16) の最新は 9.0.6 だが、気にするほどのことではない。

チュートリアルをやってみる

チュートリアルをやってみた。表示は整形されていないのでこうなる。なお、以下は Windows 10 Home Edition での実例である。

PS> groonga .\databases\introduction.db
> status
[[0,1566033247.142,0.003000020980834961],{"alloc_count":359,"starttime":1566033243,"start_time":1566033243,"uptime":4,"v
ersion":"9.0.6","n_queries":0,"cache_hit_rate":0.0,"command_version":1,"default_command_version":1,"max_command_version"
:3}]
>

整形するとこうなる。

> status #[ # [ # 0, # 1566033247.142, # 0.003000020980834961 # ], # { # "alloc_count":359, # "starttime":1566033243, # "start_time":1566033243, # "uptime":4, # "version":"9.0.6", # "n_queries":0, # "cache_hit_rate":0.0, # "command_version":1, # "default_command_version":1, # "max_command_version":3 # } #] >

4.1.5 から 4.1.6, 4.1.7, 4.1.8, 4.1.9 もできた。 ただ、4.8.2主キーによる後方一致検索 はうまくいかない。 Windows で PowerShell から実行している。日本語環境のせいなのかもしれない。

まりんきょ学問所コンピュータの部屋 全文検索システム Groonga


MARUYAMA Satosi