简体   繁体   中英

Python api to delete -- elasticsearch

I want to delete a particular '_id' in elasticsearch but its not working using elasticsearch python api client.

doc = {'system_caused': 'office', 'division': 'National', 'addt_notes': '', 'pg': '100', 'date': '2016/02/15 08:56',
               'duration': '15 minutes', 'outage_caused': 'Scheduled', 'ticket_num': '1234', 'ticket_type': 'JIRA', 'error_count': '1,000 - 5,000'}
self.es = Elasticsearch(['localhost'], verify_certs=True)

result = self.es.index(index='tickets', doc_type='tickets', body=doc)


doc = {'_id': result['_id']}
result = self.es.delete(
        index='tickets', doc_type='tickets', **doc)

The inserts are working fine but the delete is failing.

Here is the error I get -

TypeError: delete() got an unexpected keyword argument '_id'

According to the source code the input parameters for delete include id , but not _id . Check out the code here .

When you use the ** operator in front of a dict, it expands to keyword arguments. So we can rewrite it as:

result = self.es.delete(
    index='tickets', doc_type='tickets',
    _id=result['_id'])

Thus you are passing _id as one of the keyword parameters. As @Forge said, I assume you meant to use id in the delete call.

from elasticsearch import Elasticsearch
from elasticsearch import helpers
from elasticsearch_dsl import connections
import pandas as pd

# initialize list of lists
data = [['tom', 10, 'NY'], ['nick', 15, 'NY'], ['juli', 14, 'NY'], ['akshay', 30, 'IND'], ['Amit', 14, 'IND']]

# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Name', 'Age', 'Country'])

from elasticsearch import Elasticsearch
from elasticsearch import helpers

##connection to elastic search cluster

es_client = connections.create_connection(hosts=['http://localhost:9200/'])
def doc_generator(df):
    df_iter = df.iterrows()
    for index, document in df_iter:
        yield {
                "_index": 'age_sample',
                "_type": "_doc",
                "_source": document,
            }

helpers.bulk(es_client, doc_generator(df))

from elasticsearch_dsl import Search
s = Search(index='age_sample').query("match", _id='V5A3Y34Bq-Uo6Q7qyfBJ')
response = s.delete()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM