Elasticsearch学习

elasticsearch学习

1. 基础学习

mappings 字段映射

基础数据类型
text 类型会搜索时进行每个文字分词
keyword会全词搜索

PUT /orders
{
  "settings":{
      "number_of_shards":1, 
      "number_of_replicas":1
  },
  "mappings": {
    "properties": {
      "id":{
        "type": "integer" 
      },
      "title":{
        "type": "keyword"
      },
      "price":{
        "type": "double"
      },
      "create_at":{
        "type": "date"
      },
      "update_at":{
        "type": "date"
      },
      "description":{
        "type": "text"
      }
    }
  }
}

2. kibana命令

# index为数据库 , type为表,document 标识行,field为列

# 查看es中索引
GET /_cat/indices?v
GET /_cat/indices

# 创建索引
PUT orders

# 索引的设置
DELETE /orders

# 设置索引及其字段
PUT /orders
{
  "settings":{
      "number_of_shards":1, 
      "number_of_replicas":1
  },
  "mappings": {
    "properties": {
      "id":{
        "type": "integer" 
      },
      "title":{
        "type": "keyword"
      },
      "price":{
        "type": "double"
      },
      "create_at":{
        "type": "date"
      },
      "update_at":{
        "type": "date"
      },
      "description":{
        "type": "text"
      }
    }
  }
}
# 查看索引映射
GET /orders/_mapping

# 创建文档
POST /orders/_doc/1
{
  "id":1,
  "create_at":"2021-11-12",
  "update_at":"2021-11-12",
  "decription":"描述订单信息",
  "price":58,
  "title":"订单1"
}

POST /orders/_doc/
{
  "create_at":"2021-11-12",
  "update_at":"2021-11-12",
  "decription":"描述订单信息",
  "price":58,
  "title":"订单1"
}
# 获取索引中的文档内容
GET /orders/_search

GET /orders/_doc/2
# 默认put操作会将一个文档数据都替换掉
PUT /orders/_doc/1
{
  "doc":{
    "title":"我是你爹"
  }
}

# post方式进行修改操作
POST /orders/_doc/2/_update
{
  "doc":{
    "price":5
  }
}



# 使用_bulk实现批量新增文档
POST /orders/_doc/_bulk
  {"index":{"_id":2}}
  {"id":2,"create_at":"2021-11-12","update_at":"2021-11-12","description":"描述订单信息","price":58,"title":"我是你爹"}
  {"index":{"_id":3}}
  {"id":3,"create_at":"2021-11-12","update_at":"2021-11-12","description":"描述订单信息","price":58,"title":"我是你爹"}

# _bluk 批量新增 更新 删除
POST /orders/_doc/_bulk
{"index":{"_id":4}}
  {"id":2,"create_at":"2021-11-12","update_at":"2021-11-12","description":"描述订单信息","price":58,"title":"我是你爹"}
{"update":{"_id":1}}
  {"doc":{"title":"我是你爸爸标题"}}
{"delete":{"_id":2}}
  

##########################------------高级查询
GET /orders/_doc/_search
{
  "query":{
    "match_all":{}
  }
}

GET /orders/_mapping

##########################term 关键字查询
# term 基于关键字查询
# keyword 类型入喉搜索时,全部内容搜索
# text 类型 默认es标准分词器 中文单字分词,英文单词分词
GET /orders/_search
{
  "query":{
    "term":{
      "description":{
        "value": "息"
      }
    }
  }
}

###########################range范围查询
GET /orders/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 10,
        "lte": 100
      }
    }
  }
}

########################### prefix 前缀查询
GET /orders/_search
{
  "query": {
    "prefix": {
      "description": {
        "value": "单"
      }
    }
  }
}

########################### 多ids 查询



GET /orders/_search
{
  "query": {
    "ids": {
      "values": [1,2,3]
    }
  }
}



########################### bool 查询

GET /orders/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "ids": {
            "values": [1,2,3]
          }
        },{
          "term": {
            "title": {
              "value": "我是你爹"
            }
          }
        }
      ]
    }
  }
}

########################### multi_match 查询,不会进行分词

GET /orders/_search
{
  "query": {
    "multi_match": {
      "query": "信",
      "fields": ["description"]
    }
  }
}



########################### query_string 查询,分词的不会进行分词

GET /orders/_search
{
  "query": {
    "query_string": {
      "default_field": "description",
      "query": "述"
    }
  }
}

########################### 高亮查询  默认全文返回10条数据
GET /orders/_search
{
 "query": {
   "term": {
     "description": {
       "value": "信"
     }
   }
 },
   "highlight":{
     "pre_tags": ["<span style='color:red'>"],
     "post_tags": ["</span>"],
     "require_field_match": "false",
     "fields": {
       "description": {}
     }
 },
 "size": 10,
 "from": 0,
 "sort": [
   {
     "price": {
       "order": "desc"
     }
   }
 ],
 "_source": ["id","create_at","update_at","description"]
}



#不会重复
GET /_analyze
{
  "analyzer": "ik_smart",
  "text": ["我是你那把"]
}


# 会重复
GET /_analyze
{
  "analyzer": "ik_max_word",
  "text": ["我爱你"]
}

#####################################----filter 一般都要先加搜索条件,然后在查询
GET /orders/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "description": {
              "value": "描述"
            }
          }
        }
      ],
      "filter": [
        {
          "term": {
            "description": "描述"
          }
        }
      ]
    }
  }
}


GET /order/_search
























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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大大陈·

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值