Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

elasticsearch - Python generate json: bool malformed query, expected END_OBJECT but found FIELD_NAME

i have a probleme with elastic search when i request it with the following json, i have this error: [bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME] i've tried to look on many site but none of them give me a response

{

    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "group_issuer_name":"bnp"
                    }
                },
                {
                    "match":{
                        "asset_country":"France"
                    }
                }
            ]
        },
        "aggs":{
            "by_ptf_name":{
                "terms":{
                    "field":"ptf_name.keyword"
                },
                "aggs":{
                    "by_ptf_id":{
                        "terms":{
                            "field":"ptf_id.keyword"
                        },
                        "aggs":{
                            "sum_of_asset_exposure":{
                                "sum":{
                                    "field":"asset_exposure"
                                }
                            },
                            "min_of_ptf_total_asset":{
                                "min":{
                                    "field":"ptf_total_asset"
                                }
                            }
                        }
                    }
                }
            }
        }
    }

}
question from:https://stackoverflow.com/questions/65848790/python-generate-json-bool-malformed-query-expected-end-object-but-found-field

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are missing }. The query part must be closed and then the aggregation part should start.

The structure should be

{
  "query": {},
  "aggregation": {}
}

Modify your query as -

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "group_issuer_name": "bnp"
          }
        },
        {
          "match": {
            "asset_country": "France"
          }
        }
      ]
    }
  },                                    // note this
  "aggs": {
    "by_ptf_name": {
      "terms": {
        "field": "ptf_name.keyword"
      },
      "aggs": {
        "by_ptf_id": {
          "terms": {
            "field": "ptf_id.keyword"
          },
          "aggs": {
            "sum_of_asset_exposure": {
              "sum": {
                "field": "asset_exposure"
              }
            },
            "min_of_ptf_total_asset": {
              "min": {
                "field": "ptf_total_asset"
              }
            }
          }
        }
      }
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...