elasticsearch两个字段比较大小查询和update

es日期类型字段条件判断及update

POST /index1/_update_by_query
{
  "query": {
    "bool": {
      "must": [
        {
          "script": {
            "script": {
              "source": "doc['pubdate'].value.getMillis() > doc['time'].value.getMillis()"
            }
          }
        }
      ]
    }
  },
  "script":{
    "source":"ctx._source['pubdate']=ctx._source['time']"
  }
}

 

你可以使用 Update API 来查询并更新两个字段。以下是 Java 中的示例代码: ```java UpdateRequest updateRequest = new UpdateRequest("your-index", "your-id") .doc(jsonBuilder() .startObject() .field("field1", "new value for field1") .field("field2", "new value for field2") .endObject()); UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT); ``` 在上面的代码中,"your-index" 是你要更新的索引名称,"your-id" 是你要更新的文档 ID。`jsonBuilder()` 方法用于构建更新的文档内容。你可以在 `.field()` 方法中指定要更新的字段名称和新值。 如果你只想更新符合某些条件的文档,你可以使用 Update By Query API。以下是 Java 中的示例代码: ```java UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest("your-index"); updateByQueryRequest.setQuery(QueryBuilders.matchQuery("field1", "value1")); updateByQueryRequest.setScript(new Script( ScriptType.INLINE, "painless", "ctx._source.field1 = params.newValue1; ctx._source.field2 = params.newValue2", Collections.singletonMap("newValue1", "new value for field1").put("newValue2", "new value for field2") )); BulkByScrollResponse bulkResponse = client.updateByQuery(updateByQueryRequest, RequestOptions.DEFAULT); ``` 在上面的代码中,"your-index" 是你要更新的索引名称。`matchQuery()` 方法用于指定要更新的文档的条件,这里是 "field1" 字段等于 "value1"。`Script()` 方法用于指定要执行的更新操作,这里是将 "field1" 和 "field2" 字段的值更新为指定的新值。调用 `updateByQuery()` 方法来执行更新操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值