elasticsearch_基本操作

ElasticSearch

1、_cat
get /_cat/nodes; 查看所有节点

get /_cat/health; 查看es的健康状态

get /_cat/master; 查看主节点

get /_cat/indices; ch爱看所有索引 :show database
2、保存文档
put /customer/external/1
{
	"name":"张三"
}
  • put需要携带id,如果id号数据存在,那么会更新id号数据的内容,如果不存在,就新增数据
post /customer/external
{
  "name":"王五"
}
  • post可携带也可不携带id,如果不携带就是增加,携带了就跟put一样。
putpost
必须携带id不一定要携带id
id数据存在就更新,不存在就增加不携带就增加,携带了和put一样
3、查询文档
get /customer/external/_search
4、更新文档

使用之前的put和post都可以更新操作,但是会一致增加版本号和seq_no

使用_doc方法不会增加版本号和seq_no

post /customer/external/1/_update
{
	"doc":{
		"name":"测试"
	}
}

使用这个的话,不会一致版本累加,更新之前会先对数据进行对比

也可以使用_doc进行增加数据操作

post /customer/external/1/_update
{
	"doc":{
		"name":"测试",
		"age"
	}
}
5、删除文档

可以根据id删除文档

delete /customer/external/1
6、批量操作API

_bulk

POST customer/external/_bulk
{"index":{"_id":"11111"}}
{"name":"John Doe"}
{"index":{"_id":"222222"}}
{"name":"John Doe"}
  • 先标识操作,然后再进行赋值
POST /_bulk
{"delete":{"_index":"website","_type":"blog","_id":"123"}}
{"create":{"_index":"website","_type":"blog","_id":"123"}}
{"title":"My first blog post"}
{"index":{"_index":"website","_type":"blog"}}
{"title":"My second blog post"}
{"update":{"_index":"website","_type":"blog","_id":"123"}}
{"doc":{"title":"My update blog post"}}

进阶检索

Quert DSL(基本使用)

基本语法

GET /bank/_search
{
  "query": {},  //查询规则
  "sort": [		//排序方式
    {
      "FIELD": {
        "order": "desc"
      }
    }
  ],
  "from": 0,		//起始页
  "size": 20,		//页面大小
  "aggs": {			//聚合
    "NAME": {
      "AGG_TYPE": {}
    }
  }
}
1、query

查询条件

1、match

全文匹配:会对所查找的关键词进行分词,然后按照分词匹配,比如“张三”,会被分成“张”和“三”进行匹配。

GET /bank/_search
{
  "query": {
    "match": {
      "FIELD": "TEXT"
    }
  }
}
2、match_all

全部匹配,没有检索条件

GET /bank/_search
{
  "query": {
    "match_all": {}
  }
}
3、match_phrase

短词匹配,会把这个短语当成一个整体来匹配

GET /bank/_search
{
  "query": {
    "match_phrase": {
      "address": "mill road"
    }
  }
}
4、maulti_match

相当于like

GET /bank/_search
{
  "query":{
    "multi_match": {
      "query": "mill movico",
      "fields": ["address","city"]
    }
  }
}

在address和city字段里面,只要匹配到query里面的字段的都会被查询出来。

5、bool

复合查询

1、must

必须满足

GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "firstname": "Hattie"
          }
        }
      ]
    }
  }
}
2、must_not

必须不满足

GET /bank/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "firstname": "Hattie"
          }
        }
      ]
    }
  }
}
3、should

满足最好

GET /bank/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "firstname": "Hattie"
          }
        }
      ]
    }
  }
}

4、filter

用must查询会有相关性得分,用filter查询是不会有的,速度相对来说比较快

must

GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        {
         "range": {
           "age": {
             "gte": 10,
             "lte": 20
           }
         }
        }
      ]
    }
  }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S63NLPvm-1619320724542)(Images/image-20210422170419652.png)]

filter

