基于go的elasticsearch简易教程(1)
elasticsearch7入门指南
安装
docker安装
docker pull elasticsearch:7.14.0
docker run -d -p 9310:9300 -p 9210:9200 -e "discovery.type=single-node" elasticsearch:7.14.0
docker ps
测试
☁ ~ curl http://127.0.0.1:9210
{
"name" : "4888120ad527",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "Z572AGx6T7K-WKRCYBcRWA",
"version" : {
"number" : "7.14.0",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1",
"build_date" : "2021-07-29T20:49:32.864135063Z",
"build_snapshot" : false,
"lucene_version" : "8.9.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
安装es的客户端工具kibana
docker pull kibana:7.14.0
// 本地配置映射到容器
docker run -d --name kibana -v /Users/liang.zhang02/adjutantz/es/kibana/kibana.yml:/usr/share/kibana/config/kibana.yml -p 5611:5601 kibana:7.14.0
访问kibana
http://127.0.0.1:5611
索引
索引常见操作
# 查询索引
GET /_cat/indices?v
# 创建索引
PUT /products
# 创建副本为0的索引
PUT /orders
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
# 删除索引
DELETE /products
映射
es常见数据类型
名称 | 类型 |
---|---|
字符串 | keyword(不分词),text(分词) |
整数 | integer |
小数 | float,double(精度范围更广) |
布尔 | boolean |
日期 | date |
常见操作
# 创建
PUT /products
{
"settings": {
"number_of_replicas": 0,
"number_of_shards": 1
},
"mappings": {
"properties": {
"id":{
"type": "integer"
},
"title":{
"type": "keyword"
},
"description":{
"type": "text"
},
"price":{
"type": "float"
},
"created_at":{
"type": "date"
}
}
}
}
# 查看
GET /products/_mapping
文档
常见操作
# 新增文档,指定ID
POST /products/_doc/1
{
"id": 1,
"title": "大麦茶",
"description": "好喝的大麦茶",
"price": 4.50,
"created_at": "2022-10-02"
}
# 新增文档,自动生成ID【C0_whn8BeuVBNeP_QdhB】
POST /products/_doc
{
"id": 2,
"title": "乌龙茶",
"description": "好喝的乌龙茶",
"price": 4.50,
"created_at": "2022-10-02"
}
# 根据ID查询文档
GET /products/_doc/1
# 根据ID查询文档
GET /products/_doc/C0_whn8BeuVBNeP_QdhB
# 删除文档
DELETE /products/_doc/1
# 更新文档【先删除,再添加】
PUT /products/_doc/C0_whn8BeuVBNeP_QdhB
{
"title": "乌龙茶场馆"
}
# 更新指定字段
POST /products/_doc/C0_whn8BeuVBNeP_QdhB/_update
{
"doc":{
"id": 2,
"title": "乌龙茶场馆",
"description": "好喝的乌龙茶2",
"price": 5.50
}
}
# 批量添加文档
POST /products/_doc/_bulk
{"index":{"_id":"5"}}
{"id":5, "title":"5-title","description":"5-desc","price":8.88}
{"index":{"_id":"6"}}
{"id":6, "title":"6-title","description":"6-desc","price":9.99}
# 批量增删查改,注意update需要"doc"
POST /products/_doc/_bulk
{"index":{"_id":7}}
{"id":7, "title":"7-title","description":"7-desc","price":77.77}
{"update":{"_id":5}}
{"doc":{"title":"5-title-update"}}
{"delete":{"_id":6}}
DSL(Domain Specific Language)
查询文档所有match_all
# 查询所有
GET /products/_doc/_search
{
"query":{
"match_all":{}
}
}
# 简化,比较常用的方式
GET /products/_search
{
"query": {
"match_all": {}
}
}
条件搜索term
# term 匹配
# keyword:不分词,全部匹配
# text:分词:中文单个字,英文单词
GET /products/_search
{
"query": {
"term": {
"description": {
"value": "麦"
}
}
}
}
# keyword
GET /products/_search
{
"query": {
"term": {
"title": {
"value": "大麦茶"
}
}
}
}
# integer
GET /products/_search
{
"query": {
"term": {
"id": {
"value": "17"
}
}
}
}
# double
GET /products/_search
{
"query": {
"term": {
"price": {
"value": "8.99"
}
}
}
}
### 总结
# 1. elasticsearch中除了text会分词,其余的均不分词,即需要全部匹配
# 2. elasticsearch标准库中英文是以单词分词,中文是以单个字分词
范围查询range
GET /products/_search
{
"query": {
"range": {
"price": {
"gt": 5,
"lte": 10
}
}
}
}
前缀查询prefix
# 前缀查询,keyword不分词,可实现类似于like {{前缀}}%
GET /products/_search
{
"query": {
"prefix": {
"title": {
"value": "乌龙"
}
}
}
}
# 前缀查询,text分词,在标准库是单个中文字分词
GET /products/_search
{
"query": {
"prefix": {
"description": {
"value": "好"
}
}
}
}
通配符查询wildcard
# 通配符查询
# ?代表匹配一个,*代表匹配多个
GET /products/_search
{
"query": {
"wildcard": {
"description": {
"value": "appl?"
}
}
}
}
GET /products/_search
{
"query": {
"wildcard": {
"description": {
"value": "ap*"
}
}
}
}
通过id查询
# ids查询
GET /products/_search
{
"query": {
"ids": {
"values": [17,7,5]
}
}
}
相关性查询
# ids查询
GET /products/_search
{
"query": {
"ids": {
"values": [17,7,5]
}
}
}