今天敲了一遍常用命令,发现还是会忘。。。
PUT users
{
"mappings": {
"properties": {
"name":{
"type": "keyword"
},
"age":{
"type": "integer"
},
"remark":{
"type": "text"
}
}
}
}
PUT users/_mapping
{
"properties": {
"content1":{
"type": "keyword"
},
"content2":{
"type": "keyword"
}
}
}
GET users
DELETE users
以上是索引的基本操作命令
PUT users/_doc/1
{
"age":15,
"name":"张三的名字叫张三",
"remark":"张三的备注",
"content":"张三的content"
}
GET users/_doc/1
PUT users/_doc/1
{
"age":16
}
POST users/_doc/1
{
"doc":{
"age":15,
"name":"张三的名字",
"remark":"张三的备注"
}
}
DELETE users/_doc/1
以上是文档的基本操作命令
多创建一些文档,方便下面的复杂查询
PUT users/_doc/2
{
"age":16,
"name":"李四的名字叫李四",
"remark":"李四的备注",
"content":"李四的content"
}
PUT users/_doc/3
{
"age":17,
"name":"王五的名字叫王五",
"remark":"王五的备注",
"content":"王五的content"
}
PUT users/_doc/4
{
"age":18,
"name":"赵六的名字叫赵六",
"remark":"赵六的备注",
"content":"赵六的content"
}
PUT users/_doc/5
{
"age":18,
"name":"阿三的名字叫赵六",
"remark":"阿三的备注",
"content":"阿三的content"
}
GET users/_doc/_search?q=name:张三
GET users/_doc/_search
{
"query":{
"match":{
"remark":"备注"
}
}
}
解释:第10、11步,name字段类型是keyword,不会走倒排索引查询(字段内数据不会被分词),所以通过“张三”查不到,只能精准匹配;查询remark字段,它的类型为text,所以会被分词,可通过关键字检索
解释:第11步的查询是通过match(模糊搜索)查询,即输入的查询条件会被分词
GET users/_doc/_search
{
"query":
{
"term":{
"age":15
}
}
}
以上是文档一个字段查询的基本操作命令
其中下面用到的term和match可以根据需求变换
GET users/_doc/_search
{
"query":{
"bool":{
"must":[
{
"term":
{
"name":"张三的名字叫张三"
}
} ,
{
"match":
{
"age":"15"
}
}
]
}
}
}
GET users/_doc/_search
{
"query":{
"bool":{
"should":[
{
"term":{
"name":"张三的名字叫张三"
}
},
{
"term":{
"age":16
}
}
]
}
}
}
GET users/_doc/_search
{
"query":{
"bool":{
"must_not": [
{
"term":{
"name":"张三的名字叫张三"
}
},
{
"term":{
"age":17
}
}
]
}
}
}
gt 大于
gte 大于等于
lt 小于
lte 小于等于!
GET users/_doc/_search
{
"query":{
"bool":{
"must":{
"match":{
"remark":"李四"
}
},
"filter":{
"range":{
"age":{
"gte":16,
"lte":17
}
}
}
}
}
}
GET users/_doc/_search
{
"_source":["age"]
}
GET users/_doc/_search
{
"query":{
"bool":{
"must":{
"match":{
"remark":"的备注 张三"
}
}
}
},
"sort":[
{
"age":"desc"
},
{
"_score":"desc"
}
]
}
pre_tags:为查找的关键字添加自定义的html代码前缀
post_tags:为查找的关键字添加自定义的html代码后缀
fields:定义要高亮的字段
GET users/_doc/_search
{
"query":{
"bool":{
"must":[
{
"match":{
"remark":"的备注"
}
},
{
"match":{
"content":"张三"
}
}
]
}
},
"highlight":{
"pre_tags":"<mytag style=‘color:red‘>",
"post_tags":"</mytag>",
"fields":[
{
"remark":{}
},
{
"content":{}
}
]
}
}
from:从第几条数据开始(>关系,非>=)
size:查询几条数据
GET users/_doc/_search
{
"from:":2,
"size":2
}
可以查出来每个索引使用的空间大小,记录数等信息
GET _cat/indices?v
原文:https://www.cnblogs.com/rb2010/p/13045292.html