DRYな備忘録

Don't Repeat Yourself.

Elasticsearchでindexにaliasつける

% curl ":9200"
{
  "status" : 200,
  "name" : "Kubik",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "1.5.1",
    "build_hash" : "5e38401bc4e4388537a615569ac60925788e1cf4",
    "build_timestamp" : "2015-04-09T13:41:35Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
}
% curl -XPUT ":9200/foo_v1"
{"acknowledged":true}%
% curl ":9200/_aliases"
{"foo_v1":{"aliases":{}}}%
% curl -X POST ":9200/_aliases" -d '
{
  "actions": [
    { "add": { "index": "foo_v1", "alias": "foo" } }
  ]
}'
{"acknowledged":true}%
% curl ":9200/_aliases?pretty"
{
  "foo_v1" : {
    "aliases" : {
      "foo" : { }
    }
  }
}
%

としとくと、

% curl -X POST ":9200/foo/user?pretty" -d '{"name":"otiai10", "lang": "golang", "bio": "すもももももももものうち。吾輩は猫である。"}'
{
  "_index" : "foo_v1",
  "_type" : "user",
  "_id" : "AUzAPlKptWwY5Ss2YpXW",
  "_version" : 1,
  "created" : true
}
% curl ":9200/foo/user/_search?pretty"
{
  "took" : 19,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "foo_v1",
      "_type" : "user",
      "_id" : "AUzAPlKptWwY5Ss2YpXW",
      "_score" : 1.0,
      "_source":{"name":"otiai10", "lang": "golang", "bio": "すもももももももものうち。吾輩は猫である。"}
    } ]
  }
}
%                                                                                                  %

fooというaliasに対する操作がfoo_v1へ向いていることがわかる

おまけ

kuromojiが効いてることを確認

% curl ":9200/foo/user/_search?pretty" -d '{"query":{"match":{"bio":"吾輩"}}}'
{
  "took" : 15,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.076713204,
    "hits" : [ {
      "_index" : "foo_v1",
      "_type" : "user",
      "_id" : "AUzAPlKptWwY5Ss2YpXW",
      "_score" : 0.076713204,
      "_source":{"name":"otiai10", "lang": "golang", "bio": "すもももももももものうち。吾輩は猫である。"}
    } ]
  }
}
% curl ":9200/foo/_analyze?pretty&field=bio" -d "Aカップ揉みたい"
{
  "tokens" : [ {
    "token" : "a",
    "start_offset" : 0,
    "end_offset" : 1,
    "type" : "word",
    "position" : 1
  }, {
    "token" : "カップ",
    "start_offset" : 1,
    "end_offset" : 4,
    "type" : "word",
    "position" : 2
  }, {
    "token" : "揉み",
    "start_offset" : 4,
    "end_offset" : 6,
    "type" : "word",
    "position" : 3
  }, {
    "token" : "たい",
    "start_offset" : 6,
    "end_offset" : 8,
    "type" : "word",
    "position" : 4
  } ]
}
%