`from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
client = Elasticsearch()
s = Search(using=client)
s = s.using(client)
s = Search().using(client).query("match", title="python")
response = s.execute()
for hit in s:
print(hit.title)
response = s.delete()`
after executing this code i have got the error
AttributeError: 'Search' object has no attribute 'delete'
There are some errors in your code. First you're repeating yourself with all the using client, therefore I would suggest to you to use configure connections method for that so you don't always specify the client on each search object. Regardless, the first attempt should work. Then you're trying to delete a response which is not possible, you have to call a delete request on the query like you would do with the rest APIs of elasticsearch. After that you're iterating s ichi is the search object and not the response. I have adjusted your code for different purposes since I don't know what you were trying to do and I modified the query in a more dsl way.
from elasticsearch_dsl import Search
from elasticsearch_dsl.connections import connections
from elasticsearch_dsl.query import Match
conf = {
'default': {
'hosts': [
{
'host': 'localhost',
'port': 9200
}
]
}
}
connections.configure(**conf)
# elasticsearch dsl python falls back to the default connection by default if you don't specify it in the search object
# simple search with iteration over the results
s = Search(index='your index name').query(Match(title='Python'))
r = s.execute()
# check the query
print(s.to_dict())
# iterate over results
for hit in r.hits:
print(hit.to_dict())
# delete by query
s = Search(index='your index name').query(Match(title='Python'))
s.delete()
@roshnikasliwal can you also specify which version of elasticsearch-dsl you are using? Search.delete() was added in 5.2. Thank you
@HonzaKral yes it is versioning problem. Thank You
@WisdomPill i tried your solution it creates the document.
{u'_type': u'delete_by_query', u'created': True, u'_shards':...}
Closing this issue as it was just versions. Thanks!
Most helpful comment
There are some errors in your code. First you're repeating yourself with all the using client, therefore I would suggest to you to use configure connections method for that so you don't always specify the client on each search object. Regardless, the first attempt should work. Then you're trying to delete a response which is not possible, you have to call a delete request on the query like you would do with the rest APIs of elasticsearch. After that you're iterating s ichi is the search object and not the response. I have adjusted your code for different purposes since I don't know what you were trying to do and I modified the query in a more dsl way.