1. 程式人生 > >ElasticSearch+ElasticSearch的搜房網實戰(在windows搭建的es分散式環境)

ElasticSearch+ElasticSearch的搜房網實戰(在windows搭建的es分散式環境)

可以參考windows環境下ElasticSearch5以上版本安裝head外掛  兩個重點的連結!!!!

es的elasticsearch.yml檔案的配置(在elasticsearch.yml中新增下面的如下的配置)

master配置如下

http.cors.enabled: true
http.cors.allow-origin: "*"

cluster.name: wanan
node.name: master
node.master: true
network.host: 127.0.0.1
http.port: 9200   #服務埠號,在同一機器下必須不一樣  
discovery.zen.ping.unicast.hosts: ["127.0.0.1:9200","127.0.0.1:9201"]  

slave1的配置如下:

http.cors.enabled: true
http.cors.allow-origin: "*"

cluster.name: wanan
node.name: slave1

network.host: 127.0.0.1
http.port: 9201   #服務埠號,在同一機器下必須不一樣  

discovery.zen.ping.unicast.hosts: ["127.0.0.1"]  

slave2的配置如下:

http.cors.enabled: true
http.cors.allow-origin: "*"

cluster.name: wanan
node.name: slave2

network.host: 127.0.0.1
http.port: 9202   #服務埠號,在同一機器下必須不一樣  

discovery.zen.ping.unicast.hosts: ["127.0.0.1"]  

es和head外掛分屬於兩個不同的東西,兩個東西的訪問時有跨域的問題,需要有跨域問題的一些配置。

elasticsearch-5.6.5\config\elasticsearch.yml的配置

master的配置

http.cors.enabled: true 
http.cors.allow-origin: "*"

cluster.name: wanan
node.name: master
node.master: true
network.host: 0.0.0.0

[2018-06-15T00:11:55,630][INFO ][o.e.t.TransportService   ] [master] publish_address {192.168.1.114:9300}, bound_addresses {192.168.1.114:9300}
[2018-06-15T00:11:55,642][INFO ][o.e.b.BootstrapChecks    ] [master] bound or publishing to a non-loopback or non-link-local address, enforcing bootstrap checks
[2018-06-15T00:11:58,725][INFO ][o.e.c.s.ClusterService   ] [master] new_master {master}{yki9TDhwQoyuqZjyVGfr0A}{iA-LDjLuTra_Yc9pZeOOtg}{192.168.1.114}{192.168.1.114:9300}, reason: zen-disco-elected-as-master ([0] nodes joined)
[2018-06-15T00:11:58,778][INFO ][o.e.h.n.Netty4HttpServerTransport] [master] publish_address {192.168.1.114:9200}, bound_addresses {192.168.1.114:9200}
[2018-06-15T00:11:58,793][INFO ][o.e.n.Node               ] [master] started
[2018-06-15T00:11:58,825][INFO ][o.e.g.GatewayService     ] [master] recovered [0] indices into cluster_state

[2018-06-15T00:13:48,336][INFO ][o.e.t.TransportService   ] [master] publish_address {192.168.1.114:9300}, bound_addresses {[::]:9300}
[2018-06-15T00:13:48,349][INFO ][o.e.b.BootstrapChecks    ] [master] bound or publishing to a non-loopback or non-link-local address, enforcing bootstrap checks
[2018-06-15T00:13:51,490][INFO ][o.e.c.s.ClusterService   ] [master] new_master {master}{yki9TDhwQoyuqZjyVGfr0A}{PciM4mRaTNGPGBVJUi3XNQ}{192.168.1.114}{192.168.1.114:9300}, reason: zen-disco-elected-as-master ([0] nodes joined)
[2018-06-15T00:13:51,572][INFO ][o.e.g.GatewayService     ] [master] recovered [0] indices into cluster_state
[2018-06-15T00:13:52,797][INFO ][o.e.h.n.Netty4HttpServerTransport] [master] publish_address {192.168.1.114:9200}, bound_addresses {[::]:9200}
[2018-06-15T00:13:52,798][INFO ][o.e.n.Node               ] [master] started

es的索引建立(利用postman這個工具)

postman中 post 方式 192.168.56.1:9200/people

body中填寫以下json資料

{
	"settings":{
		"number_of_shards": 3,
		"number_of_replicas": 1
	},
	"mappings":{
		"man":{
			"properties": {
				"name": {
					"type": "text"
				},
				"country": {
					"type": "keyword"
				},
				"age": {
					"type": "integer"
				},
				"date": {
					"type": "date",
					"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
				}
			}
		},
		"women": {
		}
	}
}

es的插入

postman中 post 方式 192.168.56.1:9200/people

body中填寫以下json資料

{
"name":"wanan2",
"country":"china",
"age":30,
"date":"1995-07-27"
}

es的修改

postman中 post 方式 192.168.56.1:9200/people/man/1/_update

body中填寫以下json資料

api方式

{
"doc" :{
"name":"xiaohuamao"
}
}
指令碼方式
{
	"script":{
		"lang":"painless",
		"inline":"ctx._source.age += 10"
	}
}

es的刪除

刪除一個文件

postman中 delete 方式 192.168.56.1:9200/people/man/1

刪除一個索引

es的查詢

postman中 post 方式 192.168.56.1:9200/people/man/1/_update

192.168.56.1:9200/people/man/AWQHn-88cR5y_omfKHEn (單條記錄查詢)

192.168.56.1:9200/people/man(多條記錄查詢)配合下面的body語句使用

報錯with the same id but is a different node instance解決辦法

然後百度發現問題,是因為複製虛擬機器時,elsticsearch時,將elsticsearch資料夾下的data資料夾一併複製了。而在前面測試時,data資料夾下已經產生了data資料,於是報上面的錯誤。

解決辦法:刪除elsticsearch資料夾下的data資料夾下的節點資料

binggo!

習慣:先把錯誤貼上下來到edit-plus中然後分析問題的關鍵字,然後再百度