使用postman访问操作ElasticSearch数据库,数据格式均为json
一、集群设置
1、查看集群设置
GET http://elasticsearch-1:9200/_cluster/settings
2、修改集群设置-自动创建索引
当auto_create_index=false时,指定⼀个不存在的索引,新增文档会报错。
当auto_create_index=true时,指定⼀个不存在的索引,新增⽂档成功,并且会创建这个不存在的索引
PUT http://elasticsearch-1:9200/_cluster/settings
{
"persistent": {
"action.auto_create_index": "false"
}
}
二、索引操作-index
1、创建索引
PUT http://elasticsearch-1:9200/nba
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"team_name": {
"type": "text"
},
"position": {
"type": "text"
},
"play_year": {
"type": "long"
},
"jerse_no": {
"type": "keyword"
}
}
}
}
2、获取索引
GET http://elasticsearch-1:9200/nba
3、关闭索引
POST http://elasticsearch-1:9200/nba/_close
4、删除索引
DELETE http://elasticsearch-1:9200/nba/
三、映射操作-mapping
1、创建映射
PUT http://elasticsearch-1:9200/nba/_mapping
{
"properties": {
"name": {
"type": "text"
},
"team_name": {
"type": "text"
},
"position": {
"type": "keyword"
},
"play_year": {
"type": "keyword"
},
"jerse_no": {
"type": "keyword"
},
"country":{
"type": "keyword"
}
}
}
2、获取映射
GET http://elasticsearch-1:9200/nba/_mapping
四、文档操作-doc
1、新增文档-指定ID
PUT http://elasticsearch-1:9200/nba/_doc/3
{
"name": "詹姆斯",
"team_name": "湖⼈",
"position": "⼩前锋",
"play_year": 15,
"jerse_no": "23"
}
2、新增文档-不指定ID
POST http://elasticsearch-1:9200/nba/_doc
{
"name":"哈登",
"team_name":"⽕箭",
"position":"得分后卫",
"play_year":"10",
"jerse_no":"13",
"country":"庞各庄"
}
3、获取文档-byId
GET http://elasticsearch-1:9200/nba/_doc/1
4、新增文档-指定操作类型
生产过程当中根据业务去添加指定类型,防止数据重复,保证数据唯一性
PUT http://elasticsearch-1:9200/nba/_doc/1?op_type=create
{
"name":"哈登",
"team_name":"⽕箭",
"position":"得分后卫",
"play_year":"10",
"jerse_no":"13"
}
5、获取文档-all
方式1:
http://elasticsearch-1:9200/_mget
{
"docs": [
{
"_index": "nba",
"_type": "_doc",
"_id": "1"
},
{
"_index": "nba",
"_type": "_doc",
"_id": "sBwWSW4BSS1Cuk2FoD69"
}
]
}
方式2:
http://elasticsearch-1:9200/nba/_mget
{
"docs": [
{
"_type": "_doc",
"_id": "1"
},
{
"_type": "_doc",
"_id": "2"
}
]
}
方式3:
http://elasticsearch-1:9200/nba/_docs/_mget
{
"docs": [
{
"_id": "1"
},
{
"_id": "2"
}
]
}
方式4:
http://elasticsearch-1:9200/nba/_docs/_mget
{
"ids": [
"1",
"sBwWSW4BSS1Cuk2FoD69"
]
}
6、修改文档-整条数据更新
POST http://elasticsearch-1:9200/nba/_update/2
{
"doc": {
"name": "库⾥",
"team_name": "勇⼠",
"position": "控球后卫",
"play_year": 10,
"jerse_no": "30",
"title": "The best 3-points shooter is Curry!"
}
}
7、新增字段
POST http://elasticsearch-1:9200/nba/_update/1
{
"script": "ctx._source.age = 18"
}
8、更新指定文档字段
POST http://elasticsearch-1:9200/nba/_update/1
{
"script": {
"source": "ctx._source.age += params.age",
"params": {
"age": 4
}
}
}
9、删除字段
POST http://elasticsearch-1:9200/nba/_update/1
{
"script": "ctx._source.remove(\"age\")"
}
10、upsert
upsert 当指定的⽂档不存在时,upsert参数包含的内容将会被插⼊到索引中,作为⼀个
新⽂档;如果指定的⽂档存在,ElasticSearch引擎将会执⾏指定的更新逻辑。
POST http://elasticsearch-1:9200/nba/_update/3
{
"script": {
"source": "ctx._source.allstar += params.allstar",
"params": {
"allstar": 4
}
},
"upsert": {
"allstar": 1
}
}
11、删除文档-byId
DELETE http://elasticsearch-1:9200/nba/_doc/3
五、搜索
1、term(词条)查询-精确查询
GET http://elasticsearch-1:9200/nba/_search
词条查询:词条查询不会分析查询条件,只有当词条和查询字符串完全匹配时,才匹配搜索。
单条查询:
{
"query": {
"term": {
"jerse_no": "23"
}
}
}
多条查询:
{
"query": {
"terms": {
"jerse_no": [
"23",
"13"
]
}
}
}
2、match_all全文查询-单字段查询
GET http://elasticsearch-1:9200/nba/_search
full text(全⽂)查询
全⽂查询:ElasticSearch引擎会先分析查询字符串,将其拆分成多个分词,只要已分析的字段中包含词条的任意⼀个,或全部包含,就匹配查询条件,返回该⽂档;如果不包含任意⼀个分词,表示没有任何⽂档匹配查询条件。
注意:每次搜索默认查询出10条,可以通过from和size设定从第几条开始查询初多少条。也就是分页
无条件全量查询:
{
"query": {
"match_all": {}
},
"from": 0,
"size": 100
}
根据查询条件分词查询:
{
"query": {
"match": {
"name": "库baby"
}
},
"from": 0,
"size": 100
}
3、multi_match全文查询-多字段查询
一个参数在多个字段中查询,字段拼接为or,非and。也就是 title='shooter' or name='shooter'
GET http://elasticsearch-1:9200/nba/_search
{
"query": {
"multi_match": {
"query": "shooter",
"fields": [
"title",
"name"
]
}
}
}
4、match_phrase(词条)-精确查询
GET http://elasticsearch-1:9200/nba/_search
{
"query": {
"match_phrase": {
"position": "得分后"
}
}
}
5、match_phrase_prefix(词条)-左前缀查询
GET http://elasticsearch-1:9200/nba/_search
{
"query": {
"match_phrase_prefix": {
"title": "the best s"
}
}
}
六、分词器
1、standard-analyzer:默认,标准分词器
标准分析器是默认分词器,如果未指定,则使⽤该分词器。
POST http://elasticsearch-1:9200/_analyze
{
"analyzer": "standard",
"text": "The best 3-points shooter is Curry!"
}
2、simple-analyzer:简单分词器
simple 分析器当它遇到只要不是字⺟的字符,就将⽂本解析成term,⽽且所有的term都是⼩写的。
POST http://elasticsearch-1:9200/_analyze
{
"analyzer": "simple",
"text": "The best 3-points shooter is Curry!"
}
3、whitespace-analyzer:空白字符分词器
whitespace 分析器,当它遇到空⽩字符时,就将⽂本解析成terms
POST http://elasticsearch-1:9200/_analyze
{
"analyzer": "whitespace",
"text": "The best 3-points shooter is Curry!"
}
4、stop-analyzer
stop 分析器 和 simple分析器很像,唯⼀不同的是,stop分析器增加了对删除停⽌词的⽀持,默认使⽤了english停⽌词
stopwords 预定义的停⽌词列表,⽐如 (the,a,an,this,of,at)等等
POST http://elasticsearch-1:9200/_analyze
{
"analyzer": "stop",
"text": "The best 3-points shooter is Curry!"
}
5、language-analyzer:语言分词器
(特定的语⾔的分词器,⽐如说,english,英语分词器),内置语⾔:arabic, armenian,basque, bengali, brazilian, bulgarian, catalan, cjk, czech, danish, dutch, english, finnish,french, galician, german, greek, hindi, hungarian, indonesian, irish, italian, latvian,lithuanian, norwegian, persian, portuguese, romanian,russian, sorani, spanish,swedish, turkish, thai)
POST http://elasticsearch-1:9200/_analyze
{
"analyzer": "english",
"text": "The best 3-points shooter is Curry!"
}
6、pattern-analyzer:正则分词器
⽤正则表达式来将⽂本分割成terms,默认的正则表达式是\W+(⾮单词字符)
POST http://elasticsearch-1:9200/_analyze
{
"analyzer": "pattern",
"text": "The best 3-points shooter is Curry!"
}
7、IK-analyzer:推荐使用
POST http://elasticsearch-1:9200/_analyze
{
"analyzer": "ik_max_word",
"text": "2019年双十一再一次证明中国老百姓的财富时取之不尽用之不竭的"
}
8、创建索引时-设置分词器
PUT http://elasticsearch-1:9200/analyzer_index
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"type": "whitespace"
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text"
},
"team_name": {
"type": "text"
},
"position": {
"type": "text"
},
"play_year": {
"type": "long"
},
"jerse_no": {
"type": "keyword"
},
"title": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}