Elasticsearch-dsl-py: Issue with the scan helper

Created on 12 Jul 2017  路  2Comments  路  Source: elastic/elasticsearch-dsl-py

Hello,

When I try to use the method elasticsearch.helpers.scan I get an exception:

Traceback (most recent call last):
  File "test.py", line 11, in search
    doc_type='entity'):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/elasticsearch/helpers/__init__.py", line 284, in scan
    query = query.copy() if query else {}
AttributeError: 'str' object has no attribute 'copy'

Here a minimum piece of code that can reproduce the problem:

import elasticsearch
from elasticsearch import helpers

es = elasticsearch.Elasticsearch([{'host': 'localhost', 'port': 9200}])
body = u"""{{
    "query": {{
        "match_all": {{}}
    }}
}}"""
res_query = elasticsearch.helpers.scan(client=es, query=body, index="test",
                                                                   doc_type='entity')
for doc in res_query:
    print (doc)

My version of Python is 2.7.13 and the package elasticsearch is 5.4.0 and my Elasticsearch index is 5.3.1

Thanks in advance for any help.

Most helpful comment

As the query just pass in the dict, no need to serialize it yourself into a string. The helper might need to add something to it, that is why it's trying to make a copy. Body should just be:

body = {
    "query": {
        "match_all": {}
    }
}

(though it you do not wish to specify query and want to unload all the documents you don't have to specify it at all)

Hope this helps.

All 2 comments

As the query just pass in the dict, no need to serialize it yourself into a string. The helper might need to add something to it, that is why it's trying to make a copy. Body should just be:

body = {
    "query": {
        "match_all": {}
    }
}

(though it you do not wish to specify query and want to unload all the documents you don't have to specify it at all)

Hope this helps.

Getting the same error.Here is the query and request.
self.body ={
"query": {
"bool": {
"must": [ {
"query_string" : {
"query": query,
"analyze_wildcard": "true"
}
}],
"filter": [{
"range": {
"@timestamp": {
"gte": starttime,
"lte": endtime,
"format": "epoch_millis"
}
}
}]
}
}
}
self.body = json.dumps(self.body)
elasticsearch.helpers.scan(self.es,query=self.body,index=self.index)

What am i doing wrong here ?

Was this page helpful?
0 / 5 - 0 ratings