Elasticsearch

ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์น˜

http.cors.enabled: true
http.cors.allow-origin: "*"
# "*"์ผ ๊ฒฝ์šฐ ๋ชจ๋“  ๋„๋ฉ”์ธ ์ ‘์† ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ณด์•ˆ ํ•ด์ œ์™€ ๊ฐ™์Œ

2.x ๋ฒ„์ „

๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ๋ฐ ์ž…์ถœ๋ ฅ

RDB Elasticsearch
Database Index
Table Type
Record Document
Column Field
Schema Mapping
Function Elasticsearch
HTTP method
SQL
Create POST INSERT
Read GET SELECT
Update PUT UPDATE
Delete DELETE DELETE

๋ฐ์ดํ„ฐ ์ž…๋ ฅ

curl -XPOST http://localhost:9200/books/book/1 -d '
{
  "title" : "elasticsearch guide",
  "author" : "Kim",
  "date" : "2016-05-22",
  "pages" : 250
}
'

๋ฐ์ดํ„ฐ ์กฐํšŒ

curl -XGET http://localhost:9200/books/book/1
# ๋˜๋Š”
curl -XGET http://localhost:9200/books/book/1\?pretty

๋ฐ์ดํ„ฐ ๊ฐฑ์‹ 

curl -XPOST http://localhost:9200/books/book/1 -d '
{
  "title" : "elasticsearch guide",
  "author" : ["Kim", "Heo"],
  "date" : "2016-05-22",
  "pages" : 300
}
'

๋ฐ์ดํ„ฐ ์‚ญ์ œ

curl -XDELETE http://localhost:9200/books/book/1

๋ฐ์ดํ„ฐ ๋ฐฐ์น˜ ์ž…๋ ฅ(_bulk API)

{ "delete" : { "_index" : "books", "_type" : "book", "_id" : "1" } }
{ "update" : { "_index" : "books", "_type" : "book", "_id" : "2" } }
{ "doc" : { "date" : "2014-05-01" } }
{ "create" : { "_index" : "books", "_type" : "book", "_id" : "3" } }
{ "title" : "Elasticsearch Guide II", "author" : "Park", "pages" : 400 }
curl -XPOST http://localhost:9200/_bulk?pretty --data-binary @data.txt

index list

curl localhost:9200/_cat/indices?v

๊ฒ€์ƒ‰

#๋ฐ์ดํ„ฐ ์ ์žฌ
cd elasticsearch/cd 05.๊ฒ€์ƒ‰
curl -XPOST http://localhost:9200/_bulk --data-binary @5_1_books.json
curl -XPOST http://localhost:9200/_bulk --data-binary @5_2_magazines.json

๊ฒ€์ƒ‰ API

curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "term" : { "author" : "william" }
  }
}'
# ๋˜๋Š”
curl 'localhost:9200/books/_search?pretty' -d '
{
  'query' : {
    'term' : { "author" : "william" }
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "from" : 1,
  "size" : 2,
  "stored_fields" : ["title", "category"],
  "query" : {
    "term" : { "author" : "william" }
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "_source" : false,
  "query" : {
    "term" : { "author" : "william" }
  }
}'
- fields
curl 'localhost:9200/magazines/_search?pretty' -d '
{
  "_source" : [ "title", "c*" ]
}'
- `include`, `exclude`
curl 'localhost:9200/magazines/_search?pretty' -d '
{
  "_source" : {
    "include" : "c*"
  }
}'
# exclude
curl 'localhost:9200/magazines/_search?pretty' -d '
{
  "_source" : {
    "include" : "c*",
    "exclude" : "*ry"
  }
}'

์–ด๊ทธ๋ฆฌ๊ฒŒ์ด์…˜(aggregation)

curl -XPUT http://localhost:9200/hotels/ -d '
{
  "mappings" : {
    "hotel" : {
      "properties" : {
        "name" : { "type" : "string"},
        "stars" : { "type" : "long"},
        "rooms" : { "type" : "long"},
        "location" : { "type" : "geo_point"},
        "city" : { "type" : "string"},
        "address" : { "type" : "string"},
        "internet" : { "type" : "boolean"},
        "service" : { "type" : "string", "index" : "not_analyzed"},
        "checkin" : { "type" : "date", "format" : "dateOptionalTime"}
      }
    }
  }
}'
curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "price_min" : {
      "min" : { "field" : "price" }
    }
  }
}'
curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "price_max" : {
      "max" : { "field" : "price" }
    }
  }
}'
curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "price_sum" : {
      "sum" : { "field" : "price" }
    }
  }
}'
curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "price_avg" : {
      "avg" : { "field" : "price" }
    }
  }
}'
curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "price_cnt" : {
      "value_count" : { "field" : "price" }
    }
  }
}'
curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "price_stats" : {
      "stats" : { "field" : "price" }
    }
  }
}'
curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "price_ex_stats" : {
      "extended_stats" : { "field" : "price" }
    }
  }
}'

๊ธ€๋กœ๋ฒŒ ์–ด๊ทธ๋ฆฌ๊ฒŒ์ด์…˜

curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "query" : {
    "term" : { "name" : "seoul" }
  },
  "aggs" : {
    "avg_price" : {
      "avg" : { "field" : "price" }
    }
  }
}'
curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "query" : {
    "term" : { "name" : "seoul" }
  },
  "aggs" : {
    "all_price" : {
      "global" : {},
      "aggs" : {
        "avg_price" : {
          "avg" : { "field" : "price" }
        }
      }
    }
  }
}'

ํ•„ํ„ฐ ์–ด๊ทธ๋ฆฌ๊ฒŒ์ด์…˜

curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "filter_name" : {
      "filter" : {
        "term" : { "name" : "seoul" }
      },
      "aggs" : {
        "avg_price" : {
          "avg" : { "field" : "price" }
        }
      }
    }
  }
}'

๋ˆ„๋ฝ(missing) ์–ด๊ทธ๋ฆฌ๊ฒŒ์ด์…˜

curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "missing_service" : {
      "missing" : { "field" : "service" },
      "aggs" : {
        "avg_price" : {
          "avg" : { "field" : "price" }
        }
      }
    }
  }
}'

ํ…€ ์–ด๊ทธ๋ฆฌ๊ฒŒ์ด์…˜

curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "term_stars" : {
      "terms" : { "field" : "stars" },
      "aggs" : {
        "avg_price" : {
          "avg" : { "field" : "price" }
        }
      }
    }
  }
}'
curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "term_stars" : {
      "terms" : {
        "field" : "stars",
        "order" : { "_term" : "desc" }
      },
      "aggs" : {
        "avg_price" : {
          "avg" : { "field" : "price" }
        }
      }
    }
  }
}'
curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "term_stars" : {
      "terms" : {
        "field" : "stars",
        "order" : { "avg_price" : "asc" }
      },
      "aggs" : {
        "avg_price" : {
          "avg" : { "field" : "price" }
        }
      }
    }
  }
}'

๋ฒ”์œ„, ๋‚ ์งœ ๋ฒ”์œ„ ์–ด๊ทธ๋ฆฌ๊ฒŒ์ด์…˜

curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "range_room" : {
      "range" : {
        "field" : "rooms",
        "ranges" : [{"to":500}, {"from":500, "to":1000}, {"from":1000}]
      },
      "aggs" : {
        "avg_price" : {
          "avg" : { "field" : "price" }
        }
      }
    }
  }
}'
curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "range_room" : {
      "range" : {
        "field" : "rooms",
        "keyed" : true,
        "ranges" : [{"to":500}, {"from":500, "to":1000}, {"from":1000}]
      },
      "aggs" : {
        "avg_price" : {
          "avg" : { "field" : "price" }
        }
      }
    }
  }
}'
curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "date_r_checkin" : {
      "date_range" : {
        "field" : "checkin",
        "format" : "yyyy-MM-dd",
        "ranges" : [{"to": "now-4M"}, {"from": "now-4M"}]
      }
    }
  }
}'
curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "date_r_checkin" : {
      "date_range" : {
        "field" : "checkin",
        "format" : "yyyy-MM-dd hh:mm:ss",
        "ranges" : [{"to": "2014-03-05 12:30:45"}, {"from": "2014-03-05 12:30:45"}]
      }
    }
  }
}'

ํžˆ์Šคํ† ๊ทธ๋žจ

curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "histo_rooms" : {
      "histogram" : {
        "field" : "rooms",
        "interval" : 500
      }
    }
  }
}'
curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "histo_rooms" : {
      "histogram" : {
        "field" : "rooms",
        "interval" : 300,
        "min_doc_count" : 0
      }
    }
  }
}'

์œ„์น˜, ๊ฑฐ๋ฆฌ

curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "aggs" : {
    "geo_location" : {
      "geo_distance" : {
        "field" : "location",
        "origin" : "37.52, 126.98",
        "distance_type" : "plane",
        "unit" : "km",
        "ranges" : [{"to":3},{"from":3, "to":6},{"from":6, "to":9},{"from":9}]
      }
    }
  }
}'

์งˆ์˜(QueryDSL)

curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "term" : {
      "title" : "prince"
    }
  }
}
'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "bool" : {
      "filter" : {
        "term" : {
          "title" : "prince"
        }
      }
    }
  }
}
'

Query

curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "term" : {
      "title" : "prince"
    }
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "terms" : {
      "title" : ["prince", "king"]
    }
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "match" : {
      "title" : "The And"
    }
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "match" : {
      "title" : {
        "query" : "The And",
        "operator" : "and"
      }
    }
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "match" : {
      "title" : {
        "query" : "prince king",
        "analyzer" : "whitespace"
      }
    }
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "match" : {
      "title" : {
        "query" : "and the",
        "type" : "phrase"
      }
    }
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "multi_match" : {
      "fields" : [ "title", "plot" ],
      "query" : "prince king"
    }
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "bool" : {
      "must" : {
        "term" : { "title" : "the" }
      },
      "must_not" : {
        "term" : { "plot" : "prince" }
      },
      "should" : [
        {"term" : { "title" : "time" } },
        {"term" : { "title" : "world" } }
      ]
    }
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "query_string" : {
      "query" : "title:prince"
    }
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "query_string" : {
      "query" : "prince king",
      "default_field" : "plot",
      "default_operator" : "and"
    }
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "prefix" : {
      "title" : "prin"
    }
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "range" : {
      "pages" : { "gte" : 50, "lt" : 150 }
    }
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "range" : {
      "written" : {
        "gte" : "1600-01-01",
        "lt" : "1699-12-31"
      }
    }
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "match_all" : {}
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "fuzzy" : {
      "title" : "tree"
    }
  }
}'
curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "query" : {
    "geo_bounding_box" : {
      "location" : {
        "top_left" : { "lat" : 38.00, "lon" : 126.00 },
        "bottom_right" : { "lat" : 37.00, "lon" : 127.00 }
      }
    }
  }
}'
curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "query" : {
    "geo_distance" : {
      "distance" : "5km",
      "location" : { "lat" : 37.52, "lon" : 126.98 }
    }
  }
}'
curl 'localhost:9200/hotels/_search?pretty' -d '
{
  "query" : {
    "geo_polygon" : {
      "location" : {
        "points" : [
          { "lat" : 38.00, "lon" : 127.00 },
          { "lat" : 37.00, "lon" : 127.00 },
          { "lat" : 38.00, "lon" : 128.00 }
        ]
      }
    }
  }
}'

๋งคํ•‘

curl 'localhost:9200/books/_mapping?pretty'
curl -XPUT 'http://localhost:9200/books/_mapping/book' -d '
{
  "book" : {
    "properties" : {
      "read" : { "type" : "boolean"}
    }
  }
}'

๋‚ด์žฅํ•„๋“œ

curl -XDELETE 'http://localhost:9200/books'

curl -XPUT 'http://localhost:9200/books' -d '
{
  "mappings" : {
    "book" : {
      "_source" : { "enabled"  : false }
    }
  }
}'

curl -XPOST localhost:9200/_bulk --data-binary @5_1_books.json

curl 'http://localhost:9200/books/_search?q=prince&pretty'
curl -XPUT 'http://localhost:9200/books' -d '
{
  "mappings" : {
    "book" : {
      "_source" : {
        "includes" : ["title", "author", "category"]
      }
    }
  }
}'
curl -XPUT 'http://localhost:9200/books' -d '
{
  "mappings" : {
    "book" : {
      "_source" : {
        "excludes" : ["p*"]
      }
    }
  }
}'
curl -XPUT 'http://localhost:9200/books' -d '
{
  "mappings" : {
    "book" : {
      "_all" : { "enabled" : true },
      "properties" : {
        "title" : {
          "include_in_all" : true,
          "type" : "string"
        },
        "plot" : {
          "include_in_all" : false,
          "type" : "string"
        }
      }
    }
  }
}'

