ElasticsearchのQuery一覧
「その1」に続いて「その2」。同じくドキュメントをもとに簡単に説明していきます。
今回は実際にQueryを実行していきます!
インデックスはこんな感じ
{
"name": "suzuki",
"age" : 28,
"male": false,
"description": "I am a pen."
}
{
"name": "tanaka",
"age": 25,
"male": true,
"description": "null"
}
{
"name": "satou",
"age": 22,
"male": true
}
Term level queries
Full Text Query
と違い、検索するQueryはAnalyzeされません。そのため、数字や日付に対して有効なQueryが多いです。
Term Query
完全一致したドキュメントがマッチするQuery。
数字や日付に加えて、boolean、テキストにも使う。
検索時にフィルタリングしたい時にpost_filter
に入れて使うことが多い。(使用例ではquery
で使っています)
登録項目の定型テキスト(都道府県)等にも使ったりする。(このように使うためには、フィールドの"index"を"not_analyzed"に設定する必要がある。)
使用例
女性を検索したい時
{
"query": {
"term": {
"male": {
"value": "false"
}
}
}
}
結果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.30685282,
"hits": [
{
"_index": "test",
"_type": "user",
"_id": "1",
"_score": 0.30685282,
"_source": {
"name": "suzuki",
"age": 28,
"male": false,
"description": "I am a pen."
}
}
]
}
}
女性のユーザのみ取得することができました。
Terms Query
Term Query
の複数のQueryで検索(or)できるやつ。
使用例
名前が"tanaka"と"suzuki"のユーザを取得するQuery
{
"query": {
"terms": {
"name": [ "tanaka", "suzuki" ]
}
}
}
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.19245009,
"hits": [
{
"_index": "test",
"_type": "user",
"_id": "2",
"_score": 0.19245009,
"_source": {
"name": "tanaka",
"age": 25,
"male": true
}
},
{
"_index": "test",
"_type": "user",
"_id": "1",
"_score": 0.19245009,
"_source": {
"name": "suzuki",
"age": 28,
"male": false,
"description": "I am a pen."
}
}
]
}
}
Range Query
名前の通り範囲を指定できるQuery。
日付とか数字に使う。
日付に使う時はタイムゾーンを指定したりもできる。また、日付の代わりに"now"を指定することで、現在の時刻を指定することができる。(便利)
使用例
年齢が22以上25以下のユーザを検索する。
{
"query": {
"range": {
"age": {
"gte": 22,
"lte": 25
}
}
}
}
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "user",
"_id": "2",
"_score": 1,
"_source": {
"name": "tanaka",
"age": 25,
"male": true
}
},
{
"_index": "test",
"_type": "user",
"_id": "3",
"_score": 1,
"_source": {
"name": "satou",
"age": 22,
"male": true
}
}
]
}
}
gte,lteの片方だけを指定できたり、イコールを含めたくない場合にはgt,ltを使ったりする。
Exists Query
指定したフィールドを持っているもののみを絞り込めるQuery。
値がnullのものは含まれない。
逆に指定したフィールドを持っていないもののみを絞り込みたいときはBool Query
のmust_not
を使う。
使用例
descriptionを登録しているユーザのみを検索
{
"query": {
"exists": {
"field": "description"
}
}
}
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "user",
"_id": "1",
"_score": 1,
"_source": {
"name": "suzuki",
"age": 28,
"male": false,
"description": "I am a pen."
}
}
]
}
}
Wildcard Query
Queryに"*"と"?"が使える。
使ったことない。
使用例
nameに「a」がふくまれているユーザのみを検索
{
"query": {
"wildcard": {
"name": {
"value": "*a*"
}
}
}
}
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "user",
"_id": "2",
"_score": 1,
"_source": {
"name": "tanaka",
"age": 25,
"male": true,
"description": null
}
},
{
"_index": "test",
"_type": "user",
"_id": "3",
"_score": 1,
"_source": {
"name": "satou",
"age": 22,
"male": true
}
}
]
}
}
Regexp Query
正規表現を使用できるQuery。
Wildcard Query
と同様に使えるので、使用例は省略。
Fuzzy Query
曖昧な単語用のQuery。
レーベンシュタイン距離を元に類似度を計算している。(内容知らない)
stringのフィールド以外にも日付や数字にもつかえる。
stringで使うことが多い。
パラメータのfuzziness
でどのくらいの類似性を許容するかを設定できる(詳しくはここ)。
Term level queriesの中でこのQueryだけ、フィルターではなくクエリーとして流すことが多い。
使用例
名前が"tana"に近い人を検索
{
"query": {
"fuzzy": {
"name": {
"value": "tana",
"fuzziness": 2
}
}
}
}
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.30685282,
"hits": [
{
"_index": "test",
"_type": "user",
"_id": "2",
"_score": 0.30685282,
"_source": {
"name": "tanaka",
"age": 25,
"male": true
}
}
]
}
}
fuzzinessを設定しない(デフォルト値のAUTO)にすると何もヒットしない。
Type Query
Typeが等しいものを取得するQuery。
document名/type名/searchを使えばtypeの絞りこみはできるので、用途がわからない。
用途わかる方コメントください。
使用例
typeがuserのものを検索
{
"query": {
"type": {
"value": "user"
}
}
}
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "user",
"_id": "2",
"_score": 1,
"_source": {
"name": "tanaka",
"age": 25,
"male": true,
"description": null
}
},
{
"_index": "test",
"_type": "user",
"_id": "1",
"_score": 1,
"_source": {
"name": "suzuki",
"age": 28,
"male": false,
"description": "I am a pen."
}
},
{
"_index": "test",
"_type": "user",
"_id": "3",
"_score": 1,
"_source": {
"name": "satou",
"age": 22,
"male": true
}
}
]
}
}
今、別typeのもの作ってないので、すべて表示されます。
Ids Query
idを複数指定するためのQuery。
一つの場合はGET document名/type名/id
で取得できるので使わない。
検索から除外したいidを指定するしたり、idの一覧の中から検索したいときによく使う。
使用例
idが1と3のユーザを取得
{
"query": {
"ids": {
"values": [1, 3]
}
}
}
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "user",
"_id": "1",
"_score": 1,
"_source": {
"name": "suzuki",
"age": 28,
"male": false,
"description": "I am a pen."
}
},
{
"_index": "test",
"_type": "user",
"_id": "3",
"_score": 1,
"_source": {
"name": "satou",
"age": 22,
"male": true
}
}
]
}
}
まとめ
今回は、Queryの書き方も紹介してみましたがいかがだったでしょうか?
SQLに書き換えられるようなものも多く多かったと思います。
他にもこういう使い方があるよとか、こういうことも書いて欲しいとかありましたら、コメントしてください。