Skip to content

Elasticsearch

changwu edited this page Mar 10, 2016 · 1 revision

安裝

$ brew install elasticsearch
==> Downloading https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.2.tar.gz
######################################################################## 100.0%
==> Caveats
Data:    /usr/local/var/elasticsearch/elasticsearch_user/
Logs:    /usr/local/var/log/elasticsearch/elasticsearch_user.log
Plugins: /usr/local/var/lib/elasticsearch/plugins/
Config:  /usr/local/etc/elasticsearch/

To have launchd start elasticsearch at login:
  ln -sfv /usr/local/opt/elasticsearch/*.plist ~/Library/LaunchAgents
Then to load elasticsearch now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.elasticsearch.plist
Or, if you don't want/need launchctl, you can just run:
  elasticsearch --config=/usr/local/opt/elasticsearch/config/elasticsearch.yml

使用

PUT

建立一個 twitter 使用者

$ curl -XPUT 'http://localhost:9200/twitter/user/changwu' -d '{ "name" : "使凱窩今" }'

返回訊息

{"_index":"twitter","_type":"user","_id":"changwu","_version":1,"created":true}

建立 id 為 1 的 tweet,內容含發文者、時間與訊息

$ curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '
{
	"user": "winwu",
	"postDate": "2009-11-15T13:12:00",
	"message": "今天要做什麼呢"
}'

返回訊息

{"_index":"twitter","_type":"tweet","_id":"1","_version":1,"created":true}

GET

取得 user 是 changwu 的資料

$ curl -XGET 'http://localhost:9200/twitter/user/changwu?pretty=true'

查詢返回

{
  "_index" : "twitter",
  "_type" : "user",
  "_id" : "changwu",
  "_version" : 1,
  "found" : true,
  "_source":{ "name" : "使凱窩今" }
}

取得 id 為 1 的 tweet 內容

$ curl -XGET 'http://localhost:9200/twitter/tweet/1?pretty=true'

查詢返回

{
  "_index" : "twitter",
  "_type" : "tweet",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source":
{
    "user": "winwu",
    "postDate": "2009-11-15T13:12:00",
    "message": "今天要做什麼呢"
}
}

搜尋

搜尋所有 winwu 的推文

$ curl -XGET 'http://localhost:9200/twitter/tweet/_search?q=user:winwu&pretty=true'

返回

{
  "took" : 125,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.30685282,
    "hits" : [ {
      "_index" : "twitter",
      "_type" : "tweet",
      "_id" : "1",
      "_score" : 0.30685282,
      "_source":
{
    "user": "winwu",
    "postDate": "2009-11-15T13:12:00",
    "message": "今天要做什麼呢"
}
    } ]
  }
}
Clone this wiki locally