๋ฐ์ดํ„ฐ ํƒ€์ž…

์ด๋ฆ„ ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
store ํ•„๋“œ๊ฐ’ ์ €์žฅ ์—ฌ๋ถ€ false
index ๋ถ„์„๊ธฐ ์ ์šฉ ์—ฌ๋ถ€ analyzed, not_analyzed, no .
boost ํ•„๋“œ ๊ฐ€์ค‘์น˜ 1.0
null_value ํ•„๋“œ ์—†๋Š” ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’ ์ง€์ • .
analyzer ๋ถ„์„๊ธฐ ์ง€์ • .
index_analyzer ๋ฐ์ดํ„ฐ ์ƒ‰์ธ์— ์‚ฌ์šฉ๋  ๋ถ„์„๊ธฐ ์ง€์ • .
search_analyzer ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰์— ์‚ฌ์šฉ๋  ๋ถ„์„๊ธฐ ์ง€์ • .
include_in_all _all ๋งคํ•‘ ํ•„๋“œ ์ ์šฉ๋œ ๊ฒฝ์šฐ ์ƒ‰์ธ ์—ฌ๋ถ€ ์ง€์ • .
ignore_above ์ง€์ •๊ฐ’๋ณด๋‹ค ํฐ ํฌ๊ธฐ์˜ ๋ฌธ์ž์—ด ์ƒ‰์ธ ์ œ์™ธ .
curl -XPUT 'localhost:9200/test_nums' -d '
{
  "mappings" : {
    "test_num" : {
      "properties" : {
        "num_val" : { "type" : "integer", "ignore_malformed" : true }
      }
    }
  }
}'
curl -XPUT 'localhost:9200/test_nums/test_num/1' -d '
{
  "num_val": "hello"
}'
curl 'localhost:9200/test_nums/test_num/1'
curl 'localhost:9200/test_nums/_search?pretty' -d '
{
  "aggs" : {
    "num_stat" : {
      "stats" : { "field" : "num_val" }
    }
  }
}'
curl -XPUT localhost:9200/test_geos/ -d '
{
  "mappings" : {
    "test_geo" : {
      "properties" : {
        "name" : { "type" : "string" },
        "location" : { "type" : "geo_point" }
      }
    }
  }
}'
curl -XPUT 'localhost:9200/test_geos/test_geo/1' -d '
{
  "name" : "Conrad Seoul",
  "location" : "37.525308, 126.926644"
}'
curl 'http://localhost:9200/test_geos/_search?pretty' -d '
{
  "query" : {
    "geo_bounding_box" : {
      "location" : {
        "top_left": { "lat" : 37.53, "lon" : 126.92 },
        "bottom_right" : {"lat" : 37.52, "lon" : 126.93 }
      }
    }
  }
}'
curl -XPUT localhost:9200/test_geos/ -d '
{
  "mappings" : {
    "test_geo" : {
      "properties" : {
        "location" : {
          "type" : "geo_shape",
          "precision" : 10
        }
      }
    }
  }
}'

๋‹ค์ค‘ํ•„๋“œ

