当前位置:首页 - 博客 - 正文

ElasticSearch笔记

下载安装

windows安装

下载对应平台版本

下载地址:https://www.elastic.co/jp/downloads/elasticsearch

启动

es安装目录下的/bin/elasticsearch.bat

  1. ./bin/elasticsearch.bat
访问
  1. localhost:9200

常见问题

本地访问不了

找到./config/elasticsearch.yml文件,将xpack.security.enabled由true改为false

  1. # Enable security features
  2. xpack.security.enabled: false

其他问题看日志文件

./logs/elasticsearch.log

索引类型

正排索引

id查找内容,模糊需要全表扫描

倒排索引

关键字绑定id,再根据id查内容

基础操作(postman)

创建索引

创建一个名为shopping的索引

  1. put请求: localhost:9200/shopping
  2. {
  3. "acknowledged": true,
  4. "shards_acknowledged": true,
  5. "index": "shopping"
  6. }

get请求查看索引,delete请求删除索引

创建文档

es自动生成id

在索引为shopping中添加一条数据

  1. post请求:localhost:9200/shopping/_doc
  2. {
  3. "phone": "红米",
  4. "price": "1999.00"
  5. }

es指定id

在索引为shopping中添加一条数据并指定id为1

  1. post请求:localhost:9200/shopping/_doc/1
  2. {
  3. "phone": "红米",
  4. "price": "1999.00"
  5. }

查询文档

  1. #查询id为1的文档
  2. get请求:ost:9200/shopping/_doc/1
  3. #全部查询
  4. get请求:ost:9200/shopping/_search

更新文档

  1. #更新文档id为1数据(数据全部更新)
  2. put请求:localhost:9200/shopping/_doc/1
  3. {
  4. "phone": "红米",
  5. "price": "1999.00",
  6. "status": "0"
  7. }
  8. #更新文档id为1数据(部分更新)
  9. post请求:localhost:9200/shopping/_update/1
  10. {
  11. "doc":{
  12. "status": "1"
  13. }
  14. }

删除

  1. #删除文档id为1数据
  2. put请求:localhost:9200/shopping/_doc/1

搜索文档

  1. #url中带参数搜索
  2. get请求:localhost:9200/shopping/_search?q=phone:小米
  3. #请求体搜索(推荐)
  4. get请求:localhost:9200/shopping/_search
  5. {
  6. "query":{
  7. "match": {
  8. "phone": "小米"
  9. }
  10. }
  11. }

分页查询

  1. get请求:localhost:9200/shopping/_search
  2. {
  3. "query":{
  4. "match_all": {
  5. }
  6. },
  7. "from": 0,
  8. "size": "1"
  9. }

排序

  1. get请求:localhost:9200/shopping/_search
  2. #_source为需要显示的字段名称, sort排序字段
  3. {
  4. "query":{
  5. "match_all": {
  6. }
  7. },
  8. "from": 0,
  9. "size": 1,
  10. "_source":["phone"],
  11. "sort": {
  12. "price.keyword": {
  13. "order": "desc"
  14. }
  15. }
  16. }

问题:No mapping found for [keyword] in order to sort on

解决方法1

用.keyword进行聚合

  1. "sort": {
  2. "price.keyword": {
  3. "order": "desc"
  4. }
  5. }

解决方法2
设置price这个排序字段的fileddata为true。

  1. PUT请求:localhost:9200/shopping/_mapping
  2. {
  3. "properties": {
  4. "price":{
  5. "type": "text",
  6. "fielddata": true
  7. }
  8. }
  9. }

多条件查询

  1. get请求:localhost:9200/shopping/_search
  2. //与查询 查询品牌为小米状态为1的手机
  3. {
  4. "query":{
  5. "bool": {
  6. "must": [{
  7. "match": {
  8. "phone":"小米"
  9. }
  10. },
  11. {
  12. "match": {
  13. "status":"0"
  14. }
  15. }
  16. ]
  17. }
  18. }
  19. }
  1. get请求:localhost:9200/shopping/_search
  2. //或查询 查询品牌为小米或华为的手机
  3. {
  4. "query":{
  5. "bool": {
  6. "should": [{
  7. "match": {
  8. "phone":"小米"
  9. }
  10. },
  11. {
  12. "match": {
  13. "status":"0"
  14. }
  15. }
  16. ]
  17. }
  18. }
  19. }

范围查询

  1. get请求:localhost:9200/shopping/_search
  2. {
  3. "query":{
  4. "bool": {
  5. "must": [{
  6. "match": {
  7. "phone":"小米"
  8. }
  9. },
  10. {
  11. "match": {
  12. "status":"0"
  13. }
  14. }
  15. ],
  16. "filter": {
  17. "range": {
  18. "price": {
  19. "lt": 2000,
  20. "gt": 1000
  21. }
  22. }
  23. }
  24. }
  25. }
  26. }

精确匹配

  1. get请求:localhost:9200/shopping/_search
  2. {
  3. "query":{
  4. "match_phrase": {
  5. "phone": "红米"
  6. }
  7. }
  8. }

高亮显示

  1. #category 高亮这字段
  2. {
  3. "query":{
  4. "match":{
  5. "phone" : "红米"
  6. }
  7. },
  8. "highlight":{
  9. "fields":{
  10. "phone":{}
  11. }
  12. }
  13. }