安装
直接在 官网 下载可执行文件即可。
几个主要配置:
./config/elasticsearch.yml
1
2
3
4
|
cluster.name:elasticsearch // 集群名字,ES 会自动发现并加入同一网段下的指定名字的集群
path.data:/path/to/data //数据文件路径,默认为 ./data
path.logs:/path/to/data //日志文件路径,默认为 ./logs
network.host:192.168.0.1 // 监听的IP,不建议指定公网IP
|
HUGOMORE42
使用
基本概念
index 索引, 拥有相信结构的数据的集合,比如用户信息、商家信息,这些都可以分别创建一个索引;
type 类型, ES6.0 中已禁止一个索引中有多个类型,并且在后续版本中可能会废弃类型;
mapping 数据类型映射,类似于传统关系型数据库中的表结构;
document 文档, 类似于传统关系型数据库中的一行记录。
使用时需先创建 index 和 type, 创建 mapping 并不是必须的,但建议提前自己创建,如果往 index 中添加一个没定义的字段,ES 将抢断该字段的类型并添加到 mapping。
常用操作
创建一个名为 enterprises 的索引
1
|
curl -X PUT 'http://localhost:9200/enterprises'
|
删除名为 enterprises 的索引
1
|
curl -X DELETE 'http://localhost:9200/enterprises'
|
- 查看索引的 mapping
查看索引 enterprises 的 mapping
1
|
curl 'http://localhost:9200/enterprises/_mapping?pretty=true'
|
- 创建 mapping
为 enterprises 这个 index 创建一个 type 名为 enterprises 的 mapping:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
curl -X POST http://localhost:9200/enterprises/enterprises/_mapping -H 'Content-Type:application/json' -d'
{
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"legal_person":{
"type":"text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"province":{
"type":"text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"address":{
"type":"text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"credit_code":{
"type":"text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
'
|
添加数据时可指定文档 ID 或由 ES 自动生成一串随机 ID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# 指定ID
curl -X POST http://localhost:9200/enterprises/enterprises/fe1ecb7408a34b89b3553cb1cba4d8c2 -H 'Content-Type:application/json' -d'
{
"name":"北京建工四建工程建设有限公司",
"legal_person":"孙振泉",
"province": "北京市",
"address": "北京市东城区永外沙子口中街32号",
"mysql_id":1,
"credit_code":"91110000101510712K"
}
'
curl -X POST http://localhost:9200/enterprises/enterprises/a66b7dc07733405bb252318f0cdf0cd8 -H 'Content-Type:application/json' -d'
{
"name":"中铁六局集团有限公司",
"legal_person":"季志华",
"province": "北京市",
"address": "北京市海淀区万寿路2号",
"mysql_id":2,
"credit_code":"91110108101884765M"
}
'
# 不指定ID
curl -X POST http://localhost:9200/enterprises/enterprises -H 'Content-Type:application/json' -d'
{
"name":"测试公司名字",
"legal_person":"测试法人",
"province": "北京市",
"address": "北京市海淀区万寿路2号",
"mysql_id":2,
"credit_code":"91110108101884765M"
}
'
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# 完整修改某文档:
curl -X PUT http://localhost:9200/enterprises/enterprises/36f33b7700084aed9f79f68b52b14225 -H 'Content-Type:application/json' -d '
{
"name":"安通建设有限公司",
"legal_person":"傅凌",
"province": "北京市",
"address": "北京市朝阳区惠新西街21号",
"mysql_id":4,
"credit_code":"71092720-6 / 100000000032910"
}
'
# 修改某文档中的指定字段:
curl -XPOST 'http://localhost:9200/enterprises/enterprises/36f33b7700084aed9f79f68b52b14225/_update' -H 'Content-Type:application/json' -d '{
"doc" : {
"legal_person":"傅凌",
"name":"安通建设有限公司(北京的工程建设公司)"
}
}
'
|
1
|
curl -X DELETE http://localhost:9200/enterprises/enterprises/a66b7dc07733405bb252318f0cdf0cd8
|
endpoint: /_bulk
提交的数据格式( 详见官方文档 ):
1
2
3
4
5
6
7
|
action_and_meta_data\n
optional_source\n
action_and_meta_data\n
optional_source\n
……
action_and_meta_data\n
optional_source\n
|
example:
1
2
3
4
5
6
7
|
curl -X POST http://localhost:9200/_bulk -H 'Content-Type:application/json' -d '
{ "create" : { "_index" : "enterprises", "_type" : "enterprises", "_id" : "c039729274f14cd6b29862ae6983a00b" }}
{"create":{ "name":"中电建路桥集团有限公司","legal_person":"汤明","province":"北京市","address":"北京市海淀区车公庄西路22号海赋国际大厦A座10层","mysql_id":5,"credit_code":"91110108787757233M" }}
{ "create" : { "_index" : "enterprises", "_type" : "enterprises", "_id" : "1364f0e7333a48539b0553a8c3493a60" }}
{"create":{ "name":"北京市政路桥股份有限公司","legal_person":"傅凌","province":"北京市","address":"北京市西城区南礼士路17号","mysql_id":6,"credit_code":"91110000101126390P" }}
'
|
1
|
curl 'http://localhost:9200/enterprises/enterprises/_search?pretty=true'
|
1
2
3
4
5
|
curl -X GET 'http://localhost:9200/enterprises/enterprises/_search' -H 'Content-Type:application/json' -d '
{
"query" : { "match" : { "name" : "工程" }},
"size":5
}'
|
更复杂一点的查找:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
curl -X GET 'http://localhost:9200/jobs/jobs/_search?pretty=true' -H 'Content-Type:application/json' -d '
{
"size": 5,
"from": 0,
"min_score": 0.5,
"query" : {
"bool" : {
"must" : [
{"range": {"time":{"gte":1534813200, "lt": 1534819224}}},
{"term": {"province_id": 6}},
{"terms":{"classify_id":[6,8,10,20]}},
{"match": {"title": "books"}}
]
}
}
}
'
|