创建索引
Method:PUT
http://192.168.237.130:9200/people
{
"settings":{
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings":{
"man":{
"properties":{
"name": {
"type":"text"
},
"country": {
"type":"keyword"
},
"age":{
"type":"integer"
},
"date":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
}
注意:5.x版本中索引创建支持多个type,但是在6.x中只能有一个,不然会报错!
插入文档
指定ID插入
Method:PUT
http://192.168.237.130:9200/people/man/1
{
"name":"wei",
"country":"china",
"age":28,
"data":"1293-12-33"
}
自动ID插入
Method:POST
http://192.168.237.130:9200/people/man/
{
"name":"wei2",
"country":"china",
"age":23,
"data":"1293-12-23"
}
修改文档
直接修改文档
Method:POST
http://192.168.237.130:9200/people/man/1/_update
{
"doc":{
"name":"who am I"
}
}
通过脚本修改文档
{
"script":{
"lang":"painless",
"inline": "ctx._source.age = params.age",
"params": {
"age":100
}
}
}
删除文档
Method:DELETE
http://192.168.237.130:9200/people/man/1
删除索引
Method:DELETE
http://192.168.237.130:9200/people
查询
简单查询
Method:GET
http://192.168.237.130:9200/book/novel/1
条件查询
Method
http://192.168.237.130:9200/book/_search
查询全部
{
"query":{
"match_all":{}
},
"from":1,
"size":1
}
from 表示从结果中的第几条开始取数据,size表示取多少条
根据条件查询
{
"query":{
"match":{
"title":"ES"
}
},
"sort":[
{
"publish_date":{"order":"desc"}}
]
}
聚合查询
{
"aggs":{
"group_by_word_count":{
"terms":{
"field":"word_count"
}
},
"group_by_publish_date":{
"terms":{
"field":"publish_date"
}
}
}
}
子条件查询:Query Context
在查询过程中,除了判断文档是否满足查询条件,ES还会计算一个_score来标志匹配的程度,旨在判断目标文档和查询条件匹配有多好
全文本查询:针对文本类型数据
模糊匹配:{ "query":{ "match":{ "author":"李四" } } }
完全匹配
{ "query":{ "match_phrase":{ "author":"李四" } } }
多个字段匹配查询
{ "query":{ "multi_match":{ "query":"李四", "fields":["author","title"] } } }
query_string 查询
{ "query":{ "query_string":{ "query":"李四 AND ES" } } }
字段级别查询:针对结构化数据,如数字,日期等
字段查询{ "query":{ "term":{ "word_count":1000 } } }
范围查询
{ "query":{ "range":{ "word_count":{ "gte":1000, "lte":2000 } } } }
子条件查询:Filter Context
在查询过程中,只判断该文件是否满足条件,只有Yes或者No
{
"query":{
"bool":{
"filter":{
"term":{
"word_count":1000 }
}
}
}
}
符合条件查询
- 固定分数查询
{
"query":{
"constant_score":{
"filter":{
"match":{
"title":"ES" }
},
"boost":2
}
}
}
- 布尔查询
should是或逻辑,must是与逻辑
"query":{
"bool":{
"should":[
{
"match":{
"author":"李四"
}
},
{
"match":{
"title":"ES"
}
}
]
}
}
}
手动段合并
POST /indexName/_optimize?max_num_segments=1