我正在尝试创建一个ETL,它可以从mongo中提取数据,处理数据并加载到elastic中。我将进行日常加载,所以我想到用当前日期命名我的索引。这将有助于我稍后处理第一个索引。我使用了elasticsearch dsl指南:https://elasticsearch-dsl.readthedocs.io/en/latest/persistence.html我遇到的问题来自于我使用类的一点经验。我不知道如何从类中重置索引名称。下面是我的类代码(custom_indexs.py):
from elasticsearch_dsl import Document, Date, Integer, Keyword, Text
from elasticsearch_dsl.connections import connections
from elasticsearch_dsl import Search
import datetime
class News(Document):
title = Text(analyzer='standard', fields={'raw': Keyword()})
manual_tagging = Keyword()
class Index:
name = 'processed_news_'+datetime.datetime.now().strftime("%Y%m%d")
def save(self, ** kwargs):
return super(News, self).save(** kwargs)
def is_published(self):
return datetime.now() >= self.processed
这是我创建该类示例的代码部分:
from custom_indices import News
import elasticsearch
import elasticsearch_dsl
from elasticsearch_dsl.connections import connections
import pandas as pd
import datetime
connections.create_connection(hosts=['localhost'])
News.init()
for index, doc in df.iterrows():
new_insert = News(meta={'id': doc.url_hashed},
title = doc.title,
manual_tagging = doc.customTags,
)
new_insert.save()
每次我调用“News”类时,我都希望有一个新的名称。然而,即使我再次加载类,名称也不会改变(from custom_indexs import News)。我知道这只是我在测试时遇到的一个问题,但我想知道如何强制“重置”。实际上,我最初想在类外部更改名称,就在循环之前使用以下行:
News.Index.name = "NEW_NAME"
然而,这并不起作用。我仍然看到类上定义的名称。有人能帮忙吗?非常感谢!PS:这一定只是一个面向对象编程的问题。为我对这个问题的无知道歉。
3条答案
按热度按时间pdsfdshx1#
也许你可以利用
Document.init()
接受一个index
关键字参数的事实,如果你想自动设置索引名,你可以在News
类中实现init()
,并在你的实现中调用super().init(...)
。一个简单的例子(python 3.x):
vybvopom2#
可以在调用**保存()**时覆盖索引。
vdgimpew3#
示例如下。