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" }}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值