Elasticsearchでサンプルのデータセット(ドキュメント1000件)を用いてQuery DSLの紹介がてらを簡単なものをいくつか試す

Elasticsearchでサンプルのデータセット(ドキュメント1000件)を用いてQuery DSLの紹介がてらを簡単なものをいくつか試す方法を記す。

公式ドキュメント Introducing the Query Language を参考にした。 www.elastic.co

検証環境: Elasticsearch 6.0.0-rc1

前提として、Elasticsearchにサンプルのデータセット(ドキュメント1000件)をbulk APIを使って登録しておき簡単な検索方法を試す。 sakura818uuu.hatenadiary.com sakura818uuu.hatenadiary.com

ElasticsearchはQuery DSLというJSONベースでクエリの書く一つの方法(言語)を提供している。
前提として紹介したサンプルのデータセット(ドキュメント1000件)を用いていくつかのQuery DSLの簡単な例を用いて試してみる。

bash-3.2$ curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
> {
>   "query": { "match_all": {} }
> }
> '
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1000,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "25",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 25,
          "balance" : 40540,
          "firstname" : "Virginia",
          "lastname" : "Ayala",
          "age" : 39,
          "gender" : "F",
          "address" : "171 Putnam Avenue",
          "employer" : "Filodyne",
          "email" : "virginiaayala@filodyne.com",
          "city" : "Nicholson",
          "state" : "PA"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "44",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 44,
          "balance" : 34487,
          "firstname" : "Aurelia",
          "lastname" : "Harding",
          "age" : 37,
          "gender" : "M",
          "address" : "502 Baycliff Terrace",
          "employer" : "Orbalix",
          "email" : "aureliaharding@orbalix.com",
          "city" : "Yardville",
          "state" : "DE"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "99",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 99,
          "balance" : 47159,
          "firstname" : "Ratliff",
          "lastname" : "Heath",
          "age" : 39,
          "gender" : "F",
          "address" : "806 Rockwell Place",
          "employer" : "Zappix",
          "email" : "ratliffheath@zappix.com",
          "city" : "Shaft",
          "state" : "ND"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "119",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 119,
          "balance" : 49222,
          "firstname" : "Laverne",
          "lastname" : "Johnson",
          "age" : 28,
          "gender" : "F",
          "address" : "302 Howard Place",
          "employer" : "Senmei",
          "email" : "lavernejohnson@senmei.com",
          "city" : "Herlong",
          "state" : "DC"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "126",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 126,
          "balance" : 3607,
          "firstname" : "Effie",
          "lastname" : "Gates",
          "age" : 39,
          "gender" : "F",
          "address" : "620 National Drive",
          "employer" : "Digitalus",
          "email" : "effiegates@digitalus.com",
          "city" : "Blodgett",
          "state" : "MD"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "145",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 145,
          "balance" : 47406,
          "firstname" : "Rowena",
          "lastname" : "Wilkinson",
          "age" : 32,
          "gender" : "M",
          "address" : "891 Elton Street",
          "employer" : "Asimiline",
          "email" : "rowenawilkinson@asimiline.com",
          "city" : "Ripley",
          "state" : "NH"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "183",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 183,
          "balance" : 14223,
          "firstname" : "Hudson",
          "lastname" : "English",
          "age" : 26,
          "gender" : "F",
          "address" : "823 Herkimer Place",
          "employer" : "Xinware",
          "email" : "hudsonenglish@xinware.com",
          "city" : "Robbins",
          "state" : "ND"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "190",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 190,
          "balance" : 3150,
          "firstname" : "Blake",
          "lastname" : "Davidson",
          "age" : 30,
          "gender" : "F",
          "address" : "636 Diamond Street",
          "employer" : "Quantasis",
          "email" : "blakedavidson@quantasis.com",
          "city" : "Crumpler",
          "state" : "KY"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "208",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 208,
          "balance" : 40760,
          "firstname" : "Garcia",
          "lastname" : "Hess",
          "age" : 26,
          "gender" : "F",
          "address" : "810 Nostrand Avenue",
          "employer" : "Quiltigen",
          "email" : "garciahess@quiltigen.com",
          "city" : "Brooktrails",
          "state" : "GA"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "222",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 222,
          "balance" : 14764,
          "firstname" : "Rachelle",
          "lastname" : "Rice",
          "age" : 36,
          "gender" : "M",
          "address" : "333 Narrows Avenue",
          "employer" : "Enaut",
          "email" : "rachellerice@enaut.com",
          "city" : "Wright",
          "state" : "AZ"
        }
      }
    ]
  }
}

queryパラメータはクエリ定義を、match_allパラメータは実行したいクエリタイプを表している。
他にもsizeパラメータやfromパラメータもクエリに加えることが出来る。
sizeパラメータは表示する検索結果の数を表す。もしもsizeパラメータを設定しない場合は、デフォルトで10件検索結果を表示するようになっている。
fromパラメータはドキュメントを開始する地点を表す。例えば、fromパラメータに10が設定されていた場合はドキュメントの11〜20を返す。もしもfromパラメータを設定しない場合は、デフォルトで0に設定されている。
以下に実際にクエリを入力した結果を示す。

bash-3.2$ curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
> {
>   "query": { "match_all": {} },
>   "size": 1
> }
> '
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1000,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "25",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 25,
          "balance" : 40540,
          "firstname" : "Virginia",
          "lastname" : "Ayala",
          "age" : 39,
          "gender" : "F",
          "address" : "171 Putnam Avenue",
          "employer" : "Filodyne",
          "email" : "virginiaayala@filodyne.com",
          "city" : "Nicholson",
          "state" : "PA"
        }
      }
    ]
  }
}
bash-3.2$ curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
> {
>   "query": { "match_all": {} },
>   "from": 10,
>   "size": 10
> }
> '
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1000,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "227",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 227,
          "balance" : 19780,
          "firstname" : "Coleman",
          "lastname" : "Berg",
          "age" : 22,
          "gender" : "M",
          "address" : "776 Little Street",
          "employer" : "Exoteric",
          "email" : "colemanberg@exoteric.com",
          "city" : "Eagleville",
          "state" : "WV"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "253",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 253,
          "balance" : 20240,
          "firstname" : "Melissa",
          "lastname" : "Gould",
          "age" : 31,
          "gender" : "M",
          "address" : "440 Fuller Place",
          "employer" : "Buzzopia",
          "email" : "melissagould@buzzopia.com",
          "city" : "Lumberton",
          "state" : "MD"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "260",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 260,
          "balance" : 2726,
          "firstname" : "Kari",
          "lastname" : "Skinner",
          "age" : 30,
          "gender" : "F",
          "address" : "735 Losee Terrace",
          "employer" : "Singavera",
          "email" : "kariskinner@singavera.com",
          "city" : "Rushford",
          "state" : "WV"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "265",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 265,
          "balance" : 46910,
          "firstname" : "Marion",
          "lastname" : "Schneider",
          "age" : 26,
          "gender" : "F",
          "address" : "574 Everett Avenue",
          "employer" : "Evidends",
          "email" : "marionschneider@evidends.com",
          "city" : "Maplewood",
          "state" : "WY"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "335",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 335,
          "balance" : 35433,
          "firstname" : "Vera",
          "lastname" : "Hansen",
          "age" : 24,
          "gender" : "M",
          "address" : "252 Bushwick Avenue",
          "employer" : "Zanilla",
          "email" : "verahansen@zanilla.com",
          "city" : "Manila",
          "state" : "TN"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "366",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 366,
          "balance" : 42368,
          "firstname" : "Lydia",
          "lastname" : "Cooke",
          "age" : 31,
          "gender" : "M",
          "address" : "470 Coleman Street",
          "employer" : "Comstar",
          "email" : "lydiacooke@comstar.com",
          "city" : "Datil",
          "state" : "TN"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "385",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 385,
          "balance" : 11022,
          "firstname" : "Rosalinda",
          "lastname" : "Valencia",
          "age" : 22,
          "gender" : "M",
          "address" : "933 Lloyd Street",
          "employer" : "Zoarere",
          "email" : "rosalindavalencia@zoarere.com",
          "city" : "Waverly",
          "state" : "GA"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "397",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 397,
          "balance" : 37418,
          "firstname" : "Leonard",
          "lastname" : "Gray",
          "age" : 36,
          "gender" : "F",
          "address" : "840 Morgan Avenue",
          "employer" : "Recritube",
          "email" : "leonardgray@recritube.com",
          "city" : "Edenburg",
          "state" : "AL"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "400",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 400,
          "balance" : 20685,
          "firstname" : "Kane",
          "lastname" : "King",
          "age" : 21,
          "gender" : "F",
          "address" : "405 Cornelia Street",
          "employer" : "Tri@Tribalog",
          "email" : "kaneking@tri@tribalog.com",
          "city" : "Gulf",
          "state" : "VT"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "450",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 450,
          "balance" : 2643,
          "firstname" : "Bradford",
          "lastname" : "Nielsen",
          "age" : 25,
          "gender" : "M",
          "address" : "487 Keen Court",
          "employer" : "Exovent",
          "email" : "bradfordnielsen@exovent.com",
          "city" : "Hamilton",
          "state" : "DE"
        }
      }
    ]
  }
}

以下の例では、すべてのドキュメントに対しbalance(アカウント残高を意味する)を降順にソートさせたものをヒットさせるクエリを実行している。sizeは指定していないので検索結果の10件が表示される。

