【问题标题】:AttributeError: 'Elasticsearch' object has no attribute 'options'AttributeError:“Elasticsearch”对象没有属性“选项”
【发布时间】:2022-06-23 12:53:01
【问题描述】:

我正在尝试在 python 中使用 elasticsearch 构建模型,但出现此错误,我能够执行一次,但由于此错误,现在我无法再次运行它

import pandas as pd

df = pd.read_csv("salaries.csv")
df.head()

inputs = df.drop('salary_more_then_100k',axis='columns')

target = df['salary_more_then_100k']

from sklearn.preprocessing import LabelEncoder
le_company = LabelEncoder()
le_job = LabelEncoder()
le_degree = LabelEncoder()

inputs['company_n'] = le_company.fit_transform(inputs['company'])
inputs['job_n'] = le_job.fit_transform(inputs['job'])
inputs['degree_n'] = le_degree.fit_transform(inputs['degree'])

inputs

inputs_n = inputs.drop(['company','job','degree'],axis='columns')

inputs_n

target

from sklearn import tree
model = tree.DecisionTreeClassifier()

model.fit(inputs_n, target)

from eland.ml import MLModel
from elasticsearch import Elasticsearch

es = Elasticsearch(hosts='http://localhost:9200')

es_model = MLModel.import_model(es_client=es,
                                model=model,
                                model_id='salaries-model',
                                feature_names=list(df.columns),
                                es_if_exists='replace'
)

当我运行 es_model 行时,出现此错误 -> AttributeError: 'Elasticsearch' object has no attribute 'options'

我做错了什么,谁能告诉我

【问题讨论】:

    标签: python elasticsearch


    【解决方案1】:

    opensearch 面临类似问题。

    参考链接-https://pydole.tistory.com/entry/Python-Insert-bulk-from-pandas-dataframe-to-elasticsearch

    代码-

    import pandas as pd
    from elasticsearch import Elasticsearch
    from elasticsearch.helpers import bulk
    from opensearchpy import OpenSearch
    
    # Datetime, String, Interger Example Dataframe 
    
    listDate = ['2020-01-01 00:00:00','2020-01-01 00:01:00','2020-01-01 00:02:00', '2020-01-01 00:03:00']
    listStrings = ['a','b','c','d']
    listInterger = [1, 2, 3, 4 ]
    
    df = pd.DataFrame([ x for x in zip(listDate,listStrings,listInterger)], columns=['date','string', 'interger'])
    df['date'] = pd.to_datetime(df['date'])
    
    host = 'localhost'
    port = 9200
    auth = ('admin', 'admin') # For testing only. Don't store credentials in code.
    # ca_certs_path = '/full/path/to/root-ca.pem' # Provide a CA bundle if you use intermediate CAs with your root CA.
    
    # Optional client certificates if you don't want to use HTTP basic authentication.
    # client_cert_path = '/full/path/to/client.pem'
    # client_key_path = '/full/path/to/client-key.pem'
    
    # Create the client with SSL/TLS enabled, but hostname verification disabled.
    
    client = OpenSearch(
                hosts = [{'host': host, 'port': port}],
                http_compress = True, # enables gzip compression for request bodies
                http_auth = auth,
            # client_cert = client_cert_path,
            # client_key = client_key_path,
                use_ssl = True,
                verify_certs = False,
                ssl_assert_hostname = False,
                ssl_show_warn = False,
                # ca_certs = ca_certs_path
                 )
    
    # Create an index with non-default settings.
    index_name = 'bulk'
    index_body = {
      'settings': {
        'index': {
          'number_of_shards': 4
        }
      }
    }
    response = client.indices.create(index_name, body=index_body)
    df = pd.DataFrame(data = {'date' : df['date'],
                              'strings': df['string'],
                              'interger' : df['interger']})
     
    documents = df.to_dict(orient='records')
    print(documents)
    bulk(client, documents, index='bulk',doc_type='foo', raise_on_error=True)
    

    错误-

    (devenv) E:\env>python bulk.py
    [{'date': Timestamp('2020-01-01 00:00:00'), 'strings': 'a', 'interger': 1}, {'date': Timestamp('2020-01-01 00:01:00'), 'strings': 'b', 'interger': 2}, {'date': Timestamp('2020-01-01 00:02:00'), 'strings': 'c', 'interger': 3}, {'date': Timestamp('2020-01-01 00:03:00'), 'strings': 'd', 'interger': 4}]Traceback (most recent call last):
      File "E:\env\bulk.py", line 55, in <module>
        bulk(client, documents, index='bulk',doc_type='foo', raise_on_error=True)
      File "E:\env\devenv\lib\site-packages\elasticsearch\helpers\actions.py", line 524, in bulk
        for ok, item in streaming_bulk(
      File "E:\env\devenv\lib\site-packages\elasticsearch\helpers\actions.py", line 410, in streaming_bulk
        client = client.options()
    AttributeError: 'OpenSearch' object has no attribute 'options'
    
    (devenv) E:\env>
    (devenv) E:\env>python bulk.py
    
    (devenv) E:\env>python bulk.py
    [{'date': Timestamp('2020-01-01 00:00:00'), 'strings': 'a', 'interger': 1}, {'date': Timestamp('2020-01-01 00:01:00'), 'strings': 'b', 'interger': 2}, {'date': Timestamp('2020-01-01 00:02:00'), 'strings': 'c', 'interger': 3}, {'date': Timestamp('2020-01-01 00:03:00'), 'strings': 'd', 'interger': 4}]Traceback (most recent call last):
      File "E:\env\bulk.py", line 55, in <module>
        bulk(client, documents, index='bulk',doc_type='foo', raise_on_error=True)
      File "E:\env\devenv\lib\site-packages\elasticsearch\helpers\actions.py", line 524, in bulk
        for ok, item in streaming_bulk(
      File "E:\env\devenv\lib\site-packages\elasticsearch\helpers\actions.py", line 410, in streaming_bulk
        client = client.options()
    AttributeError: 'OpenSearch' object has no attribute 'options'
    

    【讨论】:

      猜你喜欢
      • 2012-08-11
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 2012-12-01
      • 2021-04-19
      • 2021-11-22
      • 1970-01-01
      相关资源
      最近更新 更多