概要

現在Cultural Japanプロジェクトにおける検索アプリの更新を進めており、多言語データのアグリゲーションを行う必要がありました。本記事では、その方法に関する調査結果の備忘録です。

データ

データとしては、以下のように、agential(人物を示す)フィールドに、idjaenの値を持つケースを想定します。

{}"]ag{}en"""tjeiianda"""l:::":"""Kc[ahtnsa"um,seh:ika,"Hokusai",

上記のデータに対して、idでフィルタリング処理などを行いつつ、言語設定に合わせてjaまたはenの値を表示することを想定します。

理想的には、aggregationの結果として、以下のようなデータを取得したいです。

jaを指定した場合)

{}"]bu{}ck"""ekidtedosy"c"":_::c"o["cuhnnta"m:"e,:1",

enを指定した場合)

{}"]bu{}ck"""ekidtedosy"c"":_::c"o["cuKhnantta"sm:ues:1hika,",Hokusai",

方法1: nested aggregationの利用

以下の記事を参考に、nested aggregationを試します。

https://discuss.elastic.co/t/aggregeations-with-different-keys-and-values-label-and-id/274218

DP{}P{}G{}EUUELT"}T"]T"}"]"}Emaq,_,aTta"}tg{}tu"}s"g"}Eeppeeeeboagaspr"}sn"""sro"]ugsg"}"}ttioattjeityofre"en,aenpg""}/iand/"li{}cn:ne"g"}"}sgeetp_a"""_:"lettspgi,ltsrnyr"}"}"}dl:::s:t"}"i{itasd"}a"}"ttpoi,j,eo"e{en:aaet""tbt:iiepd"a"n"c:"""a{re""}lldh::e"ee"ea"e"t"t"t/Kcr"spq[""""rflrf{sl:r:y:y:y1[ahc:tau"}:::{{mi"mi""tppptnhetebse:se::"i{e{e{esa[dhro"]{{""l"lne""""um""yofa:d{:d{{es:::,se::"li{}g""s"h::"le{:{:t:"""i{":t"}nekkkka{ett""d{eeeag{re"iaa"yyy,e"raagg,www"n:mgleeoooHt"e"nnrrroi[:nttdddkatii"""ul{iaas"alla,l..i.ij"ida,d""":"chname:"

この場合、以下のような結果が返却されます。

{}"""}"}"}tt_,h,aois""""i"}""]g"}omhtssftt,mhgakeaoukaso""ai{}rg""}"}"drtcii"tvrxteedl,i_dacplaae_s"""""}gnoa"""]d"""]:oslepe:llls"_____atcbdsb"dsbu""sed"uacitiss"]ti_eouu{}ouu{}9tsd"{eto:nydcoaiaclcmc:cmc,"::f":"irdp"oug{}olo"__k""__k""u:oe[eerren"ucoekd{coekd:{5l:{:n"x":ecn"""sn:otteootteo,"0"""etjei":tuhsycuhsycf01::""iand"{ne""_ne""_a:,,::1:a""":{trctrcl0"":l:__::o__::os5"."_,0":::{eduedue,e0td.{1ro["nro["n,q,eo0:""",rctrcct"sc,Kco_"o_h"t"[ahrcrcn",tn_o:_oa:,sauu"uum"umpn,1pne1,septpt:h:e"e"irrk_:_:abb,o0o0""u,u,,Hnnoddk""us::ai00",,,

aggregations.agentiallabelidが返却されますが、冗長な結果のように思われます。

方法2: 文字列の結合

以下の記事の質問で挙げられている方法を試します。

https://stackoverflow.com/questions/70545830/aggregation-that-returns-the-users-name-and-id

fc-agentialフィールドを用意し、$$$を区切り文字として、idjaenの値を結合した値を入れてみました。

DP{}P{}G{}EUUELT"}T"]"]T"}"]"}Ema,fq,_,aTta"}tg{}c"tu"}s"g"}Eeppee-ceeboagaspr"}"}sn"""ahsro"]ugsg"}ttioa,fttjeigntyofre"etenpg"}c"/iandea/"li{}cn:ne"sgeep-t_a"""nm_:"lettrftsrnr"}"}"}aydl:::tes:t"}"i{imi"ttoi,j,egpo"i:e{et:aase:iipd"a"n"eec:"""aa{re"ll"leae"t"t"tn"/Kclr"ra["":d{slr:y:y:yt:1[ah"c:mg:"""tpppitn:h"e{:::i{e{e{ea"sa$[:n{e"""lk"um[$t"{{s:::"e,se${if":yh:ac:"""wil-kkk{ok.a{eeeraigyyyd,$dewww""$"noooH$:trrroKidddka"a"""utclssh"aunisa"hm,iek:a,Ho"kusai"

この場合、以下のような結果が返却されます。

{}"""}"}"}tt_,h,aois""""i"}""]g"}omhtssftt,mhgakeaoukaso""ai{}rg"""]"drtcii"tvrxteedsb_dacplaae_s"""""}gnouu{}:oslepe:llls"_____atcmcu""sed"uacitiss"]ti__k""9tsd"{eto:nydcoaiacoekd6"::f":"irdp"oug{}olotteo4u:oe[eerren"uhsyc,:{5l:{:n"x":ecn"""sne""_,"0"""etjei":trcf01::""iand__::oa:,,::1:a""":{edul0"":lro["ns5"."_,0":::{rccte,e0td.{o_h",q,eo0:"""rcn"sc,Kc_oa:t"[ahuum",tnpne1,sapt:"ume",serh:_:ibko0$au,$,n$"dH"ok:u$s0$a,$iK"a,tsushika,Hokusai",

nestedを使用しない分、クエリや結果は簡潔ですが、取得後にデータを修正する必要があります。

方法3: a nested top-hits aggregation?

以下の記事の回答として挙げられている方法を試します。

https://stackoverflow.com/questions/70545830/aggregation-that-returns-the-users-name-and-id

DP{}P{}G{}EUUELT"}T"]T"}"]"}Emaq,_,aTta"}tg{}tu"}s"g"}Eeppeeeeboagaspr"}sn"""sro"]ugsg"}"}ttioattjeityofre"et,aenpg"}/iand/"li{}cn:ne"g"}sgeep_a"""_:"lettrfgdtsrnr"}"}"}dl:::s:t"}"i{imiso"}"ttoi,j,eo"e{et:aase"ct:iipd"a"n"c:"""a{re"ll"l:"o"eae"t"t"t/Kcr"ra["":d:ps{slr:y:y:y1[ahc:mg:"{_i""tppptnh"e{:{hz::i{e{e{esa[:n{iee""""umt"t"{{s:::,se{ias:"h:ag":"""ile:1kkkk.n{eeeait{yyy,diwww""aoooH:lrrro.dddk"j"""ucash"ania"m,e:"

この場合、以下のような結果が返却されます。

{}"""}"}"}tt_,h,aois""""i"}""]g"}omhtssftt,mhgakeaoukaso""ai{}rg"""]"drtcii"tvrxteedsb_dacplaae_s"""""}gnouu{}:oslepe:llls"_____atcmcu""sed"uacitiss"]ti__k"""}7tsd"{eto:nydcoaiacoekdd,"::f":"irdp"oug{}olotteoo"}u:oe[eerren"uhsycch:{5l:{:n"x":ecn"""sne""_"i"}""],"0"""etjei":trctt,mhf01::""iand__::o:so""ai{}a:,,::1:a""":{edu"tvrxtl0"":lro["n{aae_s"""""}s5"."_,0":::{rct:llls"_____e,e0td.{o_""uacitiss"],q,eo0:"""rc{eto:nydcoa"sc,Kc_o::"irdp"oug{}t"[ahuu"oe[eerre",tnpn,1{:n"x":ecn""",sapt,"""etjei"ume"1::""iand,ser,::1:a"""h:_:0"":lib"."_,0":::ko0e0td.{au,q,eo0:""",n"sc,Kc"dt"[ahH"",tno,sak:"umu,ses0h:a,iik"a,,"Hokusai",

aggregations.agentialに結果の1例が含まれるため、そこからidjaの関係を抽出できます。方法2に比べて、fc-agentialのような項目を用意する必要がない点が利点ですが、これも結果が冗長なように思われます。

まとめ

Elastic Searchにおける異なるキーと値(ラベルとID)を持つアグリゲーションの調査結果をまとめました。参考になりましたら幸いです。

また、上記で挙げた3つ方法以外にも良い方法があるように思います。もしご存じの方がいらっしゃれば教えていただけますと幸いです。