こんにちは。ニフティライフスタイルの中野です。
この記事はニフティグループ Advent Calendar 2018の7日目の記事です。
昨日は @hicka04 さんで「社内で勉強会をやるときに気をつけていること」でした!
ニフティのWEBサービスでは、一部サービスのサイト内検索やログ可視化などで Elasticsearch を利用しています。
以前投稿した ElasticsearchのField Collapsingで検索結果をグルーピングする について、Elasticsearch 6.4 から Second level of collapsing という機能が追加されていましたので使ってみたいと思います。
Field Collapsing はその名のとおり検索結果を「折りたたんで」くれる機能で、検索結果をグルーピングして SQL の GROUP BY のような操作をすることができます。
Elasticsearch 6.4 から追加された Second level of collapsing では、グルーピング結果をさらにもう一段階まとめて取得することができるようです。
まずはサンプルをみてみましょう。
サンプル
データ
サンプルデータとして、青空文庫さんの作品リストcsvを使用させて頂いています。
1 2 3 |
$ head -2 list_person_all_utf8.csv 人物ID,著者名,作品ID,作品名,仮名遣い種別,翻訳者名等,入力者名,校正者名,状態,状態の開始日,底本名,出版社名,入力に使用した版,校正に使用した版 001257,"アーヴィング ワシントン",056078,"駅伝馬車",旧字旧仮名,"高垣 松雄","雀","小林繁雄",公開,2013-09-20,"スケッチ・ブック","岩波文庫、岩波書店","2010(平成22)年2月23日第31刷","1992(平成4)年2月26日第30刷" |
このデータを Elasticsearch に投入し、Field Collapsing でグルーピングした検索結果と、さらに Second level of collapsing を使った検索結果を比較してみます。
Field Collapsing の検索結果
作品リストを著者ごとにまとめたサンプルです。
それぞれの著者の下に inner_hits として作品が並んでいます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
{ "took" : 5, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 16088, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "HUIPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "会津 八一" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 9, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "HUIPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { "id" : "046513", "title" : "支那の明器", "author" : "会津 八一", "publisher" : "作品社" } }, { "_index" : "aozora", "_type" : "books", "_id" : "GkIPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { "id" : "057975", "title" : "音楽に就いて", "author" : "会津 八一", "publisher" : "中央公論社" } }, { "_index" : "aozora", "_type" : "books", "_id" : "IEIPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { "id" : "057978", "title" : "少年少女におくる言葉", "author" : "会津 八一", "publisher" : "中央公論社" } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "JkIPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "青空文庫 " ] }, "inner_hits" : { "books" : { "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "JkIPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { "id" : "056572", "title" : "青空文庫の提案", "author" : "青空文庫 ", "publisher" : "" } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "J0IPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "青野 季吉" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "J0IPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { "id" : "052214", "title" : "百万人のそして唯一人の文学", "author" : "青野 季吉", "publisher" : "筑摩書房" } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "K0IPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "秋田 滋" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 7, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "K0IPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { "id" : "047074", "title" : "親ごころ", "author" : "秋田 滋", "publisher" : "改造文庫、改造社出版" } }, { "_index" : "aozora", "_type" : "books", "_id" : "LkIPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { "id" : "047076", "title" : "狂人日記", "author" : "秋田 滋", "publisher" : "暁書房" } }, { "_index" : "aozora", "_type" : "books", "_id" : "MEIPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { "id" : "043866", "title" : "初雪", "author" : "秋田 滋", "publisher" : "改造文庫、改造社出版" } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "O0IPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "芥川 竜之介" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 375, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "O0IPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { "id" : "003804", "title" : "悪魔", "author" : "芥川 竜之介", "publisher" : "筑摩書房" } }, { "_index" : "aozora", "_type" : "books", "_id" : "V0IPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { "id" : "000174", "title" : "馬の脚", "author" : "芥川 竜之介", "publisher" : "ちくま文庫、筑摩書房" } }, { "_index" : "aozora", "_type" : "books", "_id" : "YkIPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { "id" : "000123", "title" : "大川の水", "author" : "芥川 竜之介", "publisher" : "角川文庫、角川書店" } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "DEIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "伊丹 万作" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 23, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "DEIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { "id" : "001194", "title" : "一つの世界 ――私信――", "author" : "伊丹 万作", "publisher" : "筑摩書房" } }, { "_index" : "aozora", "_type" : "books", "_id" : "z0IPjWcBtkR6x7Gbkrx4", "_score" : 1.0, "_source" : { "id" : "043868", "title" : "映画と民族性", "author" : "伊丹 万作", "publisher" : "筑摩書房" } }, { "_index" : "aozora", "_type" : "books", "_id" : "0UIPjWcBtkR6x7Gbkrx4", "_score" : 1.0, "_source" : { "id" : "043869", "title" : "映画の普及力とは", "author" : "伊丹 万作", "publisher" : "筑摩書房" } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "FUIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "伊藤 左千夫" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 30, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "FUIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { "id" : "057444", "title" : "井戸", "author" : "伊藤 左千夫", "publisher" : "日本図書センター" } }, { "_index" : "aozora", "_type" : "books", "_id" : "FkIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { "id" : "056526", "title" : "落穂", "author" : "伊藤 左千夫", "publisher" : "アイドル・ブックス、ポプラ社" } }, { "_index" : "aozora", "_type" : "books", "_id" : "HkIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { "id" : "057469", "title" : "子規と和歌", "author" : "伊藤 左千夫", "publisher" : "増進会出版社" } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "NEIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "伊藤 信二" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "NEIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { "id" : "054099", "title" : "冬のしぶき ――母親から獄中の息子に――", "author" : "伊藤 信二", "publisher" : "新日本出版社" } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "NkIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "伊東 忠太" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 5, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "NkIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { "id" : "046334", "title" : "建築の本義", "author" : "伊東 忠太", "publisher" : "萬里閣書房" } }, { "_index" : "aozora", "_type" : "books", "_id" : "N0IPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { "id" : "046335", "title" : "国語尊重", "author" : "伊東 忠太", "publisher" : "萬里閣書房" } }, { "_index" : "aozora", "_type" : "books", "_id" : "OEIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { "id" : "046336", "title" : "日本建築の発達と地震", "author" : "伊東 忠太", "publisher" : "萬里閣書房" } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "S0IPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "伊藤 野枝" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 74, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "S0IPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { "id" : "046560", "title" : "少数と多数", "author" : "伊藤 野枝", "publisher" : "學藝書林" } }, { "_index" : "aozora", "_type" : "books", "_id" : "T0IPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { "id" : "057001", "title" : "書簡 大杉栄宛 (一九一六年五月三一日)", "author" : "伊藤 野枝", "publisher" : "學藝書林" } }, { "_index" : "aozora", "_type" : "books", "_id" : "U0IPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { "id" : "057005", "title" : "書簡 大杉栄宛 (一九一六年五月二七日)", "author" : "伊藤 野枝", "publisher" : "學藝書林" } } ] } } } } ] } } |
Second level of collapsing の検索結果
作品リストを著者ごとにまとめたあとに、Second level of collapsing でさらに出版社ごとにまとめたサンプルです。
それぞれの著者の下にある inner_hits が、さらに出版社ごとにまとまっています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
{ "took" : 15, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 16088, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "HUIPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "会津 八一" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 9, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "HUIPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "作品社" ] } }, { "_index" : "aozora", "_type" : "books", "_id" : "GkIPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "中央公論社" ] } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "JkIPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "青空文庫 " ] }, "inner_hits" : { "books" : { "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "JkIPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "" ] } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "J0IPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "青野 季吉" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "J0IPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "筑摩書房" ] } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "K0IPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "秋田 滋" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 7, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "K0IPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "改造文庫、改造社出版" ] } }, { "_index" : "aozora", "_type" : "books", "_id" : "LkIPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "暁書房" ] } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "O0IPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "芥川 竜之介" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 375, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "O0IPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "筑摩書房" ] } }, { "_index" : "aozora", "_type" : "books", "_id" : "V0IPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "ちくま文庫、筑摩書房" ] } }, { "_index" : "aozora", "_type" : "books", "_id" : "YkIPjWcBtkR6x7GbkbCk", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "角川文庫、角川書店" ] } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "DEIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "伊丹 万作" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 23, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "DEIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "筑摩書房" ] } }, { "_index" : "aozora", "_type" : "books", "_id" : "1EIPjWcBtkR6x7Gbkrx4", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "作品社" ] } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "FUIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "伊藤 左千夫" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 30, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "FUIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "日本図書センター" ] } }, { "_index" : "aozora", "_type" : "books", "_id" : "FkIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "アイドル・ブックス、ポプラ社" ] } }, { "_index" : "aozora", "_type" : "books", "_id" : "HkIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "増進会出版社" ] } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "NEIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "伊藤 信二" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "NEIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "新日本出版社" ] } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "NkIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "伊東 忠太" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 5, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "NkIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "萬里閣書房" ] } } ] } } } }, { "_index" : "aozora", "_type" : "books", "_id" : "S0IPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "author" : [ "伊藤 野枝" ] }, "inner_hits" : { "books" : { "hits" : { "total" : 74, "max_score" : 1.0, "hits" : [ { "_index" : "aozora", "_type" : "books", "_id" : "S0IPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "學藝書林" ] } }, { "_index" : "aozora", "_type" : "books", "_id" : "bUIPjWcBtkR6x7GbkbGk", "_score" : 1.0, "_source" : { }, "fields" : { "publisher" : [ "作品社" ] } } ] } } } } ] } } |
使い方
次に、ドキュメント に従って Second level of collapsing の使い方を確認したいと思います。
Second level of collapsing
グルーピング結果をさらにグルーピングするためには、collapse 句内の inner_hits に collapse 句を追加します。
追加した collapse 句の中では、通常の Field Collapsing と同じように、グルーピングのキーとなるフィールド名を filed に指定します。
下記のクエリでは、aozora というインデックスを検索条件なしで取得して、検索結果を author というフィールドの値ごとにグルーピングし、それをさらに publisher でグルーピングしています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
GET /aozora/_search { "query": { "match_all": {} }, "collapse": { "field": "author", "inner_hits": { "name": "books", "size": 3, "collapse": { "field": "publisher" } } } } |
グループの展開
通常の Field Collapsing では inner_hits 句を追加することで、各グループに含まれるレコードを検索結果に含めることができましたが、残念ながら Second level of collapsing でさらにグルーピングした内容を inner_hits で取得することはできないようです。
Second level of collapsing の基本的な使い方については、以上となります。
まとめ
Second level of collapsing はいかがでしたでしょうか。
二重にグルーピングすることで、WEBサイトやECサービスでの検索結果の一覧性向上や、Kibanaでのログ可視化にも役立てることができそうですね。
弊社の提供するニフティ不動産やニフティアルバイト等の横断検索サービスでも、Elasticsearchの便利な検索機能を活用して利用者様に想像以上の体験を提供できるよう、改善していきたいと思います。
明日は @kanishionori さんがinput要素をCSSだけでいじり倒してくれるそうです!お楽しみに!