GET /bank/_search
{
  "query": {
    "bool": {
      "filter": [
        {
         "range": {
           "age": {
             "gte": 10,
             "lte": 20
           }
         }
        }
      ]
    }
  }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8xCur466-1619320724544)(Images/image-20210422170448451.png)]

6、term

和match一样,匹配某个属性的值,全文检索用match,其他非text字段匹配用term,item是不会分词的。

GET /bank/_search
{
  "query": {
    "term": {
      "balance": {
        "value": "39868"
      }
    }
  }
}
7、 .keyword

精确查找,得整个字段匹配才会命中查询

GET /bank/_search
{
  "query": {
    "match": {
      "address.keyword": "132 Gunnison Court"
    }
  }
}
2、from-to

分页

GET /bank/_search
{
  "query": {
    "match_all": {}
  },
  "from":0,
  "size":5
}
3、sort

排序

GET bank/_search
{
    "query":{              
        "match_all":{}
    },
    "sort":[				//排序的规则
        {
            "account_number":"asc"
        },
        {
            "balance":"desc"
        }
    ]
}
4、_source

指定返回字段

GET /bank/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["firstname","lastname"]
}
5、aggs

聚合查询

  • 搜索address中包含mill的所有人的年龄分布以及平均年龄,但是不显示这些人的详情

    • 搜索address中包含mill
    GET /bank/_search
    {
      "query": {
        "match": {
          "address": "mill"
        }
      }
    }
    
    • 年龄分布
    GET /bank/_search
    {
      "query": {
        "match": {
          "address": "mill"
        }
      },
      "aggs": {
        "ageagg": {
          "terms": {
            "field": "age",
            "size": 10
          }
        }
      }
    }
    
    • 平均年龄
    GET /bank/_search
    {
      "query": {
        "match": {
          "address": "mill"
        }
      },
      "aggs": {
        "ageagg": {
          "terms": {
            "field": "age",
            "size": 10
          }
        },
        "ageavgagg":{
          "avg": {
            "field": "age"
          }
        }
      }
    }
    
    • 查询结果

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XWQQn2MO-1619320724546)(Images/image-20210422171708310.png)]

Mapping

1、字段类型

创建索引之前指定类型

PUT /my_index
{
  "mappings": {
    "properties": {
      "age":{"type": "integer"},
      "name":{"type": "text"},
      "exmail":{"type": "keyword"}
    }
  }
}

指定为keyword之后只能够精确检索。

2、添加新的字段映射

在原有的属性里新加一个字段

PUT /my_index/_mapping
{
  "properties": {
    "employee_id":{
      "type":"keyword",
      "index":false
    }
  }
}
3、修改映射&&数据迁移

不能修改已存在的映射,只能够创建新的映射进行数据迁移

新建映射

PUT /newbank
{
    "mappings": {
        "properties": {
            "account_number": {
                "type": "long"
            },
            "address": {
                "type": "text"
            },
            "age": {
                "type": "integer"
            },
            "balance": {
                "type": "long"
            },
            "city": {
                "type": "keyword"
            },
            "email": {
                "type": "keyword"
            },
            "employer": {
                "type": "keyword"
            },
            "firstname": {
                "type": "text"
            },
            "gender": {
                "type": "keyword"
            },
            "lastname": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "state": {
                "type": "keyword"
            }
        }
    }
}

数据迁移

POST _reindex
{
    "source": {
        "index": "bank"
    },
    "dest": {
        "index": "newbank"
    }
}
4、分词

安装ik分词器

要下载的版本和elasticsearch版本一致

下载好解压放在elasticsearch的plugin包下面

重启elasticsearch

成功把中文分词

5、自定义扩展词库

安装nginx,在windows下下载好点击运行即可

在nginx的html包目录下,新建es/fenci.txt

修改elasticsearch下的ik下的config配置文件

测试成功,详细部分查看谷粒商城资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值