groongaは、JavaScriptに似た文法での条件絞込や、計算した値を用いたソートを行うことができます。また、位置情報(緯度・経度)を用いた絞込・ソートを行うことができます。
selectコマンドのfilterパラメータは、queryパラメータと同様に、レコードの検索条件を指定します。filterパラメータとqueryパラメータが異なる点は、filterパラメータには、JavaScriptの式に似た文法で条件を指定する点です。
実行例
> select --table Site --filter "_id <= 1" --output_columns _id,_key
[[0,1280378885.95759,0.000306],[[[1],[["_id","UInt32"],["_key","ShortText"]],[1,"http://example.org/"]]]]
ここで、filterパラメータには
_id <= 1
という条件を指定しています。この場合は_idの値が1以下のレコードが検索結果として得られます。
また、&& や || を使って、条件のAND・OR指定をすることもできます。
実行例
> select --table Site --filter "_id >= 4 && _id <= 6" --output_columns _id,_key
[[0,1280378886.15944,0.000311],[[[3],[["_id","UInt32"],["_key","ShortText"]],[4,"http://example.net/afr"],[5,"http://example.org/aba"],[6,"http://example.com/rab"]]]]
> select --table Site --filter "_id <= 2 || _id >= 7" --output_columns _id,_key
[[0,1280378886.36174,0.000321],[[[5],[["_id","UInt32"],["_key","ShortText"]],[1,"http://example.org/"],[2,"http://example.net/"],[7,"http://example.net/atv"],[8,"http://example.org/gat"],[9,"http://example.com/vdw"]]]]
queryパラメータとfilterパラメータを同時に指定すると、両者の条件をともに満たすレコードが結果として返ります。
selectコマンドのscorerパラメータは、 全文検索を行った結果の各レコードに対して処理を行うためのパラメータです。
filterパラメータと同様に、 JavaScriptの式に似たな文法で様々な条件を指定することができます。
実行例
> select --table Site --filter "1" --scorer "_score = rand()" --output_columns _id,_key,_score --sortby _score
[[0,1280378886.56471,0.000504],[[[9],[["_id","UInt32"],["_key","ShortText"],["_score","Int32"]],[3,"http://example.com/",35005211],[9,"http://example.com/vdw",278722862],[5,"http://example.org/aba",294702567],[1,"http://example.org/",304089172],[7,"http://example.net/atv",336465782],[4,"http://example.net/afr",521595368],[8,"http://example.org/gat",861021530],[2,"http://example.net/",1303455736],[6,"http://example.com/rab",1726956429]]]]
> select --table Site --filter "1" --scorer "_score = rand()" --output_columns _id,_key,_score --sortby _score
[[0,1280378886.76988,0.00049],[[[9],[["_id","UInt32"],["_key","ShortText"],["_score","Int32"]],[1,"http://example.org/",233665123],[3,"http://example.com/",468703135],[7,"http://example.net/atv",635723058],[4,"http://example.net/afr",1101513929],[9,"http://example.com/vdw",1125898167],[6,"http://example.com/rab",1315634022],[8,"http://example.org/gat",1369133069],[5,"http://example.org/aba",1801979802],[2,"http://example.net/",2145174067]]]]
検索結果には、’_score’という名前の、全文検索のスコアが代入されている仮想的なカラムが付与されることをチュートリアル中ソートの項目で説明しました。
上記の実行例では、scorerパラメータに
_score = rand()
という条件を指定しています。ここでは、rand()という乱数を返す関数を用いて、全文検索のスコアを乱数で上書きしています。
sortbyパラメータには、
_score
を指定しています。これは、スコア順に昇順にソートすることを意味しています。
よって、上記のクエリは実行されるたびに検索結果の並び順がランダムに変わります。
groongaでは、位置情報(経緯度)を保存することができます。また、保存した経緯度を用いて絞込やソートができます。
位置情報を保存するためのカラムの型として、TokyoGeoPoint/WGS84GeoPointの2つの型があります。前者は日本測地系、後者は世界測地系(WGS84相当)の経緯度を保存します。
経緯度をデータを入力するためには、”緯度のミリ秒x経度のミリ秒”という形式の文字列を代入する必要があります。
ここでは、ためしに東京駅と新宿駅とついて、世界測地系での位置情報を保存してみましょう。東京駅は緯度が35度40分52.975秒、経度が139度45分57.902秒です。新宿駅は緯度が35度41分27.316秒、経度が139度42分0.929秒です。よって、代入する文字列はそれぞれ”128452975x503157902”/”128487316x502920929”となります。
実行例
> column_create --table Site --name location --type WGS84GeoPoint
[[0,1280378886.97522,0.109348],true]
> load --table Site
> [
> {"_key":"http://example.org/","location":"128452975x503157902"}
> {"_key":"http://example.net/","location":"128487316x502920929"},
> ]
[[0,1280378887.28534,0.801067],2]
> select --table Site --query "_id:1 OR _id:2" --output_columns _key,location
[[0,1280378888.28703,0.000371],[[[2],[["_key","ShortText"],["location","WGS84GeoPoint"]],["http://example.org/","128452975x503157902"],["http://example.net/","128487316x502920929"]]]]
scorerパラメータにおいて、 geo_distance 関数を用いることにより、2点間の距離を計算することができます。
ここでは、秋葉原駅からの距離を表示させてみましょう。世界測地系では、秋葉原駅の位置は緯度が35度41分55.259秒、経度が139度46分27.188秒です。よって、geo_distance関数に与える文字列は”128515259x503187188”となります。
実行例
> select --table Site --query "_id:1 OR _id:2" --output_columns _key,location,_score --scorer '_score = geo_distance(location, "128515259x503187188")'
[[0,1280378888.48983,0.000429],[[[2],[["_key","ShortText"],["location","WGS84GeoPoint"],["_score","Int32"]],["http://example.org/","128452975x503157902",2054],["http://example.net/","128487316x502920929",6720]]]]
この結果を見ると、東京駅と秋葉原駅は2054m、秋葉原駅と新宿駅は6720m離れているようです。
geo_distance関数は、_scoreを通じてソートでも用いることができます。
実行例
> select --table Site --query "_id:1 OR _id:2" --output_columns _key,location,_score --scorer '_score = geo_distance(location, "128515259x503187188")' --sortby -_score
[[0,1280378888.69281,0.000524],[[[2],[["_key","ShortText"],["location","WGS84GeoPoint"],["_score","Int32"]],["http://example.net/","128487316x502920929",6720],["http://example.org/","128452975x503157902",2054]]]]
「ある地点から何m以内に存在する」といった絞込も可能です。
filterパラメータにおいて、 geo_in_circle 関数を用いることにより、2点間の距離が指定のm以下におさまるかどうかを判定することができます。
たとえば、秋葉原駅から5000m以内にあるレコードを検索してみましょう。
実行例
> select --table Site --output_columns _key,location --filter 'geo_in_circle(location, "128515259x503187188", 5000)'
[[0,1280378888.89597,0.000335],[[[1],[["_key","ShortText"],["location","WGS84GeoPoint"]],["http://example.org/","128452975x503157902"]]]]
また、経緯度が指定の矩形領域内であるかどうかを判定する geo_in_rectangle 関数も存在します。