概念
Index
類(lèi)似于關(guān)系型數(shù)據(jù)庫(kù)中的database稼锅。索引只是一個(gè)邏輯上的空間缀雳,物理上是分為多個(gè)文件來(lái)管理的贤斜。命名必須全小寫(xiě)懂酱。
Type
類(lèi)似于關(guān)系型數(shù)據(jù)庫(kù)中的table,根據(jù)用戶需求每個(gè)index中可以新建任意數(shù)量的type茄猫。
Document
類(lèi)似于關(guān)系型數(shù)據(jù)庫(kù)中的row狈蚤。每個(gè)Document是一個(gè)json格式的文本。
Mapping
更像是一個(gè)用來(lái)定義每個(gè)字段類(lèi)型的語(yǔ)義規(guī)范在mysql中類(lèi)似sql語(yǔ)句划纽,在ES中經(jīng)過(guò)包裝后脆侮,都被封裝為友好的Restful風(fēng)格的接口進(jìn)行操作。這一點(diǎn)也是為什么開(kāi)發(fā)人員更愿意使用ES或者compass這樣的框架而不是直接使用Lucene的一個(gè)原因勇劣。
安裝
OS參數(shù)調(diào)整
vi /etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
vi /etc/sysctl.conf
vm.max_map_count=655360
ES參數(shù)調(diào)整
elasticsearch-2.4.5/config/jvm.options
-Xms512m
-Xmx512m
elasticsearch-2.4.5/config/elasticsearch.yml
network.host: 0.0.0.0
http.port: 9200
elasticsearch-analysis-ik安裝
下載版本:elasticsearch-analysis-ik-1.10.6.zip
下載地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
上傳elasticsearch-analysis-ik-1.10.6.zip至elasticsearch-2.4.5/plugins/analysis-ik(需先創(chuàng)建analysis-ik目錄)
解壓并修改plugin-descriptor.properties
java.version=1.8
elasticsearch.version=2.4.5
啟動(dòng)
elasticsearch-2.4.5/bin/elasticsearch
驗(yàn)證
GET http://{host}:9200
常見(jiàn)REST API 操作
查看集群狀態(tài)
GET http://{host}:9200/_cat/health?v
查看所有節(jié)點(diǎn)狀態(tài)
GET http://{host}:9200/_cat/nodes?v
創(chuàng)建索引
PUT http://{host}:9200/test_index
創(chuàng)建索引并指定配置
PUT http://{host}:9200/test_index
{
"settings": {
"refresh_interval": "1s",
"number_of_shards" : 5,
"number_of_replicas" : 0
},
"mappings": {
"test_type": {
"dynamic": false,
"properties": {
"name": {
"type": "string",
"index": "analyzed",
"analyzer": "ik"
},
"about": {
"type": "string",
"index": "analyzed",
"analyzer": "ik"
},
"interests": {
"type": "string",
"index": "analyzed",
"analyzer": "ik"
}
}
}
}
}
修改索引設(shè)置
PUT http://10.16.30.37:9200/cpinfo_index/_settings
{
"settings": {
"number_of_replicas" : 1
}
}
查看所有索引
GET http://{host}:9200/_cat/indices?v
查看索引mapping
GET http://{host}:9200/{index_name}/_mapping?pretty
向索引添加新type
POST http://{host}:9200/{index_name}/{type_name}
{
"mappings": {
"test_type2": {
"dynamic": false,
"properties": {
"name": {
"type": "string",
"index": "analyzed",
"analyzer": "standard"
},
"about": {
"type": "string",
"index": "analyzed",
"analyzer": "standard"
}
}
}
}
}
刪除索引
DELETE http://{host}:9200/{index_name}
添加數(shù)據(jù)
PUT http://{host}:9200/{index_name}/{type_name}/{id}
{
"name" : "Scott",
"about" : "Smith",
"location": {
"lat": 40.722,
"lon": -73.989
}
}
批量添加數(shù)據(jù)(方法一)
curl -XPOST localhost:9200/_bulk --data-binary @data.json
data.json內(nèi)容:
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "1" } }
{ "name" : "scott1", "about" : "work1", "interests" : "read1"}
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "2" } }
{ "name" : "scott2", "about" : "work2", "interests" : "read2"}
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "3" } }
{ "name" : "scott3", "about" : "work3", "interests" : "read3"}
批量添加數(shù)據(jù)(方法二)
curl -XPOST http://127.0.0.1:9200/_bulk -d '
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "4" } }
{ "name" : "scott1", "about" : "work1", "interests" : "read1"}
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "5" } }
{ "name" : "scott2", "about" : "work2", "interests" : "read2"}
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "6" } }
{ "name" : "scott3", "about" : "work3", "interests" : "read3"}
'
查詢單條數(shù)據(jù)
GET http://{host}:9200/{index_name}/{type_name}/{id}?pretty
條件查詢
curl -XPOST 'http://{host}:9200/{index_name}/{type_name}/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "interests": "閱讀" } },
{ "match": { "age": 97 } }
]
}
},
"sort": { "age": { "order": "desc" } },
"from": 0,
"size": 1000
}'
查看token
POST http://{host}9200/{index_name}/_analyze?analyzer=chinese
張三
查詢解析
POST http://{host}:9200/{index_name}/{type_name}/_validate/query?explain
{
"query": {
"multi_match": {
"query": "學(xué)習(xí)",
"fields": ["name", "about"]
}
}
}