官网地址:https://www.elastic.co/
安装
最低要求jdk1.7,下载解压就可以了。
配置
配置文件位置:$ES_HOME/config/elasticsearch.yml
虽然es默认配置就能直接用,但是建议修改一下默认配置,如集群名字,分片数,节点名,数据存放位置,日志存放位置。
启动
$ES_HOME/bin/elasticsearch -d
-d参数表示后台启动,还可以指定其他启动参数,如内存大小。 -Xmx3g -Xms3g -XX:PermSize=80
常用命令
查看集群健康状态
curl 'localhost:9200/_cat/health?v'
列出所有索引
curl 'localhost:9200/_cat/indices?v'
创建一个索引
curl -XPUT 'localhost:9200/customer?pretty'
插入一个文档(指定id 如果不指定id会随机生成一个id)
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
"name": "John Doe"
}'
根据id查找
curl -XGET 'localhost:9200/customer/external/1?pretty'
删除索引
curl -XDELETE 'localhost:9200/customer?pretty'
修改文档
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
"doc": { "name": "Jane Doe" }
}'
不指定id插入文档(不指定id用POST)
curl -XPOST 'localhost:9200/customer/external?pretty'-d '
{
"name": "Jane Doe"
}'
删除文档
curl -XDELETE 'localhost:9200/customer/external/2?pretty'
普通搜索
curl 'localhost:9200/bank/_search?q=*&pretty'
创建mapping
PUT /{indices}/_mapping/{type}
{
"my_index": {
"mappings": {
"my_type": {...}
}
}
}
curl -XPUT http://localhost:9200/fstore/_mapping/test -d '{
"test": {
"properties": {
"api": {
"type": "string",
"index": "not_analyzed"
},
"appId": {
"type": "long"
},
"appName": {
"type": "string",
"index": "not_analyzed"
}
},
"_source":{"excludes":["appName"]}
}
}'
查看mapping
GET /{indices}/_mapping/{types}
curl localhost:9200/fstore/_mapping/test?pretty
聚合
curl -XPOST 'localhost:9200/bank/_search?pretty'-d '
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state"
}
}
}
}'
基于位置的搜索
Geo Location and Search
一个完整的搜索
curl -XPOST '192.168.3.82:9200/tclappstore/_search?pretty' -d '
{
"from": 0,
"size": 10,
"query": {
"filtered": {
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "途牛",
"fields": [
"name",
"recommend_name",
"search_keywords"
],
"boost": 50
}
},
{
"wildcard": {
"name.raw_lowercase": {
"wildcard": "*途牛*",
"boost": 100
}
}
},
{
"wildcard": {
"recommend_name": {
"wildcard": "*途牛*",
"boost": 100
}
}
},
{
"match": {
"name.first_letter_pinyin": {
"query": "途牛",
"type": "boolean"
}
}
},
{
"match": {
"name.full_letter_pinyin": {
"query": "途牛",
"type": "boolean",
"boost": 10
}
}
},
{
"match": {
"name.ik_pinyin": {
"query": "途牛",
"type": "boolean"
}
}
}
]
}
},
"filter": {
"bool": {
"must": [
{
"term": {
"status": 1
}
},
{
"terms": {
"cates": [
1
]
}
}
]
}
}
}
},
"sort": [
{
"_score": {
"order": "desc"
}
},
{
"get_real_download_count": {
"order": "desc"
}
},
{
"sort": {
"order": "desc"
}
}
]
}'
另外还有批量插入,批量修改文档等
JAVA中连接ES
有两种方式连接ES
建议客户端jar包版本和集群ES版本一直
- NodeClient :以节点形式加入集群
- TransportClient:作为一个普通客户端连接集群
Client client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));
如果不是默认集群名,需要设置集群名
Settings settings = Settings.settingsBuilder()
.put("cluster.name", "myClusterName").build();
Client client = TransportClient.builder().settings(settings).build();
文档API
创建索引
CreateIndexRequest createIndexRequest = new CreateIndexRequest(esIndexName);
// 从指定位置读取json文件作为mapping
String setting = readMapping("elasticsearch/settings/app-index.json");
createIndexRequest.source(setting);
// 创建索引
EsClient.getInstance().getClient().admin().indices().create(createIndexRequest).actionGet();
删除一个文档
DeleteResponse response = client.prepareDelete("twitter", // 索引名
"tweet", // type
"1" //文档id
).get();
搜索API
如果debug可以看到查询的dsl语句,可以直接用dsl语句去查寻
setExplain(true) 可以看到返回结果的评分详细
升级
备份数据
https://www.elastic.co/guide/en/elasticsearch/reference/current/backup.html
Upgrade From | Upgrade To | Supported Upgrade Type |
---|---|---|
|
|
Full cluster restart |
|
|
Full cluster restart |
|
|
Rolling upgrade |
|
|
Rolling upgrade (if |
|
|
Rolling upgrade |
|
|
Full cluster restart |
插件
安装插件
$ES_HOME/bin/plugin 加 -h 参数可以查看功能
$ES_HOME/bin/plugin list查看已经安装的插件
建议安装插件head
安装方法:$ES_HOME/bin/plugin -install mobz/elasticsearch-head
中文分词插件ik安装方法
http://my.oschina.net/xiaohui249/blog/232784 (没试是否可以成功,记得大概是这样的)
json查看:http://json.iapolo.com/