elasticsearch常见操作

索引index

查看集群状态:

复制代码
GET _cat/health

查看所有index:

说明:v是用来要求在结果中返回表头

复制代码
GET _cat/indices?v
GET _aliases

查看dev开头的index:

复制代码
GET _cat/indices/dev*

删除指定index:

复制代码
DELETE test_omp

复制或索引重命名:

复制代码
### 直接复制索引到新的索引名称
POST localhost:9200/_reindex
{
  "source": {
    "index": "indexName"
  },
  "dest": {
    "index": "newIndexName"
  }
}

### 查询复制索引到新的索引名称
POST localhost:9200/_reindex
{
  "source": {
    "index": "indexName",
    "type": "typeName",
    "query": {
      "term": {
        "name": "shao"
      }
    }
  },
  "dest": {
    "index": "newIndexName"
  }
}

文档document

https://blog.csdn.net/qq_19006223/article/details/110124775

https://javaedge.blog.csdn.net/article/details/110599524

查看指定索引指定id的文档:

复制代码
GET /test_gateway/_doc/L_-4DH0BA5KB_D7Szmt8

查看指定索引的所有文档(并根据指定字段排序,默认返回10条):

复制代码
GET /test_gateway/_search?q=*&sort=request_log.appType.keyword:asc&pretty
或json格式:
GET /test_gateway/_search
{
  "query": { "match_all": {} },
  "sort": [
    {"request_log.appType.keyword": "asc" }
  ]
}

模糊搜索:

复制代码
GET prod_artwork-omp/_search?from=0&size=2
{
  "query": {
    "wildcard": {
      "omp_request_log.uri": "upload"
    }
  }
}

新增文档使用PUT、POST请求。

复制代码
PUT /<target>/_doc/<_id>
POST /<target>/_doc/
PUT /<target>/_create/<_id>
POST /<target>/_create/<_id>

target为索引名称,_doc为默认type。通过前两种请求可看出,id可以自行指定,也可以由ES自动生成。_create可以保证只有当文档不存在的时候进行插入,而_doc还可以用于文档更新。

插入文档(也可指定_id:POST test_omp/_doc/{_id}):

复制代码
POST test_omp/_doc
{
  "omp_request_log": {
    "myid": "id001"
  }
}

删除文档(也支持json格式):

复制代码
DELETE test_omp/_doc/AQQmDn0BA5KB_D7SC_7f

POST /test_omp/_delete_by_query
{
  "query": {
    "match": {
      "_id": "AQQmDn0BA5KB_D7SC_7f"
    }
  }
}

修改文档:

复制代码
# 指定字段更新
POST test_omp/_update/9gUwDn0BA5KB_D7SWQLN
{
  "doc": {
      "omp_request_log": {
        "id": "myid002"
    }
  }
}

# 全字段更新
POST test_omp/_doc/9gUwDn0BA5KB_D7SWQLN
{
  "omp_request_log": {
    "id": "myid003"
  }
}

此外还可以通过脚本修改,例如将所有存在age字段的文档,其值改成5岁。

复制代码
POST /user/_update_by_query
{
  "script": {
    "source": "ctx._source['age']=5" 
  },
  "query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field": "age"
          }
        }
      ]
    }
  }
}

此外update和_update_by_query字段还可以修改Filed,例如将name修改成name1,这块内容使用较少,如感兴趣,请参考官方文档。

映射和类型mapping

https://blog.csdn.net/Andre_dong/article/details/109672646

include_type_name:

在Elasticsearch 6.0.0或更高版本中创建的索引只能包含一个映射类型.在5.x中创建的具有多种映射类型的索引将继续像在Elasticsearch 6.x中一样工作.映射类型将在Elasticsearch 7.0.0中完全删除。

查看指定index的映射:

复制代码
GET test_omp/_mapping

添加指定index的映射(会顺带添加index):

复制代码
PUT test_omp?include_type_name=false
{
    "mappings": {
        "properties": {
            "service": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "class": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "omp_request_log": {
                "properties": {
                    "id": {
                        "type": "text"
                    },
                    "requestId": {
                        "type": "text"
                    },
                    "requestMethod": {
                        "type": "keyword"
                    },
                    "uri": {
                        "type": "text"
                    },
                    "requestHeader": {
                        "type": "text"
                    },
                    "requestParams": {
                        "type": "text"
                    },
                    "requestBody": {
                        "type": "text"
                    },
                    "responseCode": {
                        "type": "text"
                    },
                    "responseMsg": {
                        "type": "text"
                    },
                    "responseBody": {
                        "type": "text"
                    },
                    "bizResponseBody": {
                        "type": "text"
                    },
                    "exception": {
                        "type": "text"
                    },
                    "ipAddress": {
                        "type": "text"
                    },
                    "execTime": {
                        "type": "long"
                    },
                    "sysId": {
                        "type": "text"
                    },
                    "createTime": {
                        "type": "date"
                    },
                    "updateTime": {
                        "type": "date"
                    }
                }
            }
        }
    }
}

类型type:

https://www.cnblogs.com/koenigsea/p/13072923.html

相关推荐
艾莉丝努力练剑4 分钟前
Al Ping免费上新:GLM-4.7 && MiniMaxM2.1重磅上线,附独家使用教程
java·大数据·linux·运维·人工智能·python
semantist@语校7 分钟前
第五十七篇|东京银星日本语学校的数据建模:高密度城市中的学习节律、制度边界与 Prompt 接口设计
大数据·数据库·人工智能·学习·百度·prompt·知识图谱
无代码专家10 分钟前
无代码技术:企业全场景数字化的降本增效新范式
大数据·人工智能·低代码·云计算
TDengine (老段)17 分钟前
携手桂冠电力、南网储能、中能拾贝,TDengine 三项案例入选“星河奖”
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
神算大模型APi--天枢64621 分钟前
国产大模型后端开发与部署实战:从算力架构到行业规模化落地
大数据·运维·服务器·人工智能·架构
ctrigger26 分钟前
高级《高级统计实务》考试大纲
大数据·人工智能
JZC_xiaozhong32 分钟前
如何统一管理自研系统与外购系统的用户权限?
大数据·iam·企业数据安全·数据集成与应用集成·权限治理·多系统权限管理·统一权限管理
青木川崎39 分钟前
大数据技术之hive
大数据·hive·hadoop
电商API_1800790524739 分钟前
进阶篇:电商商品评论情感分析 + 关键词挖掘(Python NLP 实战)
大数据·开发语言·网络·数据库·人工智能
SelectDB技术团队1 小时前
慢 SQL 诊断准确率 99.99%,天翼云基于 Apache Doris MCP 的 AI 智能运维实践
大数据·数据库·人工智能·sql·apache