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

ElasticsearchでScripting

pisatoshi
April 21, 2014

 ElasticsearchでScripting

pisatoshi

April 21, 2014
Tweet

More Decks by pisatoshi

Other Decks in Technology

Transcript

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


    Ұఆ࣌ؒຖʹϦϩʔυʢΠϯλʔόϧࢦఆՄೳʣ w /BUJWF4DSJQU
 +BWBͰ࣮૷
 ࠷଎
 $-"441"5)্ʹKBSϑΝΠϧΛ഑ஔ
 ىಈ࣌ʹϩʔυɺߋ৽࣌͸ཁ࠶ىಈ
  2. ϑΟʔϧυ஋ͷจࣈྻஔ׵ 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(\"[ϐʔʔ]\")"
 }
 }
 }' ి࿩൪߸ Β͖͠จࣈྻ ΛϚεΫͯ͠ΈΔ
  3. ϑΟʔϧυ஋ͷจࣈྻஔ׵ {
 "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ʹి࿩ͯ͠Ͷɻ" ]
 }
 } ]
 }
 } ϚεΫͯ͠Έͨ݁Ռ
  4. ݕࡧ݁ՌͷϑΟϧλϦϯά 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ϑΟʔϧυ͕ࢦఆ௕Ҏ্ͷυΩϡϝϯτͷΈฦ͢
  5. %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ϑΟʔϧυ͕ࢦఆ௕Ҏ্ͷυΩϡϝϯτͷΈ࡟আ
  6. ݕࡧ݁Ռͷιʔτ 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Ͱιʔτ஋ͷσʔλܕΛࢦఆ
  7. /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;" }" }
  8. Ϋϥελߏ੒Ͱͷ஫ҙ఺ w શͯͷϊʔυʹಉҰεΫϦϓτΛ഑ஔ͢Δ
 ϊʔυؒͰεΫϦϓτ͸ಉظ͞Εͳ͍ w ࠞͥΔͳةݥʂ
 ϊʔυؒͰͷεΫϦϓτͷόʔδϣϯҧ͍ͳͲ͸ݕ஌͠ͳ͍
 ֤ϊʔυ͸໊લղܾ͑͞Ͱ͖Ε͹ϩʔυͨ͠εΫϦϓτΛ࣮ߦ͢Δ
 ಉҰ໊ͰҟͳΔεΫϦϓτΛ഑ஔͨ͠৔߹ɺॲཧ͢ΔϊʔυʹԠͯ͡
 ҟͳΔ݁ՌΛฦ͢

    w εΫϦϓτ͸࣮ߦՄೳͳϊʔυͰͷΈॲཧ͞ΕΔ
 ࣮ߦՄೳͳγϟʔυ͕ଘࡏ͢Δ৔߹͸ͦͷγϟʔυ͚ͩͰ࣮ߦ͢Δ
 ʢ࣮ߦͰ͖ͳ͍γϟʔυʹ͸ॲཧΛৼΓ෼͚ͳ͍ʜͬΆ͍ʣ
 ϓϥΠϚϦʗϨϓϦΧͷશγϟʔυͰεΫϦϓτΛ࣮ߦͰ͖ͳ͍৔߹ɺ ͦͷγϟʔυͰͷݕࡧ͸ΤϥʔʹͳΔ
  9. ·ͱΊ w %ZOBNJD4DSJQU
 खܰʹࢼͤͯ։ൃ޲͚ w 1SFMPBEFE4DSJQU
 ΦʔτϦϩʔυ͸ศར w /BUJWF4DSJQU
 ੑೳ໘Ͱ͸࠷଎


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