首先在Linux中启动docker,并启动es以及相关容器
#创建索引
PUT index_01
{
}
#查看索引
GET _cat/indices
#创建索引并设置分片副本,索引名称不能以下划线开头,不能用大写,不能以关键字命名
PUT index_02
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
}
}
#删除缩影
DELETE index_02
#分词器
GET _analyze
{
"text": "北京大兴区中科电商电商谷",
"analyzer": "ik_max_word"
}
#创建索引和type
PUT index_user
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"user":{
"properties": {
"name":{
"type": "text",
"analyzer": "ik_max_word"
},
"age":{
"type": "integer"
},
"sex":{
"type": "integer"
},
"address":{
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
#根据索引和mapping查询数据
GET index_user/user/_search
{
}
#POST方式新增数据
POST /index_user/user/
{
"name":"test1",
"age":20,
"sex":0,
"address":"北京大兴"
}
#PUT方式新增数据,必须加上ID
PUT /index_user/user/1
{
"name":"小明",
"age":20,
"sex":0,
"address":"北京大兴2"
}
#删除document 最后面是id
DELETE index_user/user/6gw6j3QBGLI-1RrOIdKR
#修改数据
PUT index_user/user/1
{
"name":"小明modify",
"age":20,
"sex":0,
"address":"北京大兴"
}
#数据查询-请求体查询-过滤查询(精准查询,不评分,bool+filter)
GET index_user/user/_search
{
"query": {
"bool": {
"filter": {
"term": {
"age": 20
}
}
}
}
}
#数据查询-请求体查询-字句查询(精准查询,不评分,bool+filter)
#gte大于等于 lt小于
GET index_user/user/_search
{
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gte": 20,
"lt": 22
}
}
}
}
}
}
#数据查询-请求体查询-匹配度查询(全文检索查询,进行打分,常用match)
GET index_user/user/_search
{
"query": {
"match": {
"address": "北京"
}
}
}
#数据查询-组合查询-should
GET index_user/user/_search
{
"query": {
"bool": {
"filter": {
"term": {
"age": "20"
}
},
"should": [
{
"match": {
"name": "小明"
}
}
]
}
}
}
#数据查询-组合查询-must(分页)
GET index_user/user/_search
{
"query": {
"bool": {
"filter": {
"term": {
"age": 20
}
},
"must": [
{
"term": {
"name": {
"value": "小明"
}
}
}
]
}
},
"from": 0,
"size": 20
}