Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Elasticsearch query runner error: 'str' object has no attribute 'pop' error when parsing query #6941

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions redash/query_runner/elasticsearch2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
from typing import Optional, Tuple

Expand Down Expand Up @@ -64,6 +65,7 @@ def run_query(self, query, user):
return data, error

def _build_query(self, query: str) -> Tuple[dict, str, Optional[list]]:
query = json.loads(query)
index_name = query.pop("index", "")
result_fields = query.pop("result_fields", None)
url = "/{}/_search".format(index_name)
Expand Down
13 changes: 12 additions & 1 deletion tests/query_runner/test_elasticsearch2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from unittest import TestCase
from unittest import TestCase, mock

from redash.query_runner.elasticsearch2 import (
ElasticSearch2,
Expand Down Expand Up @@ -137,3 +137,14 @@ def test_parse_results(self):
],
}
self.assertDictEqual(XPackSQLElasticSearch._parse_results(None, response), expected)


class TestElasticSearch2(TestCase):
@mock.patch("redash.query_runner.elasticsearch2.ElasticSearch2.__init__", return_value=None)
def test_build_query(self, mock_init):
query_runner = ElasticSearch2()
query_str = '{"index": "test_index", "result_fields": ["field1", "field2"]}'
query_dict, url, result_fields = query_runner._build_query(query_str)
self.assertEqual(query_dict, {})
self.assertEqual(url, "/test_index/_search")
self.assertEqual(result_fields, ["field1", "field2"])
Loading