2009.04.17

Category: PHP / Tags:

Hyper Estraier を phpからつかってみる(検索のみ)

http://page2.xrea.jp/
に、ノード API の Pure PHP クラスライブラリ EstraierPure があったので、これを利用して
Hyper Estraier をつかってみる。
でだしは、MTの検索が重いので、全文検索のnamazuを利用していたのだけど、つかいこなして
ないこともあって、いまいち検索結果がピンとこない。
てことで、文章ドラフトをつくれる、Hyper Estraierで、やってみようかとというところと,
PHP Namazu モジュールでは、URIを指定しての検索ができない(つまりはブロガー指定の検索ができない)ので、

一応、Cのソースまで見てみたのですが、実装ができてないみたいだったので、できるならごめんなさい。
自分で実装すれば、っていうのも今回はなしで。

P2Pあたりの機能で実装できそうなこちらにチャレンジしてみます。
ブログ登録があると自動で検索対象にしてくれるモジュールがあるのも決めてでした。
namazuだと、毎日indexの更新するcronが動くまでまたないといけなかったので。
下記にドキュメントはあるのですが
http://page2.xrea.jp/doc/all/index.php?package=Services_HyperEstraier
多くの人がとりあえず、indexの生成とかは、サーバーがわのシェルスクリプトとかでやって
検索インターフェイスの実装だけをしたいと思うので、(というかおいらが)その分のメモ書き
現状の検索は
html -> js(ajax) -> api -> namazu -> api -> js -> html
のながれで、jsから、apiにアクセスして、apiが内部でnamazuの検索結果を取得して、XML
に変換して返しているので、この aip の書き直しと namazuを hyper estraier への移行で
対応したいと思います。
Class HyperEstraier_Condition が検索で主に使われるクラスになります。
簡単なながれで、
1.検索のオプション値(検索ワード、表示順、取得数)を設置して、
2.検索メソット(serach)にアクセスして、結果を取得
3.取得したデータを加工して表示
  (xmlでも結果を取得できるので上手くいけばいらないかもしれない)
を想定
HyperEstraier_Conditionの初期値(つまりは、検索の初期値)は、下記の通り
//初期値
$this->phrase = null;
$this->attrs = array();
$this->order = null;
$this->max = -1;
$this->skip = 0;
$this->options = 0;
$this->auxiliary = 32;
$this->distinct = null;
$this->mask = 0;
それぞれ、setterとgetterのメソットがあるので、あとはそれらを利用して自由に検索してください。
簡易一覧 – そのうち適当な日本語に置き換える予定
function_name param の順です
//Set the search phrase.
set_phrase($phrase) (string $phrase A search phrase.) void
//Add an expression for an attribute.
add_attr($expr) (string $expr A search expression.) void
//Set the order of a condition object.
set_order($order) (string $order An expression for the order. By default, the order is by score descending.) void
//Set the maximum number of retrieval.
set_max($max) (int $max The maximum number of retrieval. By default, the number of retrieval is not limited.) void
//Set the number of documents to be skipped.
set_skip($skip) (int $skip The number of documents to be skipped. By default, it is 0.) void
//Set options of retrieval.
set_options($options) (int $options Options: * – ESTRAIERPURE_CONDITION_SURE’ specifies that it checks every N-gram key. * – ESTRAIERPURE_CONDITION_USUAL’, which is the default, * specifies that it checks N-gram keys with skipping one key. * – ESTRAIERPURE_CONDITION_FAST’ skips two keys. * – ESTRAIERPURE_CONDITION_AGITO’ skips three keys. * – “ESTRAIERPURE_CONDITION_NOID” F’ specifies not to perform TF-ID F tuning. * – ESTRAIERPURE_CONDITION_SIMPLE’ specifies to use simplified phrase. * – ESTRAIERPURE_CONDITION_ROUGH’ specifies to use rough phrase. * – ESTRAIERPURE_CONDITION_UNION’ specifies to use union phrase. * – ESTRAIERPURE_CONDITION_ISECT’ specifies to use intersection phrase. * Each option can be specified at the same time by bitwise or. * If keys are skipped, though search speed is improved, the relevance ratio grows less.) void
//Set permission to adopt result of the auxiliary index.
set_auxiliary($min) (int $min The minimum hits to adopt result of the auxiliary index. If it is not more than 0, the auxiliary index is not used. By default, it is 32.) void
// Set the attribute distinction filter.
set_distinct($name) (string $name The name of an attribute to be distinct.) void
//Set the mask of targets of meta search.
set_mask($mask) (int $mask A masking number. 1 means the first target, 2 means the second target, 4 means the third target, and power values of 2 and their summation compose the mask.) void
// getter methods
それぞれのgetter methods
function phrase()
function attrs()
function order()
function max()
function skip()
function options()
function auxiliary()
function distinct()
function mask()

