Elasticsearch 入门
PUT 'localhost:9200/weather'
@return {
"acknowledged": true,
"shards_acknowledged": true,
"index": "weather"
}
- 新增一条记录
```
PUT localhost:9200/accounts/person/2
注:POSTMAN ->body ->raw (json)
{
"user": "张三",
"title": "工程师",
"desc": "数据库管理"
}
@return{
"_index": "accounts",
"_type": "person",
"_id": "2",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
```
DELETE 'localhost:9200/weather'
@return {
"acknowledged": true
}
localhost:9200/accounts/person
{
"user": "张si",
"title": "工程师",
"desc": "数据库管理"
}
生成随机id
@return {
"_index": "accounts",
"_type": "person",
"_id": "vFL2fXUBQ-J0hUIur3oG",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
GET localhost:9200/accounts/person/1?pretty=true
@return {
"_index": "accounts",
"_type": "person",
"_id": "1",
"_version": 2,
"_seq_no": 1,
"_primary_term": 1,
"found": true, //查询成功
"_source": { //原始记录
"user": "张三",
"title": "工程师",
"desc": "数据库管理"
}
}
GET localhost:9200/accounts/person/_search //获取/Index/Type下所有数据
@return {
"took": 762,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "accounts",
"_type": "person",
"_id": "2",
"_score": 1,
"_source": {
"user": "张si",
"title": "工程师",
"desc": "数据库管理"
}
},
{
"_index": "accounts",
"_type": "person",
"_id": "u1LzfXUBQ-J0hUIuaXp9",
"_score": 1,
"_source": {
"user": "张si",
"title": "工程师",
"desc": "数据库管理"
}
}
]
}
}
- Elastic 的查询非常特别,使用自己的查询语法,要求 GET 请求带有数据体。
```
GET localhost:9200/accounts/person/_search
//带有数据体
@request{
"query" : { "match" : { "desc" : "软件" }}
}
```
- Elastic 默认一次返回10条结果,可以通过size字段改变这个设置
```
@request{
"query" : { "match" : { "desc" : "管理" }},
"size": 1
}
```
- 逻辑运算 OR
```
{
"query" : { "match" : { "desc" : "软件 系统" }}
}
```
- 逻辑运算 AND
```
{
"query": {
"bool": {
"must": [
{ "match": { "desc": "软件" } },
{ "match": { "desc": "系统" } }
]
}
}
}
```
-
网友评论