es查询字段更新记录_ElasticSearh更新nested字段(Array数组)。怎么根据查询条件(query)复制一个(index)到新的Index how to update by query...

GET usernested/_search

{

"query": {

"nested": {

"path": "tags",

"query": {

"bool": {

"must": [

{

"term": {

"tags.brandid": "93a8296c-5b64-49ea-bd92-b19192def2e9"

}

},

{

"term": {

"tags.site": "163"

}

}

]

}

}

}

}

}

//按照条件新建一个 index 作为测试数据使用

POST _reindex

{

"source": {

"index": "usernested",

"query": {

"nested": {

"path": "tags",

"query": {

"bool": {

"must": [

{

"term": {

"tags.brand": "c55fd643-1333-4647-b898-fb3e5e4e6d67"

}

},

{

"term": {

"tags.site": "163"

}

}

]

}

}

}

}

},

"dest": {

"index": "new_usernested"

}

}

//查询

GET new_usernested/_search

{

"query": {

"nested": {

"path": "tags",

"query": {

"bool": {

"must": [

{

"term": {

"tags.brand": "c55fd643-1333-4647-b898-fb3e5e4e6d67"

}

},

{

"term": {

"tags.site": "163"

}

}

]

}

}

}

}

}

//根据条件更新一个 nested 的文档

GET usernested/_update_by_query

{

"query": {

"nested": {

"path": "tags",

"query": {

"bool": {

"must": [

{

"term": {

"tags.brand": "c55fd643-1333-4647-b898-fb3e5e4e6d67"

}

},

{

"term": {

"tags.site": "163"

}

}

]

}

}

}

},

"script": {

"inline": "for(e in ctx._source.tags){e.brand = 'test2';}" //更新 nested 字段

//"inline":"ctx._source.userid = 'testid'" //更新普通字段

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用子查询nested query)可以实现在一个字段中查找多个值的需求。以下是使用子查询实现 match_phrase 查询一个字段的多个值的示例代码: ``` SearchRequest searchRequest = new SearchRequest("your_index_name"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); // 构建子查询 QueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("your_nested_field", QueryBuilders.matchPhraseQuery("your_nested_field.your_text_field", "your search phrase"), ScoreMode.None); // 构建父查询 QueryBuilder parentQueryBuilder = QueryBuilders.boolQuery().must(nestedQueryBuilder); searchSourceBuilder.query(parentQueryBuilder); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); ``` 在上述代码中,我们首先使用 nestedQuery 方法构建了一个查询,然后将这个子查询作为父查询一个 must 条件。这样做的效果是,查询会在 nested field 中查找字段 your_text_field 包含 "your search phrase" 的文档,并将符合条件的文档返回。 需要注意的是,使用子查询需要先在索引中定义 nested 类型的字段。在创建索引时,可以使用 mapping API 来指定字段类型为 nested。另外,如果您需要在子查询中查找多个值,只需将 matchPhraseQuery 方法中的搜索短语替换为数组即可。 希望这个示例代码可以帮助您实现您的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值