上記の、設定がおわったら
EstraierPure_Node の search メソットになげれば、検索結果(EstraierPure_NodeResultのobject)が返ってきます。
get_doc
get_doc_attr
get_doc_attr_by_uri
get_doc_by_uri
あたりをつかって、加工すればOK

2009.04.01

Category: PostgreSQL / Tags:

postgreSQLでDBのサイズを確認したい場合

vacuumedb -f (full) は、できないけど、DBの再利用領域は知りたい場合
pg_database_size と pgstattuple (contribにあります) の2つのコマンド
を使うといい
使い方は、DBに入って、
dbname=# select pg_database_size(’databasename’);
pg_database_size
——————
49801171592
dbname=#select * from pgstattuple(’tablename’);
table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_space | free_percent
————+————-+————+—————+——————+—————-+——————–+————+————–
1738309632 | 9737154 | 1466581672 | 84.37 | 0 | 0 | 0 | 222155348 | 12.78
(1 row)
とかでてきます、大切なのは、free_spaceの所の値、ここが再利用領域になります。
– 以下に pgstattuple のREADME( 2002/08/22 石井達夫)をつけておきます。
1. pgstattupleとは
pgstattupleは,UPDATEやDELETEで作られたテーブルのゴミ領域の大きさを,
テーブル自体の物理的な大きさに対するパーセンテージで返却します.つ
まり,返却値が大きければ,それだけゴミも多いので,vacuumをかける必
要があるという判断の助けになるわけです.これ以外にもいろいろな情報
が返ります.
test=# \x
Expanded display is on.
test=# select * from pgstattuple(’pg_proc’);
-[ RECORD 1 ]——+——-
table_len | 458752
tuple_count | 1470
tuple_len | 438896
tuple_percent | 95.67
dead_tuple_count | 11
dead_tuple_len | 3157
dead_tuple_percent | 0.69
free_space | 8932
free_percent | 1.95
各項目の説明です.
table_len – テーブルの物理的な大きさ(バイト)
tuple_count – タプル数
tuple_len – タプル長の合計(バイト)
tuple_percent – タプルの割合.table_lenに対するtuple_lenの比率.
dead_tuple_len – デッドタプル数
dead_tuple_percent – デッドタプルの割合.table_lenに対するtuple_lenの比率.
free_space – 再利用可能な領域(バイト)
free_percent – 再利用可能な領域.table_lenに対するfree_spaceの比率.
2. pgstattupleのインストール
PostgreSQLが/usr/local/pgsqlにインストール済であり,testデータベー
スにpgstattupleをインストールする場合の手順を示します.
$ make
$ make install
ユーザ定義関数を登録します.
$ psql -e -f /usr/local/pgsql/share/contrib/pgstattuple.sql test
3. pgstattupleの使い方
pgstattupleの呼び出し形式は以下です.
CREATE OR REPLACE FUNCTION pgstattuple(text) RETURNS pgstattuple_type
AS ‘MODULE_PATHNAME’, ‘pgstattuple’
LANGUAGE ‘c’ WITH (isstrict);
第一引数: テーブル名
関数の戻りはpgstattuple_type型です.
pgstattupleはテーブルにAccessShareLockしかかけないので,
pgstattuple を実行中に該当テーブルに更新や削除が発生すると,正しく
ない結果を返す可能性があります.
pgstattupleがタプルを「ゴミ」と判断する基準は,
HeapTupleSatisfiesNow()が偽を返したときです.
4. pgstattupleのライセンス条件について
pgstattuple.cの冒頭に書いてある通りです.また,pgstattuple は完全に無保
証です.pgstattuple を使用したことによって生じるいかなる結果に関して
も責任を負いません.
5. 改訂履歴
2002/09/04
SRF変更に伴い,Tom Lane が修正インターフェイスの修正を行った.
そのことをこのREADMEにも反映.
2002/08/23
SRF(Set Returning Function)を使って7.3用に書き換え.
2001/12/20 Tom Laneによる修正
Fix pgstattuple to acquire a read lock on the target table. This
prevents embarassments such as having the table dropped or truncated
partway through the scan. Also, fix free space calculation to include
pages that currently contain no tuples.
2001/10/01 PostgreSQL 7.2 用contrib moduleに登録
2001/08/30 pgstattuple バージョン 0.1リリース