LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2017, 10:32 PM   #1
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 281

Rep: Reputation: 30
Python Requests Query Elasticsearch


I have the following code but I seem to be getting an error related to lists? I'm not totally sure why. I eventually want the return of the query into a variable:

Code:
import requests

headers = {
    'apikey': 'myapikey'
          }

data = {
    '{ "query" : {"bool": { "must": [{ "match": { "message": "gmail.com yahoo.com" } },{ "range" : { "@timestamp": { "gte" : "now-7d"} } } ]}}}'
       }

r = requests.get('https://myelasticsearch.com/pastebin-*/_search', headers=headers, data=data)

Error:

Code:
Traceback (most recent call last):
  File "elastic-test.py", line 13, in <module>
    r = requests.get('https://myelasticsearch.com/pastebin-*/_search', headers=headers, data=data)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 440, in send
    timeout=timeout
  File "/usr/local/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 357, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python2.7/httplib.py", line 1057, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.7/httplib.py", line 1097, in _send_request
    self.endheaders(body)
  File "/usr/lib/python2.7/httplib.py", line 1053, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 901, in _send_output
    self.send(message_body)
  File "/usr/lib/python2.7/httplib.py", line 873, in send
    self.sock.sendall(data)
  File "/usr/lib/python2.7/ssl.py", line 743, in sendall
    v = self.send(data[count:])
TypeError: 'set' object has no attribute '__getitem__'
 
Old 11-22-2017, 11:53 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,985

Rep: Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569
So your "data" variable is a string in squiggly brackets? In Python 3, that's a set containing a string.

You wanted it to be a dictionary, right?

Code:
data = {'query': 'contents of the query'}

Last edited by dugan; 11-22-2017 at 12:03 PM.
 
Old 11-22-2017, 12:13 PM   #3
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 281

Original Poster
Rep: Reputation: 30
Sorry, I should have specified, this is Python 2.7.
 
Old 11-22-2017, 01:59 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,985

Rep: Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569Reputation: 4569
Quote:
Originally Posted by snowman81 View Post
Sorry, I should have specified, this is Python 2.7.
The fix I gave you still applies.
 
Old 11-23-2017, 02:40 AM   #5
camp0
Member
 
Registered: Dec 2016
Location: Dublin
Distribution: Fedora
Posts: 70

Rep: Reputation: 3
I see two problems on the variable data:

- The first is that you are creating a dict with no value
Code:
data = {
    '{ "query" : {"bool": { "must": [{ "match": { "message": "gmail.com yahoo.com" } },{ "range" : { "@timestamp": { "gte" : "now-7d"} } } ]}}}'
       }
Code:
data = { 'mykey' }
- The second is that the key is not hashable in terms that you are using a dictionary as key.

The correct solution (I think) should be
Code:
data = { 'query' :  {"bool": { "must"......
 
Old 11-23-2017, 03:19 PM   #6
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 281

Original Poster
Rep: Reputation: 30
Thanks guys! That seemed to work!
 
  


Reply

Tags
elasticsearch, python, requests


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do install the requests module -python Glenn D. Linux - Software 1 05-28-2017 05:34 AM
IPTables block multiple requests to port 80 query welshdemon Linux - Security 3 02-16-2014 05:18 AM
Python dict query and assign values problems ArthurHuang Programming 2 03-13-2012 04:18 AM
[SOLVED] MySQLdb Python repeating the same query returns the same data tadeas Programming 3 12-05-2011 10:29 AM
Python MySQL: interactive query input threaderslash Programming 4 09-22-2009 06:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration