Rohit Kumar
Rohit Kumar

Reputation: 719

Error with elastichsearch querying through python-requests

I can get the desired result with following curl request . but when using the python-requests it's not working. i haven't worked with elasticsearch before so it may be a basic question.

curl -XGET 'http://something.someone.com:9200/logstash-2018.02.09/_search?pretty'  -d'{  "query": {
        "filtered": {
          "query": {
            "query_string": {
              "query": "host: \"pod1-cph3.someone.com\"  AND facility: user AND severity: info AND ident:web20 AND message: Write",
              "analyze_wildcard": true                                                                                                     
            }                         
          }
        }
      }
    }'

python-code snippet:-

headers ={'Content-Type': 'application/json'}
elasticsearch_url="http://something.someone.com:9200/logstash-{}/".format(current_date)
data_payload= {
    "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "host: \"pod1-cph3.someone.com\"  AND facility: user AND severity: info AND ident:web20 AND message: Write",
          "analyze_wildcard": "true"
        }
      }
    }
  }
}

resp=requests.get(elasticsearch_url,data=json.dumps(data_payload),headers=headers)
print resp.content

note that it's not my desire output. output is rather long. i have pasted some part of it:-

{"logstash-2018.02.09":{"aliases":{},"mappings":{"fluentd":{"properties":{"@timestamp":{"type":"date","format":"strict_date_optional_time||epoch_millis"},"CODE_FILE":{"type":"string"},"CODE_FUNC":{"type":"string"},"CODE_FUNCTION":{"type":"string"},"CODE_LINE":{"type":"string"},"CONFIG_FILE":{"type":"string"},"CONFIG_LINE":{"type":"str

Upvotes: 0

Views: 249

Answers (2)

Roshni Kasliwal
Roshni Kasliwal

Reputation: 229

try this,

resp=requests.get(elasticsearch_url,data=json.dumps(data_payload),headers=headers)

data=resp.json()

Upvotes: 3

Rohit Kumar
Rohit Kumar

Reputation: 719

i missed the _search in the uri. Thanks for the responses though.

Upvotes: 0

Related Questions