curl -XPUT 'localhost:9200/books' -d '
{
  "mappings" : {
    "book" : {
      "properties" : {
        "title" : {
          "type" : "string", "index" : "analyzed",
          "fields" : {
            "raw" : { "type" : "string", "index" : "not_analyzed" }
          }
        }
      }
    }
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "query" : {
    "term" : { "title.raw" : "The Prince and the Pauper" }
  }
}'
curl -XPUT 'localhost:9200/books' -d '
{
  "mappings" : {
    "book" : {
      "properties" : {
        "title" : {
          "type" : "string", "index" : "analyzed",
          "fields" : {
            "tokens" : {
              "type" : "token_count",
              "store" : true,
              "analyzer" : "standard"
            }
          }
        }
      }
    }
  }
}'
curl 'localhost:9200/books/_search?pretty' -d '
{
  "fields" : [ "title", "title.tokens" ],
  "query" : {
    "term" : { "title" : "the" }
  }
}'

ํ•„๋“œ ๋ณต์‚ฌ

curl -XPUT 'localhost:9200/books' -d '
{
  "mappings" : {
    "book" : {
      "properties" : {
        "title" : { "type" : "string", "copy_to" : "pk_data" },
        "author" : { "type" : "string", "copy_to" : "pk_data" },
        "pk_data" : { "type" : "string", "store" : true }
      }
    }
  }
}'
curl -XPUT 'localhost:9200/books' -d '
{
  "mappings" : {
    "book" : {
      "properties" : {
        "title" : { "type" : "string", "copy_to" : ["title_1", "title_2"] },
        "title_1" : { "type" : "string", "store" : true },
        "title_2" : { "type" : "string", "store" : true }
      }
    }
  }
}'

๋ถ„์„

curl -XPOST 'localhost:9200/_analyze?tokenizer=whitespace&pretty' -d 'Around the World in Eighty Days'

curl -XPOST 'localhost:9200/_analyze?tokenizer=whitespace&filters=lowercase&pretty' -d 'Around the World in Eighty Days'

curl -XPOST 'localhost:9200/_analyze?tokenizer=whitespace&filters=lowercase,stop&pretty' -d 'Around the World in Eighty Days'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "whitespace",
          "filter" : [ "lowercase", "stop" ]
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?pretty' -d 'Around the World in Eighty Days'

๋ถ„์„๊ธฐ

curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "standard" : {
          "type" : "standard",
          "stopwords" : ["in", "the", "world"],
          "max_token_length" : 512
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=standard&pretty' -d 'Around the World in Eighty Days'
echo 'in
the
world' > config/stopword_list.txt
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "stop" : {
          "type" : "stop",
          "stopwords_path" : "stopword_list.txt"
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=stop&pretty' -d 'Around the World in Eighty Days'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "pattern" : {
          "type" : "pattern",
          "lowercase" : false,
          "pattern" : "[A-Z]"
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=pattern&pretty' -d 'Around the World in Eighty Days'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "pattern" : {
          "type" : "pattern",
          "pattern" : "\\d"
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=pattern&pretty' -d 'Around the World in 80 Days'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "language" : {
          "type" : "english"
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=language&pretty' -d '์‚ผ๊ตญ์ง€(ไธ‰ๅœ‹ๅฟ—)'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "language" : {
          "type" : "cjk"
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=language&pretty' -d '์‚ผ๊ตญ์ง€(ไธ‰ๅœ‹ๅฟ—)'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "snowball" : {
          "type" : "snowball",
          "pattern" : "english"
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=snowball&pretty' -d 'Around the World in Eighty Days'

ํ† ํฌ๋‚˜์ด์ €

curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "standard"
        }
      }
    }
  }
}'

# ๋˜๋Š”

curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "my_tokenizer"
        }
      },
      "tokenizer" : {
        "my_tokenizer" : {
          "type" : "standard"
        }
      }
    }
  }
}'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "my_ngram"
        }
      },
      "tokenizer" : {
        "my_ngram" : {
          "type" : "nGram",
          "min_gram" : "2",
          "max_gram" : "3",
          "token_chars" : [ "letter", "digit" ]
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d 'Around the World in 80 Days'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "my_edgeNGram"
        }
      },
      "tokenizer" : {
        "my_edgeNGram" : {
          "type" : "nGram",
          "min_gram" : "2",
          "max_gram" : "4",
          "token_chars" : [ "letter", "digit" ]
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d 'Around the World in 80 Days'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "my_hierarchy"
        }
      },
      "tokenizer" : {
        "my_hierarchy" : {
          "type" : "path_hierarchy"
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d 'Program/Search/Elasticsearch'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "my_hierarchy"
        }
      },
      "tokenizer" : {
        "my_hierarchy" : {
          "type" : "path_hierarchy",
          "replacement" : "__html__quot;,
          "skip" : 1
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d 'Program/Search/Elasticsearch'

ํ† ํฐํ•„ํ„ฐ

curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "standard",
          "filter" : ["standard"]
        }
      }
    }
  }
}'

# ๋˜๋Š”

curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "standard",
          "filter" : ["my_tokenfilter"]
        }
      },
      "filter" : {
        "my_tokenfilter" : {
          "type" : "standard"
        }
      }
    }
  }
}'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "standard",
          "filter" : ["my_tokenfilter"]
        }
      },
      "filter" : {
        "my_tokenfilter" : {
          "type" : "length",
          "min" : 3, "max" : 5
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d 'Around the World in Eighty Days'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "standard",
          "filter" : ["my_tokenfilter"]
        }
      },
      "filter" : {
        "my_tokenfilter" : {
          "type" : "shingle",
          "max_shingle_size" : 3,
          "min_shingle_size" : 3
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d 'Around the World in Eighty Days'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "standard",
          "filter" : ["stop", "my_tokenfilter"]
        }
      },
      "filter" : {
        "my_tokenfilter" : {
          "type" : "shingle",
          "max_shingle_size" : 3,
          "min_shingle_size" : 3,
          "output_unigrams" : false,
          "token_separator" : "-",
          "filler_token" : "*"
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d 'Around the World in Eighty Days'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "whitespace",
          "filter" : ["my_tokenfilter"]
        }
      },
      "filter" : {
        "my_tokenfilter" : {
          "type" : "word_delimiter"
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d "Father's Wi-Fi SmartPhone, SD3000-12-Delux"
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "whitespace",
          "filter" : ["my_keyword", "my_stemmer"]
        }
      },
      "filter" : {
        "my_keyword" : {
          "type" : "keyword_marker",
          "keywords" : ["swimming"]
        },
        "my_stemmer" : {
          "type" : "stemmer",
          "name" : "english"
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d 'Birds are flying, fishes are swimming, children are playing'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "standard",
          "filter" : ["keyword_repeat", "my_stemmer"]
        }
      },
      "filter" : {
        "my_stemmer" : {
          "type" : "stemmer",
          "name" : "english"
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d 'Birds are flying, fishes are swimming, children are playing'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "standard",
          "filter" : ["keyword_repeat", "my_stemmer", "my_unique"]
        }
      },
      "filter" : {
        "my_stemmer" : {
          "type" : "stemmer",
          "name" : "english"
        },
        "my_unique" : {
          "type" : "unique"
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d 'Birds are flying, fishes are swimming, children are playing'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "standard",
          "filter" : ["snowball", "lowercase", "my_filter"]
        }
      },
      "filter" : {
        "my_filter" : {
          "type" : "synonym",
          "synonyms" : ["quick, fast", "jump, hop => hop"]
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d 'The Quick Rabbit Jumped'
echo 'quick, fast
jump, hop => hop' > config/synonym.txt
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "standard",
          "filter" : ["snowball", "lowercase", "my_filter"]
        }
      },
      "filter" : {
        "my_filter" : {
          "type" : "synonym",
          "synonyms_path" : "synonym.txt"
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d 'The Quick Rabbit Jumped'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "standard",
          "filter" : ["my_filter"]
        }
      },
      "filter" : {
        "my_filter" : {
          "type" : "dictionary_decompounder",
          "word_list" : ["base", "ball"]
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d 'I play baseball and basketball'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "standard",
          "filter" : ["reverse"]
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d 'Around the World in Eighty Days'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "standard",
          "filter" : ["my_filter"]
        }
      },
      "filter" : {
        "my_filter" : {
          "type" : "truncate",
          "length" : 4
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d 'Around the World in Eighty Days'
cd $ELASTICSEARCH_HOME
mkdir -p ../config/hunspell/en_US
cp en_US.* ../config/hunspell/en_US

mkdir -p ../config/hunspell/ko_KR
cp ko_KR.* ../config/hunspell/ko_KR

curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "standard",
          "filter" : ["my_filter"]
        }
      },
      "filter" : {
        "my_filter" : {
          "type" : "hunspell",
          "locale" : "ko_KR"
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d '๋™ํ•ด๋ฌผ๊ณผ ๋ฐฑ๋‘์‚ฐ์ด ๋งˆ๋ฅด๊ณ  ๋‹ณ๋„๋ก'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "standard",
          "filter" : ["my_filter"]
        }
      },
      "filter" : {
        "my_filter" : {
          "type" : "cjk_bigram"
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d '์‚ผ๊ตญ์ง€(ไธ‰ๅœ‹ๅฟ—)'
curl -XPUT 'localhost:9200/books' -d '
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "tokenizer" : "standard",
          "filter" : ["my_filter"]
        }
      },
      "filter" : {
        "my_filter" : {
          "type" : "keep",
          "keep_words" : ["the","Eighty","Days"]
        }
      }
    }
  }
}'
curl -XPOST 'localhost:9200/books/_analyze?analyzer=my_analyzer&pretty' -d 'Around the World in Eighty Days'

