1.创建索引
PUT test
2.创建文档
PUT test/_create/1
{
"name" : "张三",
"age":18
}
3.创建索引指定字段类型
PUT test2
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "long"
},
"birthday": {
"type": "date"
}
}
}
}
4.查询索引信息
GET test2
5.查看默认信息
创建索引并插入数据
PUT test3/_doc/1
{
"name": "张三",
"age": 1
}
查看test3索引的默认类型
如果文档字段没有指定类型,es就会给默认字段类型
6.查看es健康信息
GET _cat/health
通过_cat/可以获得es当前的信息
7.修改文档信息
方法一:
PUT /test3/_doc/1
{
"name": "张三1"
}
方法二:
POST /test3/_update/1
{
"doc": {
"name": "张三12"
}
}
8.删除索引
DELETE test1
9.删除索引下的文档
DELETE test/_doc/1
10.获取所有数据
GET test/_search
11.获取指定id下的数据
GET test/_doc/1
12.查询指定名称下的数据
方式一:
GET test/_search?q=name:"李"
方式二:
GET test/_search
{
"query": {
"match": {
"name": "李"
}
}
}
score 匹配度,值越大则表示匹配度越高
13.复杂查询
- 查询指定字段
GET test/_search
{
"query": {
"match": {
"name": "李"
}
},
"_source": ["name"]
}
_source:查询指定字段
- 对查询结果进行排序
GET test/_search
{
"query": {
"match": {
"name": "李"
}
},
"sort": [
{
"age": {"order": "desc"}
}
]
}
- 分页查询
GET test/_search
{
"sort": [
{
"age": {"order": "desc"}
}
],
"from": 0,
"size": 1
}
from:从第几个数据开始
size:当前页面显示的数据
- must查询
GET test/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "李"
}
},
{
"match": {
"age": "30"
}
}
]
}
}
}
must相当于sql中的and,所有条件都要符合
- should查询
GET test/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "李"
}
},
{
"match": {
"age": "20"
}
}
]
}
}
}
should相当于sql中的or
- must_not查询
GET test/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"name": "李"
}
},
{
"match": {
"age": "30"
}
}
]
}
}
}
查询姓名不是李,并且年龄不是30岁的人,相当于sql中的not
- 过滤器filter
GET test/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "李"
}
}
],
"filter": [
{
"range": {
"age": {
"gt": 21
}
}
}
]
}
}
}
查询名称含有李,并且age大于21的数据
gt 大于
gte 大于等于
lt 小于
lte 小于等于
- 多条件查询
多个条件使用空格隔开,只要满足其中一个条件就可以查到 - 精确查询
term 查询是直接通过倒排索引指定的词条进行精确的查找
分词:
term,直接精确查询(会遇到分词器解析,是否被解析取决于这个字段的类型)
match,会使用分词器解析
两个类型:
text:可以被分词器解析
keyword:不会被分词器解析
14.高亮查询
GET test/_search
{
"query": {
"match": {
"name": "李四"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}
自定义搜索高亮
GET test/_search
{
"query": {
"match": {
"name": "李四"
}
},
"highlight": {
"pre_tags": "<p class='key' style='color:red'>",
"post_tags": "</p>",
"fields": {
"name": {}
}
}
}