概念
Index
類似于關(guān)系型數(shù)據(jù)庫中的database。索引只是一個邏輯上的空間,物理上是分為多個文件來管理的。命名必須全小寫。
Type
類似于關(guān)系型數(shù)據(jù)庫中的table,根據(jù)用戶需求每個index中可以新建任意數(shù)量的type。
Document
類似于關(guān)系型數(shù)據(jù)庫中的row。每個Document是一個json格式的文本。
Mapping
更像是一個用來定義每個字段類型的語義規(guī)范在mysql中類似sql語句,在ES中經(jīng)過包裝后,都被封裝為友好的Restful風(fēng)格的接口進行操作。這一點也是為什么開發(fā)人員更愿意使用ES或者compass這樣的框架而不是直接使用Lucene的一個原因。
安裝
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
啟動
elasticsearch-2.4.5/bin/elasticsearch
驗證
GET http://{host}:9200
常見REST API 操作
查看集群狀態(tài)
GET http://{host}:9200/_cat/health?v
查看所有節(jié)點狀態(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"]
}
}
}