0
votes

I have the below query in which I want to get the search term "Schwarz" either in the name field or the message field. The language must be Austrian and the status type should be equal to the provided list. I'm getting the following exception and I can't figure out why:

QueryParsingException[[my_test_index] [_na] query malformed, no field after start_object]; }]",

{
        "query": {
            "filtered": {
                "query": {
                    "bool": {
                        "must": [
                            {
                                "bool": {
                                    "should": [
                                        {
                                            "term": {
                                                "name": "Schwarz"
                                            }
                                        },
                                        {
                                            "term": {
                                                "message": "Schwarz"
                                            }
                                        }
                                    ],
                                    "minimum_should_match": 1
                                }
                            },
                            {
                                "terms": {
                                    "status_type": [
                                        "1",
                                        "2",
                                        "3",
                                        "4",
                                        "5",
                                        "6",
                                        "7"
                                    ]
                                }
                            },
                            {
                                "term": {
                                    "language": "Austrian"
                                }
                            }
                        ]
                    }
                }
            }
        },
        "sort": [
            {
                "total": {
                    "order": "desc"
                }
            }
        ]
    }

Here is the query without the filter that still works:

{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "standard_analyzed_name": "Schwarz"
              }
            },
            {
              "match": {
                "standard_analyzed_message": "Schwarz"
              }
            }
          ],
          "must": [
            {
              "terms": {
                "buzzsumo_status_type": [
                  "1",
                  "2",
                  "3",
                  "4",
                  "5",
                  "6",
                  "7"
                ]
              }
            },
            {
              "term": {
                "language": "Austrian"
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "total_interactions": {
        "order": "desc"
      }
    }
  ]
}
1

1 Answers

1
votes

In a filtered query you MUST have a filter part, which is not present here. I would suggest rewriting it like this, i.e. move the terms and term parts into the filter part:

{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "should": [
            {
              "term": {
                "name": "Schwarz"
              }
            },
            {
              "term": {
                "message": "Schwarz"
              }
            }
          ],
          "minimum_should_match": 1
        }
      },
      "filter": {
        "bool": {
          "must": [
            {
              "terms": {
                "status_type": [
                  "1",
                  "2",
                  "3",
                  "4",
                  "5",
                  "6",
                  "7"
                ]
              }
            },
            {
              "term": {
                "language": "Austrian"
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "total": {
        "order": "desc"
      }
    }
  ]
}

An alternative is to not use the filtered query and simply write it like this:

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "term": {
                  "name": "Schwarz"
                }
              },
              {
                "term": {
                  "message": "Schwarz"
                }
              }
            ],
            "minimum_should_match": 1
          }
        },
        {
          "terms": {
            "status_type": [
              "1",
              "2",
              "3",
              "4",
              "5",
              "6",
              "7"
            ]
          }
        },
        {
          "term": {
            "language": "Austrian"
          }
        }
      ]
    }
  },
  "sort": [
    {
      "total": {
        "order": "desc"
      }
    }
  ]
}