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

ElasticSearch入門

 ElasticSearch入門

ElasticSearch

Jun Ohtani

August 19, 2013
Tweet

More Decks by Jun Ohtani

Other Decks in Programming

Transcript

  1. υΩϡϝϯτొ࿥ͷྲྀΕ 1 2 ΧπΦ͸αβΤͷఋ αβΤ͸ϫΧϝͷ࢞ 1 2 ΧπΦ αβΤ ͸

    ͸ ͷ ͷ αβΤ ϫΧϝ ఋ ࢞ υΩϡϝϯτͷొ࿥ ୯ޠʹ෼ׂ 13೥8݄30೔༵ۚ೔
  2. υΩϡϝϯτొ࿥ͷྲྀΕ 1 2 ΧπΦ͸αβΤͷఋ αβΤ͸ϫΧϝͷ࢞ 1 2 ΧπΦ αβΤ ͸

    ͸ ͷ ͷ αβΤ ϫΧϝ ఋ ࢞ ΧπΦ αβΤ 1 1 2 ͸ ͷ ࢞ ϫΧϝ 2 1 2 1 2 1 ఋ 2 υΩϡϝϯτͷొ࿥ ୯ޠʹ෼ׂ ୯ޠ͔Βidͷ഑ྻ͕ Ҿ͚ΔΑ͏ʹ ʹసஔΠϯσοΫε 13೥8݄30೔༵ۚ೔
  3. ݕࡧͷྲྀΕ ΧπΦɹαβΤ ΧπΦ αβΤ 1 1 2 ͸ ͷ ࢞

    ϫΧϝ 2 1 2 1 2 1 ఋ 2 ݕࡧจࣈೖྗ 13೥8݄30೔༵ۚ೔
  4. ݕࡧͷྲྀΕ ΧπΦɹαβΤ ΧπΦ αβΤ ΧπΦ αβΤ 1 1 2 ͸

    ͷ ࢞ ϫΧϝ 2 1 2 1 2 1 ఋ 2 AND ݕࡧจࣈೖྗ ݕࡧจࣈͷύʔε ʴݕࡧΫΤϦԽ 13೥8݄30೔༵ۚ೔
  5. ݕࡧͷྲྀΕ ΧπΦɹαβΤ ΧπΦ αβΤ ΧπΦ αβΤ 1 1 2 ͸

    ͷ ࢞ ϫΧϝ 2 1 2 1 2 1 ఋ 2 AND ݕࡧจࣈೖྗ ݕࡧจࣈͷύʔε ʴݕࡧΫΤϦԽ సஔΠϯσοΫεΛݕࡧ 13೥8݄30೔༵ۚ೔
  6. ݕࡧͷྲྀΕ ΧπΦɹαβΤ ΧπΦ αβΤ ΧπΦ αβΤ 1 1 2 ͸

    ͷ ࢞ ϫΧϝ 2 1 2 1 2 1 ఋ 2 AND ݕࡧจࣈೖྗ ݕࡧจࣈͷύʔε ʴݕࡧΫΤϦԽ సஔΠϯσοΫεΛݕࡧ ˠANDݕࡧͳͷͰidͷ ഑ྻͷANDΛͱΔ 13೥8݄30೔༵ۚ೔
  7. ݕࡧͷྲྀΕ ΧπΦɹαβΤ ΧπΦ αβΤ ΧπΦ αβΤ 1 1 2 ͸

    ͷ ࢞ ϫΧϝ 2 1 2 1 2 1 ఋ 2 AND ݕࡧจࣈೖྗ ݕࡧจࣈͷύʔε ʴݕࡧΫΤϦԽ సஔΠϯσοΫεΛݕࡧ ˠANDݕࡧͳͷͰidͷ ഑ྻͷANDΛͱΔ 13೥8݄30೔༵ۚ೔
  8. ElasticSearchͱ͸ʁ ෼ࢄܕRESTfulݕࡧˍղੳαʔό OSSʢApache Software License Version 2.0ʣ Apache LuceneΛίΞʹ࠾༻ʢ0.90.3͸Lucene 4.4.0ʹରԠʣ

    ϚϧνςφϯτରԠ εΩʔϚϑϦʔˍυΩϡϝϯτࢦ޲ ָ؍తഉଞ੍ޚ ͳͲͳͲ 13೥8݄30೔༵ۚ೔
  9. ΠϯσοΫεߏ੒ʢ࿦ཧʣ ElasticSearchͰ͸࿦ཧߏ੒͕URLͱಉ͡ߏ੒ υΩϡϝϯτͷొ࿥ curl -XPUT http://localhost:9200/books/novel/doc1 -d ‘{...}’ υΩϡϝϯτͷݕࡧʢΠϯσοΫε୯Ґʣ curl

    -XGET http://localhost:9200/books/_search?q=... υΩϡϝϯτͷݕࡧʢλΠϓ୯Ґʣ curl -XGET http://localhost:9200/books/novel/_search?q=... υΩϡϝϯτͷIDʹΑΔऔಘʢυΩϡϝϯτ୯Ґʣ curl -XGET http://localhost:9200/books/novel/doc1 13೥8݄30೔༵ۚ೔
  10. ઃఆؔ࿈ʢجຊʣ ΄ͱΜͲ͢΂ͯͷઃఆʹ͍ͭͯɺREST APIͰઃఆ͕Մೳ ઃఆϑΝΠϧͱͯ͠อଘ΋Մೳ ֬ೝ΋REST APIͰՄೳ Ϋϥελͷঢ়ଶͳͲ΋ ΠϯσοΫεͷ࡞੒ɺ࡟আ΋ curl -XPUT

    'http://localhost:9200/twitter/' curl -XDELETE 'http://localhost:9200/twitter/' curl -XGET 'http://localhost:9200/_cluster/health?pretty=true' 13೥8݄30೔༵ۚ೔
  11. ઃఆؔ࿈ʢಈతϚοϐϯάʣ ϑΟʔϧυ໊ͳͲʹΑΔϑΟʔϧυܕͷਪఆΛઃఆՄೳ SolrͷμΠφϛοΫϑΟʔϧυͷΑΓॊೈͳػೳ mappingsఆٛʹdynamic_templatesͷઃఆΛ഑ྻͰ௥Ճ { "person" : { "dynamic_templates" :

    [ { "template_1" : { "match" : "multi*", "mapping" : { "type" : "multi_field", "fields" : { "{name}" : {"type": "{dynamic_type}", "index" : "analyzed"}, "org" : {"type": "{dynamic_type}", "index" : "not_analyzed"} } } } },... 13೥8݄30೔༵ۚ೔
  12. ઃఆؔ࿈ʢςϯϓϨʔτʣ ΠϯσοΫε୯Ґͷઃఆ΋ςϯϓϨʔτԽՄೳ ΠϯσοΫε໊ʹtemplate͕Ұகͨ͠΋ͷΛద༻ɻorderͷେ͖͍΋ͷͰ্ॻ͖ ྫɿجຊઃఆ͸ϨϓϦΧ0͕ͩɺha_Ͱ͸͡·ΔΠϯσοΫε͸ϨϓϦΧ5 curl -XPUT localhost:9200/_template/main_template -d ' {

    "template" : "*", "order" : "1", "settings" : { "index.number_of_replicas" : 0 } } curl -XPUT localhost:9200/_template/ha_template -d ' { "template" : "ha_*", "order" : "10", "settings" : { "index.number_of_replicas" : 5 } } 13೥8݄30೔༵ۚ೔
  13. σʔλߏ଄ʢωετʣ ωετɺ਌ࢠɺΦϒδΣΫτͳͲͷσʔλ΋ొ࿥Մೳ ʢSolr͸جຊϑϥοτͳσʔλͷΈʣ ྫɿTγϟπͷ৭ͱαΠζͷ૊Έ߹Θͤʢωετʣ { "variation" : [ { "color"

    : "blue", "size" : 4 }, { "name" : "green", "size" : 6 } ] } { "query" : { "nested" : { "path" : "variation", "query" : { "bool" : { "must" : [ { "term" : {"variation.name" : "green"} }, { "term" : {"variation.size" : "6"} } ] } } } } } ΫΤϦ σʔλ 13೥8݄30೔༵ۚ೔
  14. σʔλߏ଄ʢ਌ࢠʣ ਌ࢠؔ܎ʹؔ͢Δݕࡧ΋ՄೳʢmappingͰઃఆ΋ඞཁʣ ࢠσʔλొ࿥࣌ʹ਌IDΛࢦఆ $ curl -XPUT localhost:9200/shop/variation/1?parent=1 -d '{ "color"

    : "red", "size" : "XL" }' ࢠσʔλ $ curl -XPUT localhost:9200/shop/cloth/1 -d '{ "name" : "something" }' ਌σʔλ $ curl -XPUT localhost:9200/shop/variation/2?parent=1 -d '{ "color" : "green", "size" : "S" }' ࢠσʔλ { "has_child" : { "type" : "variation", "query" : { "bool" : { "must" : [ { "term": {"size" : "XL"} }, { "term": {"color" : "red"} } ] } } } } ΫΤϦ 13೥8݄30೔༵ۚ೔
  15. ศརػೳʢϩάʣ index slow log/search slow logʢSolrʹ͸ͳ͍ػೳʣ ΠϯσοΫε࣌ɺݕࡧ࣌ʹॲཧ͕͖͍࣌ؒ͠஋Λ௒͑ͨΒϩάग़ྗ ϩάϨϕϧ͝ͱʹ஋ΛઃఆՄೳ #index.search.slowlog.threshold.query.warn: 10s

    #index.search.slowlog.threshold.query.info: 5s #index.search.slowlog.threshold.query.debug: 2s #index.search.slowlog.threshold.query.trace: 500ms #index.search.slowlog.threshold.fetch.warn: 1s #index.search.slowlog.threshold.fetch.info: 800ms #index.search.slowlog.threshold.fetch.debug: 500ms #index.search.slowlog.threshold.fetch.trace: 200ms 13೥8݄30೔༵ۚ೔
  16. ݕࡧػೳʢPercoratorʣ ΫΤϦΛΠϯσοΫεԽͯ͠ొ࿥ ొ࿥ࡁΈͷΫΤϦʹώοτ͢Δσʔλ͔Ͳ͏͔ΛಛఆͷϦΫΤετʹσʔλ Λૹ৴͢Δ͜ͱͰΘ͔Δ࢓૊Έʢར༻ՄೳͳΫΤϦ͸Ұ෦੍ݶ͋Γʣ curl -XPUT localhost:9200/_percolator/test/kuku -d '{ "query"

    : { "term" : { "field1" : "value1" } } } curl -XGET localhost:9200/test/type1/_percolate -d '{ "doc" : { "field1" : "value1" } }' And the matches are part of the response: {"ok":true, "matches":["kuku"]} percoratorͷొ࿥ percoratorͷ֬ೝ 13೥8݄30೔༵ۚ೔
  17. ొ࿥ػೳʢόϧΫॲཧʣ େྔͷॲཧΛߦ͏ͨΊͷAPIʢ_bulkʣ http://www.elasticsearch.org/guide/reference/api/bulk/ UDP΋ར༻Մೳ { "index" : { "_index" :

    "test", "_type" : "type1", "_id" : "1" } } { "field1" : "value1" } { "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } } { "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } } { "field1" : "value3" } { "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1"} } { "doc" : {"field2" : "value2"} } όϧΫॲཧσʔλྫ 13೥8݄30೔༵ۚ೔
  18. ࢀߟॻ੶ͱ͔ ElasticSearch ServerʢPackt Publishingɺ2013/02݄ൃചʣ http://www.packtpub.com/elasticsearch-server-for-fast-scalable-flexible- search-solution/book http://elasticsearchserverbook.com Mastering ElasticSearchʢPackt Publishingɺ2013/12ൃച༧ఆʣ

    http://www.packtpub.com/mastering-elasticsearch-querying-and-data- handling/book Elasticsearch in ActionʢManning Publicationsɺ2014/य़ɺMEAPߪೖՄʣ http://www.manning.com/hinman/ 13೥8݄30೔༵ۚ೔
  19. LuceneͱESͷόʔδϣϯ ElasticSearch Lucene 0.90.3 4.4 0.90.2 4.3.1 0.90.1 4.3 0.90.0

    4.2.1 0.90.0rc2 4.2.1 0.90.0rc1 4.2 0.20.6 3.6.2 0.20.5 3.6.2 0.90.0 beta1 4.1 13೥8݄30೔༵ۚ೔