ํ•œ๊ธ€ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ

# elasticsearch 5.1.1
./bin/elasticsearch-plugin install https://oss.sonatype.org/service/local/repositories/releases/content/org/bitbucket/eunjeon/elasticsearch-analysis-seunjeon/5.1.1.1/elasticsearch-analysis-seunjeon-5.1.1.1.zip
wget https://oss.sonatype.org/service/local/repositories/releases/content/org/bitbucket/eunjeon/elasticsearch-analysis-seunjeon/5.1.1.1/elasticsearch-analysis-seunjeon-5.1.1.1.zip
./bin/elasticsearch-plugin install file:///home/ec2-user/local/elasticsearch/elasticsearch-analysis-seunjeon-5.1.1.1.zip
# elasticsearch 2.4.1 ์ดํ•˜
./bin/elasticsearch-plugin install org.bitbucket.eunjeon/elasticsearch-analysis-seunjeon/2.4.0.1
#!/usr/bin/env bash

ES='http://localhost:9200'
ESIDX='seunjeon-idx'

curl -XDELETE "${ES}/${ESIDX}?pretty"
sleep 1
curl -XPUT "${ES}/${ESIDX}/?pretty" -d '{
  "settings" : {
    "index":{
      "analysis":{
        "analyzer":{
          "korean":{
            "type":"custom",
            "tokenizer":"seunjeon_default_tokenizer"
          }
        },
        "tokenizer": {
          "seunjeon_default_tokenizer": {
            "type": "seunjeon_tokenizer",
            "index_eojeol": false,
            "user_words": ["๋‚„๋ผ+๋น ๋น ,-100", "c\\+\\+", "์–ด๊ทธ๋กœ", "๋ฒ„์นด์ถฉ", "abc๋งˆํŠธ"]
          }
        }
      }
    }
  }
}'

sleep 1

echo "# ์‚ผ์„ฑ/N ์ „์ž/N"
curl -XGET "${ES}/${ESIDX}/_analyze?analyzer=korean&pretty" -d '์‚ผ์„ฑ์ „์ž'

echo "# ๋น ๋ฅด/V ์ง€/V"
curl -XGET "${ES}/${ESIDX}/_analyze?analyzer=korean&pretty" -d '๋นจ๋ผ์ง'

echo "# ์Šฌํ”„/V"
curl -XGET "${ES}/${ESIDX}/_analyze?analyzer=korean&pretty" -d '์Šฌํ”ˆ'

echo "# ์ƒˆ๋กญ/V ์‚ฌ์ „/N ์ƒ์„ฑ/N"
curl -XGET "${ES}/${ESIDX}/_analyze?analyzer=korean&pretty" -d '์ƒˆ๋กœ์šด์‚ฌ์ „์ƒ์„ฑ'

echo "# ๋‚„๋ผ/N ๋น ๋น /N c++/N"
curl -XGET "${ES}/${ESIDX}/_analyze?analyzer=korean&pretty" -d '๋‚„๋ผ๋น ๋น  c++'
curl -XPOST 'localhost:9200/${ES}/_analyze?analyzer=korean&pretty' -d '๋™ํ•ด๋ฌผ๊ณผ ๋ฐฑ๋‘์‚ฐ์ด ๋งˆ๋ฅด๊ณ  ๋‹ณ๋„๋ก'

๋ถ€๋ถ„์‚ญ์ œ

curl -XDELETE 'http://localhost:9200/twitter/tweet/_delete_by_query?q=user:kimchy'

#or
curl -XPOST 'http://localhost:9200/twitter/tweet/_delete_by__query' -d '
{
  "query" : {
    "term" : {
      "user" : "kimchy"
    }
  }
}'

cluster

ubuntu plugin

wget https://github.com/mobz/elasticsearch-head/archive/master.zip
bin/plugin install file:/path/to/master.zip

์ฐธ๊ณ 

What Else?
inflearn react api server -50% ํ• ์ธ์ฟ ํฐ: 20652-ab1f1cd4c373 buy me a coffee