elasticsearch 常用命令总结

本文详细介绍了Elasticsearch的基本操作,包括索引的创建、映射定义、文档的增删改查以及批量导入等核心功能,并提供了实际使用的curl命令示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.创建一个索引 (index 是索引的名字)

curl -XPUT http://localhost:9200/indexName

2.映射相光

#创建一个映射
curl -XPOST http://localhost:9200/quanzi/fulltext/_mapping -d'
{
        "properties": {
            "content": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_smart"
            }
        }
}'
#查看映射
curl -XGET "http://localhost:9200/quanzi/_mapping/fulltext?pretty"
#新增一个字段
curl -XPUT "http://localhost:9200/quanzi/_mapping/fulltext"; -H 'Content-Type: application/json' -d'
{
 
        "properties": {
            "tags4": {
                "type":     "keyword"
            }
        }
    
}'


3.分析器的测试

 #老版
http://localhost:9200/quanzi/_analyze?text=糜烂胎停宫腔粘连&tokenizer=ik_smart
#6.0以上版本
curl -XGET "http://localhost:9200/_analyze"; -H 'Content-Type: application/json' -d'
{
  "analyzer" : "ik_max_word",
  "text" : "糜烂胎停宫腔粘连”  
}’

4.索引的添加和修改

#添加一个文档
curl -XPUT "http://localhost:9200/quanzi/article/2"; -H 'Content-Type: application/json' -d'
{ 
    "content":"中国的女子很多有这个问题",
    "tags":["试管","高龄"]
  }'

#更新部分数据
上面的脚本是对所有的文档都起作用,这里讲解下如何只对部分文档进行修改。使用doc可以实现简单的递归合并、内部合并、替换KV以及数组。
curl -XPOST "http://localhost:9200/quanzi/article/1/_update"; -H 'Content-Type: application/json' -d'
{ 
  "doc":{ 
    "tags":["多囊","高龄"]}
  }'
如果同时使用了doc和script,那么doc的操作会自动忽略。因此最好是把特殊的操作也放在脚本中。

5.删除的操作

#.批量删除文档
curl -XPOST "http://localhost:9200/feeds/_delete_by_query"; -H 'Content-Type: application/json' -d'
{

  "query": {
    "match": {
      "type": "article"
    }
  }
}’
#.删除单个文档
    curl -XDELETE "http://localhost:9200/feeds/feed/article_59f9385843707f81a34a6540”;
#.删除索引
    curl -XDELETE "http://localhost:9200/quanzi"
    curl -XDELETE "http://localhost:9200/index_*";
    curl -XDELETE "http://localhost:9200/quanzi,test";
    curl -XDELETE "http://localhost:9200/_all";
    curl -XDELETE "http://localhost:9200/*";

6.批量导入(文件和控制台在同一服务器上操作)

curl -H "Content-Type: application/json;charset=UTF-8" -XPOST 'http://localhost:9200/_bulk?pretty' --data-binary @word.json
#word.json 的格式如下:
导入格式如下:
{ "index" : { "_index" : "association", "_type" : "word", "_id" : "doctor_1111" }}
{"content":"杨静","type":2,"frequency":1,"location":"","code":"11111","hospital":{"name":"解放军第306医院","id":"11111"}}
{ "index" : { "_index" : "association", "_type" : "word", "_id" : "doctor_2222" }}
{"content":"兰永连","type":2,"frequency":1,"location":"","code":"2222","hospital":{"name":"北京妇产医院","id":"22222"}}
{ "index" : { "_index" : "association", "_type" : "word", "_id" : "doctor_1425276716764" }}





### Elasticsearch 常用 API 请求示例 以下是几种常见的 Elasticsearch API 请求及其用途: #### 1. 创建索引 通过 `PUT` 方法可以创建一个新的索引,并定义其映射结构。以下是一个创建为 `blog` 的索引并指定 `article` 类型的示例[^4]。 ```json PUT /blog { "mappings": { "properties": { "id": { "type": "long", "store": true, "index": true }, "title": { "type": "text", "store": true, "index": true, "analyzer": "standard" }, "content": { "type": "text", "store": true, "index": true, "analyzer": "standard" } } } } ``` --- #### 2. 插入文档 可以通过 `POST` 或 `PUT` 方法向已存在的索引中插入文档。以下是在 `blog` 索引下插入一条记录的示例。 ```json POST /blog/_doc/1 { "id": 1, "title": "Introduction to Elasticsearch", "content": "This is an introduction to using Elasticsearch." } ``` --- #### 3. 查询文档 使用 `_search` 接口执行查询操作,支持多种查询方式。下面展示了基于布尔查询返回状态码为 `200` 的前 10 条记录的例子[^2]。 ```json GET /_search { "size": 10, "query": { "bool": { "must": [ { "term": { "status": 200 } } ] } } } ``` --- #### 4. 更新文档 更新现有文档的部分字段可使用 `POST _update/{id}` 接口。例如,更新 ID 为 `1` 的文档标题。 ```json POST /blog/_update/1 { "doc": { "title": "Updated Title of the Document" } } ``` --- #### 5. 删除文档 删除特定文档可通过 `DELETE` 方法实现。例如,删除 ID 为 `1` 的文档。 ```json DELETE /blog/_doc/1 ``` --- #### 6. 使用 DSL 进行复杂查询 对于更复杂的场景,可以利用 Elasticsearch 提供的 Query DSL 执行高级查询。以下例子演示了如何查找 `content` 字段包含关键词 `elastic` 同时满足条件 `(title 包含 lucene 或 solr)` 的文档[^3]。 ```json GET /_search { "query": { "query_string": { "default_field": "content", "query": "elastic AND (title:lucene OR title:solr)" } } } ``` --- #### 7. 获取集群健康状况 监控 Elasticsearch 集群的状态可用如下命令获取整体健康信息。 ```json GET /_cluster/health ``` --- #### 8. 设置集群配置 动态调整集群级别的参数也可以通过 RESTful API 实现。比如修改分片分配策略。 ```json PUT /_cluster/settings { "persistent": { "cluster.routing.allocation.enable": "all" } } ``` --- ### 总结 上述列举了几种常用的 Elasticsearch API 请求形式,涵盖了从基础的数据增删改查到较为深入的集群管理功能。每一种接口都提供了灵活多样的选项来适应不同的业务需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值