fastjson 修改多层嵌套的Json数据


一、需求

操作多层嵌套的Json数据,修改key值或删除key,然后更新原始Json数据

二、期望效果

删除原始Json中query-->bool-->should-->match_phrase-->recallChar2-->zero_terms_query字段(key),然后更新原始Json。

Json数据格式如下:
原始Json

{
	"query": {
		"bool": {
			"filter": [
				{
					"term": {
						"label.keyword": {
							"boost": 1.0,
							"value": "字符串"
						}
					}
				}
			],
			"adjust_pure_negative": true,
			"should": [
				{
					"match": {
						"recallChar2": {
							"auto_generate_synonyms_phrase_query": true,
							"query": "测试",
							"zero_terms_query": "NONE",
							"fuzzy_transpositions": true,
							"boost": 1.0,
							"prefix_length": 0,
							"operator": "OR",
							"lenient": false,
							"max_expansions": 50
						}
					}
				},
				{
					"match_phrase": {
						"recallChar2": {
							"query": "测试",
							"zero_terms_query": "NONE",
							"boost": 1.0,
							"slop": 0
						}
					}
				}
			],
			"boost": 1.0
		}
	}
}

新Json

{
	"query": {
		"bool": {
			"filter": [
				{
					"term": {
						"label.keyword": {
							"boost": 1.0,
							"value": "字符串"
						}
					}
				}
			],
			"adjust_pure_negative": true,
			"should": [
				{
					"match": {
						"recallChar2": {
							"auto_generate_synonyms_phrase_query": true,
							"query": "测试",
							"zero_terms_query": "NONE",
							"fuzzy_transpositions": true,
							"boost": 1.0,
							"prefix_length": 0,
							"operator": "OR",
							"lenient": false,
							"max_expansions": 50
						}
					}
				},
				{
					"match_phrase": {
						"recallChar2": {
							"query": "测试",
							"boost": 1.0,
							"slop": 0
						}
					}
				}
			],
			"boost": 1.0
		}
	}
}

三、实现

//入参esSearchRequestParam:原始Json	返参:新Json
private JSONObject updateEsRequestParam(JSONObject esSearchRequestParam) {
        Object bool = JSONObject.parseObject(esSearchRequestParam.get("query").toString()).get("bool");
        Object should = JSONObject.parseObject(bool.toString()).get("should");
        JSONArray shouldArray = JSONObject.parseArray(should.toString());
        for (int i = 0; i < shouldArray.size(); i++) {
            JSONObject jsonObject = shouldArray.getJSONObject(i);
            if (jsonObject.containsKey("match_phrase")){
                Object matchPhrase = jsonObject.get("match_phrase");
                Object recallChar2 = JSONObject.parseObject(matchPhrase.toString()).get("recallChar2");
                JSONObject recallChar2New = JSONObject.parseObject(recallChar2.toString());
//                    jsonObject1.fluentPut("zero_terms_query","测试");
                recallChar2New.remove("zero_terms_query");
                Object matchPhraseNew = JSONObject.parseObject(matchPhrase.toString()).fluentPut("recallChar2", recallChar2New);
                jsonObject.fluentPut("match_phrase", matchPhraseNew);
            }
        }
        JSONObject boolNew = JSONObject.parseObject(bool.toString()).fluentPut("should", shouldArray);
        JSONObject queryNew = JSONObject.parseObject(esSearchRequestParam.get("query").toString()).fluentPut("bool", boolNew);
        return esSearchRequestParam.fluentPut("query", queryNew);
    }

四、总结

遇到这类问题先分析Json的数据格式

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值