bash-3.2$ curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
> {
>   "query": { "match_all": {} },
>   "sort": { "balance": { "order": "desc" } }
> }
> '
{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1000,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "248",
        "_score" : null,
        "_source" : {
          "account_number" : 248,
          "balance" : 49989,
          "firstname" : "West",
          "lastname" : "England",
          "age" : 36,
          "gender" : "M",
          "address" : "717 Hendrickson Place",
          "employer" : "Obliq",
          "email" : "westengland@obliq.com",
          "city" : "Maury",
          "state" : "WA"
        },
        "sort" : [
          49989
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "854",
        "_score" : null,
        "_source" : {
          "account_number" : 854,
          "balance" : 49795,
          "firstname" : "Jimenez",
          "lastname" : "Barry",
          "age" : 25,
          "gender" : "F",
          "address" : "603 Cooper Street",
          "employer" : "Verton",
          "email" : "jimenezbarry@verton.com",
          "city" : "Moscow",
          "state" : "AL"
        },
        "sort" : [
          49795
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "240",
        "_score" : null,
        "_source" : {
          "account_number" : 240,
          "balance" : 49741,
          "firstname" : "Oconnor",
          "lastname" : "Clay",
          "age" : 35,
          "gender" : "F",
          "address" : "659 Highland Boulevard",
          "employer" : "Franscene",
          "email" : "oconnorclay@franscene.com",
          "city" : "Kilbourne",
          "state" : "NH"
        },
        "sort" : [
          49741
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "97",
        "_score" : null,
        "_source" : {
          "account_number" : 97,
          "balance" : 49671,
          "firstname" : "Karen",
          "lastname" : "Trujillo",
          "age" : 40,
          "gender" : "F",
          "address" : "512 Cumberland Walk",
          "employer" : "Tsunamia",
          "email" : "karentrujillo@tsunamia.com",
          "city" : "Fredericktown",
          "state" : "MO"
        },
        "sort" : [
          49671
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "842",
        "_score" : null,
        "_source" : {
          "account_number" : 842,
          "balance" : 49587,
          "firstname" : "Meagan",
          "lastname" : "Buckner",
          "age" : 23,
          "gender" : "F",
          "address" : "833 Bushwick Court",
          "employer" : "Biospan",
          "email" : "meaganbuckner@biospan.com",
          "city" : "Craig",
          "state" : "TX"
        },
        "sort" : [
          49587
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "168",
        "_score" : null,
        "_source" : {
          "account_number" : 168,
          "balance" : 49568,
          "firstname" : "Carissa",
          "lastname" : "Simon",
          "age" : 20,
          "gender" : "M",
          "address" : "975 Flatbush Avenue",
          "employer" : "Zillacom",
          "email" : "carissasimon@zillacom.com",
          "city" : "Neibert",
          "state" : "IL"
        },
        "sort" : [
          49568
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "803",
        "_score" : null,
        "_source" : {
          "account_number" : 803,
          "balance" : 49567,
          "firstname" : "Marissa",
          "lastname" : "Spears",
          "age" : 25,
          "gender" : "M",
          "address" : "963 Highland Avenue",
          "employer" : "Centregy",
          "email" : "marissaspears@centregy.com",
          "city" : "Bloomington",
          "state" : "MS"
        },
        "sort" : [
          49567
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "926",
        "_score" : null,
        "_source" : {
          "account_number" : 926,
          "balance" : 49433,
          "firstname" : "Welch",
          "lastname" : "Mcgowan",
          "age" : 21,
          "gender" : "M",
          "address" : "833 Quincy Street",
          "employer" : "Atomica",
          "email" : "welchmcgowan@atomica.com",
          "city" : "Hampstead",
          "state" : "VT"
        },
        "sort" : [
          49433
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "954",
        "_score" : null,
        "_source" : {
          "account_number" : 954,
          "balance" : 49404,
          "firstname" : "Jenna",
          "lastname" : "Martin",
          "age" : 22,
          "gender" : "M",
          "address" : "688 Hart Street",
          "employer" : "Zinca",
          "email" : "jennamartin@zinca.com",
          "city" : "Oasis",
          "state" : "MD"
        },
        "sort" : [
          49404
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "572",
        "_score" : null,
        "_source" : {
          "account_number" : 572,
          "balance" : 49355,
          "firstname" : "Therese",
          "lastname" : "Espinoza",
          "age" : 20,
          "gender" : "M",
          "address" : "994 Chester Court",
          "employer" : "Gonkle",
          "email" : "thereseespinoza@gonkle.com",
          "city" : "Hayes",
          "state" : "UT"
        },
        "sort" : [
          49355
        ]
      }
    ]
  }
}