Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ElasticsearchでScripting

Ec94734aeba732630e328317bf059aa5?s=47 pisatoshi
April 21, 2014

 ElasticsearchでScripting

Ec94734aeba732630e328317bf059aa5?s=128

pisatoshi

April 21, 2014
Tweet

Transcript

  1. &MBTUJDTFBSDIͰ 4DSJQUJOH ୈճ&MBTUJDTFBSDIษڧձ
 !QJTBUPTIJ

  2. ࣗݾ঺հ ໊લୌా੟ݾ !QJTBUPTIJ  ॴଐגࣜձࣾ෋࢜௨ιϑτ΢ΣΞςΫϊϩδʔζ ੜଉ஍ͧ͠ʔ͔ ͓࢓ࣄ#BB4ج൫։ൃɻݘͷࢄาɻ

  3. 4DSJQUJOH֓ཁ w ΫΤϦ࣮ߦ࣌ͷࣜධՁΛಠࣗͷϩδοΫͰΧελϚΠζͰ͖Δ
 ݕࡧ݁ՌͷϑΟʔϧυɾϑΟϧλϦϯά
 υΩϡϝϯτͷείΞ
 ݕࡧ݁ՌҰཡͷιʔτॱ
 ʜFUD w ଟ࠼ͳݴޠαϙʔτ
 .7&-

    %FGBVMU 
 +BWB4DSJQU
 1ZUIPO
 (SPPWZ
 +BWB
  4. େ·͔ͳ෼ྨ w %ZOBNJD4DSJQU
 ΫΤϦʹ௚઀εΫϦϓτΛهड़
 ͓खܰ w 1SFMPBEFE4DSJQU
 αʔόʹεΫϦϓτϑΝΠϧΛ഑ஔ &4@)0.&DPOpHTDSJQUT 


    Ұఆ࣌ؒຖʹϦϩʔυʢΠϯλʔόϧࢦఆՄೳʣ w /BUJWF4DSJQU
 +BWBͰ࣮૷
 ࠷଎
 $-"441"5)্ʹKBSϑΝΠϧΛ഑ஔ
 ىಈ࣌ʹϩʔυɺߋ৽࣌͸ཁ࠶ىಈ
  5. 4BNQMFT

  6. ϑΟʔϧυ஋ͷจࣈྻஔ׵ curl ‘http://localhost:9200/test/foo/_search?pretty' -d '
 {
 "query" : {
 "match_all"

    : {}
 },
 "fields" : [
 "body"
 ],
 "script_fields" : {
 “replaced" : {
 "script": "java.util.regex.Pattern.compile(\"[0-9]{2,4}-[0-9]{2,4}- [0-9]{4}\").matcher(_source.body).replaceAll(\"[ϐʔʔ]\")"
 }
 }
 }' ి࿩൪߸ Β͖͠จࣈྻ ΛϚεΫͯ͠ΈΔ
  7. ϑΟʔϧυ஋ͷจࣈྻஔ׵ {
 "took" : 317,
 "timed_out" : false,
 "_shards" :

    {
 "total" : 5,
 "successful" : 5,
 "failed" : 0
 },
 "hits" : {
 "total" : 1,
 "max_score" : 1.0,
 "hits" : [ {
 "_index" : "test",
 "_type" : "foo",
 "_id" : "5y7HmrMFQFq2y81KiMKXPQ",
 "_score" : 1.0,
 "fields" : {
 "replaced" : [ "͜Μʹͪ͸ɻ[ϐʔʔ]ʹి࿩ͯ͠Ͷɻ" ],
 "body" : [ "͜Μʹͪ͸ɻ090-1234-5678ʹి࿩ͯ͠Ͷɻ" ]
 }
 } ]
 }
 } ϚεΫͯ͠Έͨ݁Ռ
  8. ݕࡧ݁ՌͷϑΟϧλϦϯά curl -XPOST ‘http://localhost:9200/test/state/_search?pretty' -d '
 {
 "query" : {


    "filtered" : {
 "query" : {
 "match_all" : {}
 },
 "filter" : {
 "script" : {
 "params": {
 "len": 10
 },
 "script" : "_source.name!=null && _source.name.length()>=len"
 }
 }
 }
 }
 }' lOBNFzϑΟʔϧυ͕ࢦఆ௕Ҏ্ͷυΩϡϝϯτͷΈฦ͢
  9. %FMFUFCZRVFSZ curl -XDELETE ‘http://localhost:9200/test/state/_query' -d '
 {
 "query" : {


    "filtered" : {
 "query" : {
 "match_all" : {}
 },
 "filter" : {
 "script" : {
 "params": {
 "len": 10
 },
 "script" : "_source.name!=null && _source.name.length()>=len"
 }
 }
 }
 }
 }' %FMFUFCZRVFSZͰ΋࢖͑Δ
 lOBNFzϑΟʔϧυ͕ࢦఆ௕Ҏ্ͷυΩϡϝϯτͷΈ࡟আ
  10. ݕࡧ݁Ռͷιʔτ curl -XPOST ‘http://localhost:9200/test/state/_search?pretty' -d '
 {
 "query": {
 "match_all":

    {}
 },
 "sort": {
 "_script": {
 "script": "_source.name != null?_source.name.length():0",
 "type" : "number"
 }
 }
 }' lOBNFzϑΟʔϧυͷ௕͞Λιʔτ஋ͱͯ͠ฦ͢
 lUZQFzͰιʔτ஋ͷσʔλܕΛࢦఆ
  11. /BUJWF4DSJQU

  12. /BUJWF4DSJQUͷ࣮૷ w /BUJWF4DSJQU'BDUPSZΛ࣮૷ͯ͠OFX4DSJQU ϝιουΛΦʔόʔϥΠυ w "CTUSBDU4FBSDI4DSJQUΛܧঝͯ͠SVO ϝιουΛΦʔόʔϥΠυ public class MyScript

    extends AbstractSearchScript {" ! public static class Factory extends AbstractComponent implements NativeScriptFactory{" ! @Inject" public Factory(Node node, Settings settings) {" }" ! @Override" public ExecutableScript newScript(@Nullable Map<String, Object> params) {" return new MyScript();" }" }" ! private MyScript() {" }" ! @Override" public Object run() {" return SOME_RESPONSE;" }" }
  13. /BUJWF4DSJQUͰඇਖ਼نԽ /PEFΦϒδΣΫτʹΞΫηεͰ͖ΔͷͰ$MJFOU΋࢖͑Δ͸ͣ ඥ෇͍ͨυΩϡϝϯτΛऔಘͯ͠ϑΟʔϧυʹηοτ͢Ε͹ +0*/ͬΆ͍͜ͱ͕Ͱ͖ͦ͏ ɾɾɾͱࢥͬͨΒ΋͏͋ͬͨ
 
 IUUQTHJUIVCDPNJNPUPWFMBTUJDTFBSDIOBUJWFTDSJQUFYBNQMF ରԠ͕ͩɺʮTΛখจࣈʹʯमਖ਼͢Δ͚ͩͰରԠՄೳ

  14. ࠷ޙʹ

  15. Ϋϥελߏ੒Ͱͷ஫ҙ఺ w શͯͷϊʔυʹಉҰεΫϦϓτΛ഑ஔ͢Δ
 ϊʔυؒͰεΫϦϓτ͸ಉظ͞Εͳ͍ w ࠞͥΔͳةݥʂ
 ϊʔυؒͰͷεΫϦϓτͷόʔδϣϯҧ͍ͳͲ͸ݕ஌͠ͳ͍
 ֤ϊʔυ͸໊લղܾ͑͞Ͱ͖Ε͹ϩʔυͨ͠εΫϦϓτΛ࣮ߦ͢Δ
 ಉҰ໊ͰҟͳΔεΫϦϓτΛ഑ஔͨ͠৔߹ɺॲཧ͢ΔϊʔυʹԠͯ͡
 ҟͳΔ݁ՌΛฦ͢

    w εΫϦϓτ͸࣮ߦՄೳͳϊʔυͰͷΈॲཧ͞ΕΔ
 ࣮ߦՄೳͳγϟʔυ͕ଘࡏ͢Δ৔߹͸ͦͷγϟʔυ͚ͩͰ࣮ߦ͢Δ
 ʢ࣮ߦͰ͖ͳ͍γϟʔυʹ͸ॲཧΛৼΓ෼͚ͳ͍ʜͬΆ͍ʣ
 ϓϥΠϚϦʗϨϓϦΧͷશγϟʔυͰεΫϦϓτΛ࣮ߦͰ͖ͳ͍৔߹ɺ ͦͷγϟʔυͰͷݕࡧ͸ΤϥʔʹͳΔ
  16. Ϋϥελߏ੒Ͱͷ஫ҙ఺ ී௨ʹݕࡧՄೳ ݕࡧϦΫΤετ͜ͳ͍ ී௨ʹݕࡧՄೳ ݕࡧࣦഊ ˞l#FMBTDPzͷΈͰεΫϦϓτ࣮ߦՄೳͳ৔߹

  17. ·ͱΊ w %ZOBNJD4DSJQU
 खܰʹࢼͤͯ։ൃ޲͚ w 1SFMPBEFE4DSJQU
 ΦʔτϦϩʔυ͸ศར w /BUJWF4DSJQU
 ੑೳ໘Ͱ͸࠷଎


    εΫϦϓτͱͯ͠ͷखܰ͞͸͋·Γͳ͍ w ϊʔυؒͰͷεΫϦϓτͷಉظ͸ͯ͘͠Εͳ͍
 શϊʔυʹಉ͡εΫϦϓτΛ഑උ͢Δඞཁ͕͋Δ
 େن໛ͳΫϥελͩͱ໘౗
  18. ͓͠·͍