Elasticsearch 中文分詞 analysis plugin: ik

$ wget https://github.com/medcl/elasticsearch-rtf/archive/master.zip
$ unzip master.zip

$ sudo cp -r elasticsearch-rtf-master/plugins/analysis-ik /usr/share/elasticsearch2/plugins/
$ sudo cp -r elasticsearch-rtf-master/config/ik /etc/elasticsearch2/

in /etc/elasticsearch/elasticsearch.yml

index:
  analysis:
    analyzer:
      ik:
        alias: [news_analyzer_ik,ik_analyzer]
        type: org.elasticsearch.index.analysis.IkAnalyzerProvider
      ik_max_word:
        type: ik
        use_smart: false
      ik_smart:
        type: ik
        use_smart: true

index.analysis.analyzer.default.type : "ik"
$ sudo service elasticsearch restart

ref:
https://github.com/medcl/elasticsearch-analysis-ik/
https://github.com/medcl/elasticsearch-rtf

Install Elasticsearch and Plugins on Ubuntu

Installation

$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java7-installer

$ wget -qO - http://packages.elasticsearch.org/GPG-KEY-elasticsearch | sudo apt-key add -
$ sudo echo "deb http://packages.elasticsearch.org/elasticsearch/1.5/debian stable main" >> /etc/apt/sources.list
$ sudo apt-get update
$ sudo apt-get install elasticsearch

$ sudo service elasticsearch start

# try it out
$ curl -XGET 'http://127.0.0.1:9200/'

ref:
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/setup-repositories.html
https://www.digitalocean.com/community/tutorials/how-to-install-elasticsearch-on-an-ubuntu-vps
http://www.vpsee.com/2014/05/install-and-play-with-elasticsearch/

Plugins

$ sudo su root
$ cd /usr/share/elasticsearch
$ bin/plugin -i elasticsearch/license/latest
$ bin/plugin -i elasticsearch/shield/latest

$ sudo service elasticsearch restart

ref:
https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-plugins.html

testify: Testing in Go

假設你把 project 放在 $GOPATH/src/github.com/your_username/ 底下

test

in xxx_test.go

package pangu_test

import (
    "github.com/stretchr/testify/suite"
    "github.com/your_username/your_project"
    "testing"
)

type YourTestSuite struct {
    suite.Suite
}

func (suite *YourTestSuite) TestFunction1() {
    suite.Equal("expected", "actual")
}

// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestYourTestSuite(t *testing.T) {
    suite.Run(t, new(YourTestSuite))
}

go test 會自動執行當前目錄下的所有以 _test.go 結尾的檔案
更準確地說是執行所有以 Test 開頭的 functions

# run tests in the current directory
$ go test

# run tests in the current directory and sub-directories
$ go test ./...

ref:
http://golang.org/pkg/testing/
https://github.com/stretchr/testify

How to specify test resources?
https://groups.google.com/forum/#!topic/golang-nuts/VPVlIiO5yXw

example

in example_test.go

這個檔名是固定的
裡面的內容會出現在 godoc 的 Example 條目底下
確切地說
ExampleFunction1() 這個 function 的內容就會出現在 Function1() 的 Example 條目中
你可以執行 godoc -http=:6060 在本機查看

因為也是以 _test.go 結尾的關係
所以執行 go test 的時候也會執行這個檔案
貼心地幫你確認你的 example 程式碼是不是真的能夠動

package pangu_test

import (
    "fmt"
    "github.com/your_username/your_project"
)

func ExampleFunction1() {
    fmt.Println(`The knights who say "Ni"!`)
    // Output:
    // The knights who say "Ni"!
}

benchmark

in benchmark_test.go

這個檔名是固定的

package pangu_test

import (
    "testing"
)

func BenchmarkFunction1(b *testing.B) {
    for i := 0; i < b.N; i++ {
        doShit()
    }
}
# This will run tests and benchmarks
$ go test -bench=.

# If you want to skip the tests, you can do so by passing a regex to the -run flag that will not match anything. 
$ go test -run=XXX -bench=.

coverage

$ go test -cover -coverprofile=cover.out -covermode=count
$ go tool cover -func=cover.out
$ go tool cover -html=cover.out

# or

$ go test -cover -covermode=count ./...

Integrate with Travis CI and Coveralls

language: go

go:
  - 1.4.2
  - 1.4.1
  - 1.4
  - 1.3.3
  - 1.3.2
  - 1.3.1
  - 1.3
before_install:
  - go get github.com/axw/gocov/gocov
  - go get github.com/mattn/goveralls
  - go get golang.org/x/tools/cmd/cover
script:
  - goveralls -service=travis-ci

ref:
http://docs.travis-ci.com/user/languages/go/
https://coveralls.zendesk.com/hc/en-us/articles/201342809-Go

Go commands

go build

$ go build hello.go
$ ./hello

# 會編譯當前目錄下的所有 .go
# 但是只有 package 是 main 而且擁有 main() 的那個檔案會被編譯成執行檔
# 執行檔的檔名會是目錄名稱而不是 .go 的檔名
$ go build

go build 一個非 package main 的 .go 檔案不會產生任何東西
要執行 go install 才會產生 .a 並安裝到 $GOPATH/pkg

go run

$ go run hello.go

# equals to

$ go build hello.go
$ ./hello

go get

# download and install packages and dependencies
# 會把原始碼存到 `$GOPATH/src`,執行檔放到 `$GOPATH/bin`
$ go get -v github.com/nsf/gocode

# the -u flag instructs get to use the network to update the named packages and their dependencies
$ go get -u github.com/astaxie/bat

# 可以直接安裝某個 repo 的某個子目錄
$ go get github.com/rlmcpherson/s3gof3r/gof3r

# 如果你的 repo 已經在電腦裡了,你可以執行以下指令安裝 dependencies
# the -t flag instructs get to also download the packages required to build the tests
$ go get -t ./...

go install

# 假設 hello_bin 目錄中有一個同名的 hello_bin.go 而它的 package 是 main
# 則 go install 就會編譯 hello_bin.go 為執行檔 hello_bin 然後放到 $GOPATH/bin
$ cd "$GOPATH/src/github.com/vinta/hello_bin"
$ go install

# 假設 hello_lib 目錄中有一個同名的 hello_lib.go 而它的 package 是 hello_lib
# 則 go install 就會編譯 hello_lib.go 為 hello_lib.a 然後放到 $GOPATH/pkg
# 然後你就可以在程式中 import "github.com/vinta/hello_lib"
$ cd "$GOPATH/src/github.com/vinta/hello_lib"
$ go install

go get YOUR_PATH 的 YOUR_PATH 是相對於 $GOPATH/src 的路徑

ref:
http://golang.org/doc/code.html
https://github.com/golang/go/wiki/PackagePublishing

go list

# list all installed packages
$ cd "$GOPATH"
$ go list ./...

go test

# 會編譯並執行目錄下的所有 `*_test.go` 檔案
$ go test