基本操作 (6.x 版本)
- 创建索引
curl -X PUT http://192.168.183.220:9200/test -H 'Content-Type: application/json' -d'
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 0
},
"mappings": {
"_doc": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "text"
}
}
}
}
}
'
- 删除索引
curl -X DELETE http://192.168.183.220:9200/test
- 添加数据
curl -X POST http://192.168.183.220:9200/test -H 'Content-Type: application/json' -d'
{
"id": 1,
"name": "test"
}
'
- 更新数据 (id相同即更新)
curl -X POST http://192.168.183.220:9200/test -H 'Content-Type: application/json' -d'
{
"id": 1,
"name": "test01"
}
'
- 查询数据
curl -X POST http://192.168.183.220:9200/test/_search -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"name": "test01"
}
}
}
'
- 查看索引
curl -X GET http://192.168.183.220:9200/test
- 添加别名
curl -X PUT http://192.168.183.220:9200/test/_alias/abc
- 查看索引列表
curl -X GET http://192.168.183.220:9200/_cat/indices?v
- 查看集群状态
curl -X GET http://192.168.183.220:9200/_cat/health?v
ik操作
- 创建索引
curl -X PUT http://192.168.183.220:9200/ik -H 'Content-Type: application/json' -d'
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 0,
"analysis": {
"analyzer": {
"ik": {
"tokenizer": "ik_max_word"
}
}
}
},
"mappings": {
"_doc": {
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
'
- 分词分析(ik_smart)
curl -X POST http://192.168.183.220:9200/ik/_analyze -H 'Content-Type: application/json' -d'
{
"analyzer": "ik_smart",
"text": ["我是中国人"]
}
'
结果:
{
"tokens": [
{
"token": "我",
"start_offset": 0,
"end_offset": 1,
"type": "CN_CHAR",
"position": 0
},
{
"token": "是",
"start_offset": 1,
"end_offset": 2,
"type": "CN_CHAR",
"position": 1
},
{
"token": "中国人",
"start_offset": 2,
"end_offset": 5,
"type": "CN_WORD",
"position": 2
}
]
}
- 分词分析(ik_max_word)
curl -X POST http://192.168.183.220:9200/ik/_analyze -H 'Content-Type: application/json' -d'
{
"analyzer": "ik_max_word",
"text": ["我是中国人"]
}
'
结果:
{
"tokens": [
{
"token": "我",
"start_offset": 0,
"end_offset": 1,
"type": "CN_CHAR",
"position": 0
},
{
"token": "是",
"start_offset": 1,
"end_offset": 2,
"type": "CN_CHAR",
"position": 1
},
{
"token": "中国人",
"start_offset": 2,
"end_offset": 5,
"type": "CN_WORD",
"position": 2
},
{
"token": "中国",
"start_offset": 2,
"end_offset": 4,
"type": "CN_WORD",
"position": 3
},
{
"token": "国人",
"start_offset": 3,
"end_offset": 5,
"type": "CN_WORD",
"position": 4
}
]
}
- 添加数据
curl -X POST http://192.168.183.220:9200/ik/_doc/1 -H 'Content-Type: application/json' -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}
'
curl -X POST http://192.168.183.220:9200/ik/_doc/2 -H 'Content-Type: application/json' -d'
{"content":"公安部:各地校车将享最高路权"}
'
curl -X POST http://192.168.183.220:9200/ik/_doc/3 -H 'Content-Type: application/json' -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
'
curl -X POST http://192.168.183.220:9200/ik/_doc/4 -H 'Content-Type: application/json' -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
'
- 查询数据
curl -X POST http://192.168.183.220:9200/ik/_search -H 'Content-Type: application/json' -d'
{
"query": {
"term": {
"content": "中国"
}
}
}
'
结果:
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.6489038,
"hits": [
{
"_index": "ik",
"_type": "_doc",
"_id": "4",
"_score": 0.6489038,
"_source": {
"content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
}
},
{
"_index": "ik",
"_type": "_doc",
"_id": "3",
"_score": 0.2876821,
"_source": {
"content": "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
}
}
]
}
}
参考: