ElasticSearch02_Restful风格基本操作
ElasticSearch02
Restful 风格操作索引:
1. 创建索引示例:
PUT /索引名/类型名/文档id
{请求体}
PUT /test1/_doc/1
{
"name": "BLU",
"age": 3,
"birth":"1998-09-30"
}
2. 查询索引信息(创建时没有指定字段的类型,但ES智能的配置了字段类型):
GET test1
{
"test1" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"birth" : {
"type" : "date"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1597834637327",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "tp92cyypT0yXtJ9EsRW1MA",
"version" : {
"created" : "7070199"
},
"provided_name" : "test1"
}
}
}
}
基本数据类型:
字符串类型: text keyword 注:text类型可以被分词器解析,而keyword类型不会被分词器解析
数值类型: long integer short byte double float half float scaled float
日期类型: date
布尔类型:boolean
二进制类型:binary
......
3. 创建索引并指定字段的类型:
PUT /test2
{
"mappings": {
"properties": {
"name":{
"type": "text"
},
"age":{
"type": "long"
},
"birthday":{
"type": "date"
}
}
}
}
查询索引信息:
GET test2
{
"test2" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"birthday" : {
"type" : "date"
},
"name" : {
"type" : "text"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1597833899073",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "KpZ-7iOOTR2dpxh6WloWUw",
"version" : {
"created" : "7070199"
},
"provided_name" : "test2"
}
}
}
}
4. 查看健康值信息:
GET _cat/health
1597835176 11:06:16 elasticsearch yellow 1 1 7 7 0 0 3 0 - 70.0%
5. 查看ES所有index:
GET _cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open test2 KpZ-7iOOTR2dpxh6WloWUw 1 1 0 0 208b 208b
yellow open test3 QxA5gefcSM6-uzqYrfH3uw 1 1 1 0 4kb 4kb
green open .apm-custom-link U03Ki1pkRU63zQ3IydY96w 1 0 0 0 208b 208b
green open .kibana_task_manager_1 MdDmfW_2QGecdl0-bqAWWQ 1 0 5 0 75.9kb 75.9kb
green open .apm-agent-configuration b3RptmMRS_W3YXSPL4Abzw 1 0 0 0 208b 208b
green open .kibana_1 R4adYkTXTLC5dKfJhEgS5A 1 0 46 7 175.6kb 175.6kb
yellow open test1 tp92cyypT0yXtJ9EsRW1MA 1 1 1 0 4kb 4kb
6. 修改文档数据:
POST /test1/_doc/1/_update
{
"doc":{
"name": "法外狂徒张三",
"age": 30
}
}
{
"_index" : "test1",
"_type" : "_doc",
"_id" : "1",
"_version" : 2
,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
7. 删除文档 / 删除索引:
DELETE test1/_doc/1
DELETE test1
Restful 风格操作文档:
1. 创建索引并添加数据:
PUT /blu/user/1
{
"name": "法外狂徒张三",
"age": 23,
"desc": "Hello! My name is 张三",
"tags": ["Spring","Mybatis","JAVA","MySQL"]
}
PUT /blu/user/2
{
"name": "李四",
"age": 30,
"desc": "Hello! My name is 李四",
"tags": ["SpringBoot","JPA","Python","Oracle"]
}
PUT /blu/user/3
{
"name": "王五",
"age": 65,
"desc": "Hello! My name is 王五",
"tags": ["Hibernate","C++","ElasticSearch"]
}
PUT /blu/user/4
{
"name": "小张三",
"age": 24,
"desc": "Hello! My name is 张三",
"tags": ["Hibernate","C","ElasticSearch"]
}
2. PUT 更新数据(不推荐):
PUT /blu/user/2
{
"name": "lisi23333",
"age": 30,
"desc": "Hello! My name is lisi",
"tags": ["SpringBoot","JPA","Python","Oracle"]
}
3. POST 更新数据(推荐):
POST /blu/user/2/_update
{
"doc":{
"name": "lisihahaha"
}
}
4. 查询:
GET blu/user/1
GET blu/user/_search?q=name:法外狂徒
GET blu/user/_search
{
"query": {
"match": {
"name": "法外狂徒"
}
}
}
结果过滤:查询name中带张三的文档数据,只返回name和desc字段
GET blu/user/_search
{
"query": {
"match": {
"name": "张三"
}
},
"_source": ["name","desc"]
}
排序查询:此时结果的_score字段为null
GET blu/user/_search
{
"query": {
"match": {
"name": "张三"
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
分页查询:form从第几个数据开始(0开始),size每页几个数据
GET blu/user/_search
{
"query": {
"match": {
"name": "张三"
}
},
"sort": [
{
"age": {
"order": "asc"
}
}
],
"from": 0,
"size": 2
}
多条件查询:
must 表示所有条件都要符合,等价and
should 表示只要符合一个条件即可,等价or
must_not 表示查询过滤该条件的文档数据,等价!=
GET blu/user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "张三"
}
},
{
"match": {
"age": "14"
}
}
]
}
}
}
范围过滤,查询name带张三,年龄大于10,小于等于20的数据:
GET blu/user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "张三"
}
}
],"filter": [
{
"range": {
"age": {
"gt": 10,
"lte": 20
}
}
}
]
}
}
}
多条件匹配:查询name中有张三或四的,条件使用空格隔开
GET blu/user/_search
{
"query": {
"match": {
"name": "张三 四"
}
}
}
查询结果高亮:在查询出的结果中,搜索关键字会自动添加<em>标签
GET blu/user/_search
{
"query": {
"match": {
"name": "张三"
}
},
"highlight": {
"fields": {
"name":{ }
}
}
}
自定义高亮标签:
GET blu/user/_search
{
"query": {
"match": {
"name": "张三"
}
},
"highlight": {
"pre_tags": "<span class='key' style='color:red'>",
"post_tags": "</span>",
"fields": {
"name":{ }
}
}
}
还没有评论,来说两句吧...