Elasticsearch 增删改查

本文档详细介绍了如何使用Elasticsearch进行增删改查操作,包括查询文档、范围查询、嵌套查询、批量更新、删除特定条件的文档以及新增文档的步骤。重点关注了根据字段条件进行筛选和操作的方法。

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

一、查询(_search)

例如,查询index 和 type 为idx_ot的所有文档,下面的例子忽略curl中的公共部分,只展示query部分

curl -XPOST -H "Content-Type: application/json" -H "Authorization: xxx "
 http://11.5.44.232:40002/idx_ot/idx_ot/_search?pretty -d '{
"query": {
  "match_all": {}
 }
}'

1.比较两个字段不同

{
    "query":{
        "bool":{
            "filter":{
                "script":{
                    "script":{
                        "source":"doc['modifyTime'].value != doc['createTime'].value",
                        "lang":"painless"
                    }
                }
            }
        }
    }
}

查询modifyTime和createTime不相同的文档

2. 范围查询

{
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "otQuestionId":{
                            "gt":"340",
                            "lt":"364"
                        }
                    }
                }
            ]
        }
    }
}

3. 嵌套查询

{
    "query":{
        "bool":{
            "must":[
                {
                    "term":{
                        "id":"1d061e26-10b7-489b-9d2e-296a9b5d78c0"
                    }
                },
                {
                    "nested":{
                        "path":"answers",
                        "query":{
                            "bool":{
                                "must":[
                                    {
                                        "term":{
                                            "answers.id":"1d061e26-10b7-489b-9d2e-296a9b5d78c0"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}

 

二、更新(_update_by_query)

查询的 cid2="大家电",cid3="空调" 对应文档的status字段全部更新为'0'(每个文档都有cid2、cid3、status三个字段)

curl -XPOST -H "Content-Type: application/json" -H "Authorization: Basic xxx" 
http://11.5.43.37:40000/deep_migration_dev/deep_migration_dev/_update_by_query?
pretty -d
 '{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "cid2": "大家电"
          }
        },
        {
          "term": {
            "cid3": "空调"
          }
        }
      ]
    }
  }
,
  "script": {
    "lang": "painless", 
    "inline": "ctx._source.status = '0' "
  }

}'

三、删除( _delete_by_query  )

把 cid2="大家电",cid3="空调" 对应文档全部删除

curl -XPOST -H "Content-Type: application/json" -H "Authorization: Basic xxx "
 http://11.5.43.37:40000/deep_migration_dev/deep_migration_dev/_delete_by_query?
pretty -d '{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "cid2": "大家电"
          }
        },
        {
          "term": {
            "cid3": "空调"
          }
        }
      ]
    }
  }

}'

四、增加

增加一个文档不需要指定id,如何指定则会覆盖之前的id的文档。

curl -XPOST -H "Content-Type: application/json" -H "Authorization: Basic xxx"
 http://11.5.43.37:40000/deep_migration_dev/deep_migration_dev/?pretty -d 
'{
"question": "能不能推荐一下款一级能效,价格实惠的",
"answerSeg": "稍等",
"updateTime": 1538140001000,
"source": "3",
"version": "2018",
"answer": "稍等",
"createTime": 1547540720477,
"extend2": "1000001452",
"questionSeg": "能 不能 推荐 一 下款 一级 能效 价格 实惠 的",
"cid2": "大家电",
"cid3": "空",
"id": "Rule_611255",
"shopId": "50271",
"tag": "能耗",
"sku": "3968191",
"brand": "美",
"cid1": "家用电器",
"status": 0
}'

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值