ElasticSearch基础语法


在这里插入图片描述
注意点:

  • 基于restful风格
  • 索引类似于mysql数据库库名
  • 如果自己的文档字段没有指定,那么es就会给我们默认配置字段类型!
  • 默认就是localhost:9200 , 可以不用写

增加 PUT

PUT / 索引 / 文档类型 / id
{
“key” : “value” , ( 具体的数据,请求体 )
“key” : “value”
}

PUT /test1/people/1
{
  "name": "狂神",
  "age": 13,
  "interts": ["游泳","游戏","睡觉"]
}

PUT /test1/people/2
{
  "name": "小狂神",
  "age": 3,
  "interts": ["游泳","游戏","吃饭"]
}

PUT /test1/people/3
{
  "name": "大狂神",
  "age": 23,
  "interts": ["篮球","游戏","学习"]
}


也可以在创建索引的时候就规定文档下的字段和字段的数据类型,type代表这个字段的数据类型

PUT /test2
{
  "mappings": {
    "properties": {
    "name": {
      "type": "text"
    }, 
    "age": {
      "type": "long"
    },
    "birthday": {
      "type": "date"
    }
  }
  }
}

查询 GET

GET / 索引 / _search (? 查询条件)
{
具体的查询条件 / 请求体
}

//轻量搜索 name = 神
GET /test1/_search?q=name:神

//使用查询表达式搜索,_search搜索不能写type
GET /test1/_search
{
  "query": {
    "match": {
      "age": "3"
    }
  }
}

GET /test1/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "name": "狂神"
        }}
      ],
      //添加过滤器
      "filter": [
        {"range": {
          "age": {
          //gte:大于等于,lte:小于等于
            "gte": 10
          }
        }}
      ]
    }
  }
}

查询匹配,会查询到只包含rack或者climbing的数据

GET /test1/_search
{
  "query": {
    "match": {
      "about": "rack climbing"
    }
  }
}

//查询匹配短语,只会查询到完全包含词短语的数据
GET /test1/_search
{
  "query": {
    "match_phrase": {
      "about": "rack climbing"
    }
  }
}
GET /test1/_search
{
  "query": {
    "match_phrase": {
      "about": "rack climbing"
    }
  },
  //高亮查询字段about
  "highlight": {
    "fields": {
      "about": {}
    }
  }
}

聚合查询,查出索引中所有的interts字段

GET /test1/_search
{
  "aggs": {
    "all_interests": {
      "terms": {
        "field": "interts.keyword"
      }
    }
  }
}

//查询interts字段中的平均年龄
GET /test1/_search
{
  "query": {
    "match": {
      "name": "狂神"
    }
  },
  "aggs": {
    "all_age": {
      "terms": {
        "field": "interts.keyword"},
      "aggs": {
        "avg_age": {
          "avg": {"field": "age"}
        }
      }
    }
  }
}
GET test1/_search
{
 "sort":[
   {
     "age": {
       "order": "desc"//倒序
       }
     }
   ],
   "from": 0,//相当于mysql分页查询
   "size": 1
}
GET /test1/_search
{
  "query": {
    "match": {
      "name": "狂神"
    }
  },
    "_source": ["name","age"]//指定要查询的资源(name和age)
}

更新 POST

POST / 索引 / 文档类型 / id / _update
{
更新的内容 / 请求体
}


//POST更新不会改变原有的字段,用PUT更新会完全覆盖原来的字段
//更新操作,存在更新的字段
POST /test1/people/3/_update
{
  "doc": {
  "name": "狂神"
  }
}

//更新不存在的字段,相当于直接添加
POST /test1/people/3/_update
{
  "doc": {
    "about": "I like go to rack climbing"
  }
}

POST /test1/people/1/_update
{
  "doc": {
    "about": "I like to collect rack albums"
  }
}

POST /test1/people/3/_update
{
  "doc": {
    "about": "I love to go rack climbing"
  }
}

POST /test1/people/2/_update
{
  "doc": {
    "about": "I want to be a rich man"
  }
}

删除 DELETE

DELETE / 索引 / 文档类型 / id

DELTE /test   				删除某个索引
DELTE _all    				删除全部索引
Delete /test/people/1       删除某条数据

知识点

查询

term是直接通过倒排索引指定的词条进程精确查找的

关于分词:

  • term:精确查询(使用倒排索引)
  • match:会使用分词器解析(先分析文档,然后通过分析的文档进行查询)

两种类型(相当于字段):

  • keyword:关键词 ,不会被分词器解析(是一个整体)
  • text:文本,会被分词器解析

来源于:遇见狂神说,elasticsearch教学视频
视频链接: [狂神说Java]ElasticSearch.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值