本文整理汇总了Python中elasticsearch_dsl.Index.exists方法的典型用法代码示例。如果您正苦于以下问题:Python Index.exists方法的具体用法?Python Index.exists怎么用?Python Index.exists使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类elasticsearch_dsl.Index
的用法示例。
在下文中一共展示了Index.exists方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_create_index_manually
# 需要导入模块: from elasticsearch_dsl import Index [as 别名]
# 或者: from elasticsearch_dsl.Index import exists [as 别名]
def test_create_index_manually(self):
out = io.StringIO()
index_name = 'test_manually_created_index'
call_command('create_index', index_name, stdout=out)
self.assertIn("Created search index '{}'".format(index_name), out.getvalue())
index = Index(index_name)
self.assertTrue(index.exists())
index.delete()
self.assertFalse(index.exists())
示例2: test_create_index_usings_settings
# 需要导入模块: from elasticsearch_dsl import Index [as 别名]
# 或者: from elasticsearch_dsl.Index import exists [as 别名]
def test_create_index_usings_settings(self):
out = io.StringIO()
call_command('create_index', stdout=out)
self.assertIn("Creating search indices from settings", out.getvalue())
self.assertIn("Created search index '{}'".format(self.settings['default']['index']), out.getvalue())
index = Index(self.settings['default']['index'])
self.assertTrue(index.exists())
index.delete()
self.assertFalse(index.exists())
示例3: BaseSearchTestCase
# 需要导入模块: from elasticsearch_dsl import Index [as 别名]
# 或者: from elasticsearch_dsl.Index import exists [as 别名]
class BaseSearchTestCase(TestCase):
def setUp(self):
from django.conf import settings
SEARCH = getattr(settings, 'SEARCH')
connections.create_connection('testing', **SEARCH['default']['connections'])
self.index = Index(SEARCH['default']['index'], using='testing')
# This is needed for test_documents, but has side effects in all running tests
doctypes_list = (
value for name, value
in inspect.getmembers(documents)
if not name.startswith('_') and
inspect.isclass(value) and
issubclass(value, DocType) and
name != DocType.__name__
)
for doctype in doctypes_list:
# Remove assigned index
doctype._doc_type.index = None
# Associate docs with test index
self.index.doc_type(doctype)
if self.index.exists():
self.index.delete(ignore=404)
self.index.create()
self.search = Search(index=SEARCH['default']['index'])
def tearDown(self):
self.index.delete()
queue = django_rq.get_queue()
queue.empty()
示例4: initialize_index
# 需要导入模块: from elasticsearch_dsl import Index [as 别名]
# 或者: from elasticsearch_dsl.Index import exists [as 别名]
def initialize_index(self, delete_if_exists=False):
"""
Initialize index with mapping in ElasticSearch
:param delete_if_exists: delete index, if exists
:return: None
"""
def update_index_settings():
"""
Function updates settings for slovenian lemmatization of words.
As far as we know, elasticsearch-dsl library does not support
custom filter settings.
:return: None
"""
analysis_settings = {
"analysis": {
"filter": {
"lemmagen_filter_sl": {
"type": "lemmagen",
"lexicon": "sl"
}
},
"analyzer": {
"lemmagen_sl": {
"type": "custom",
"tokenizer": "uax_url_email",
"filter": [
"lemmagen_filter_sl",
"lowercase"
]
}
}
}
}
self.client.cluster.health(index=self.index_name,
wait_for_status='green',
request_timeout=2)
self.client.indices.close(index=self.index_name)
self.client.indices.put_settings(json.dumps(analysis_settings),
index=self.index_name)
self.client.indices.open(index=self.index_name)
index = Index(self.index_name, using=self.client)
if delete_if_exists and index.exists():
index.delete()
index.settings(
# use higher number in production
number_of_replicas=0
)
# register models
index.doc_type(Document)
index.create()
update_index_settings() # set lemmanizer
示例5: es_index_mapping
# 需要导入模块: from elasticsearch_dsl import Index [as 别名]
# 或者: from elasticsearch_dsl.Index import exists [as 别名]
def es_index_mapping(index_name, doc_type, force=False):
# 创建索引
index = Index(index_name)
index.doc_type(doc_type)
if not index.exists():
index.create()
else:
if force:
index.upgrade()
示例6: create_search_index
# 需要导入模块: from elasticsearch_dsl import Index [as 别名]
# 或者: from elasticsearch_dsl.Index import exists [as 别名]
def create_search_index(index_name, doc_types=None, connection='default', delete_if_exists=False):
index = Index(index_name, using=connection)
if delete_if_exists:
index.delete(ignore=404)
if doc_types:
for dt in doc_types:
if isinstance(dt, str):
dt = get_document_class(dt)
index.doc_type(dt)
if not index.exists():
index.create()
return index
示例7: run
# 需要导入模块: from elasticsearch_dsl import Index [as 别名]
# 或者: from elasticsearch_dsl.Index import exists [as 别名]
def run(self, *args, **options):
self.target_name = self.target_name or self.index_name
index = Index(self.target_name)
if not index.exists():
self.print_error(
u"Index '{0}' does not exist."
.format(self.target_name)
)
sys.exit(1)
self.print_info(u"Indexing objects on '{0}'.".format(self.target_name))
models = self.trampoline_config.get_index_models(self.index_name)
for model in models:
self.print_info(u"Processing model: '{0}'.".format(model.__name__))
queryset = model.get_indexable_queryset()
for obj in queryset:
if obj.is_indexable():
if not self.dry_run:
result = obj.es_index(
async=False,
index_name=self.target_name
)
if result.result is False:
status = self.STATUS_FAILED
else:
status = self.STATUS_INDEXED
else:
status = self.STATUS_INDEXED
else:
status = self.STATUS_SKIPPED
self.print_obj_status(obj, model.__name__, status)
self.print_success("Indexation completed.")
示例8: __init__
# 需要导入模块: from elasticsearch_dsl import Index [as 别名]
# 或者: from elasticsearch_dsl.Index import exists [as 别名]
class ElasticSearchIndex:
def __init__(self, name='qb', similarity='default', bm25_b=None, bm25_k1=None):
self.name = name
self.ix = Index(self.name)
self.answer_doc = create_doctype(self.name, similarity)
if bm25_b is None:
bm25_b = .75
if bm25_k1 is None:
bm25_k1 = 1.2
self.bm25_b = bm25_b
self.bm25_k1 = bm25_k1
def delete(self):
try:
self.ix.delete()
except elasticsearch.exceptions.NotFoundError:
log.info('Could not delete non-existent index.')
def exists(self):
return self.ix.exists()
def init(self):
self.ix.create()
self.ix.close()
self.ix.put_settings(body={'similarity': {
'qb_bm25': {'type': 'BM25', 'b': self.bm25_b, 'k1': self.bm25_k1}}
})
self.ix.open()
self.answer_doc.init(index=self.name)
def build_large_docs(self, documents: Dict[str, str], use_wiki=True, use_qb=True, rebuild_index=False):
if rebuild_index or bool(int(os.getenv('QB_REBUILD_INDEX', 0))):
log.info(f'Deleting index: {self.name}')
self.delete()
if self.exists():
log.info(f'Index {self.name} exists')
else:
log.info(f'Index {self.name} does not exist')
self.init()
wiki_lookup = Wikipedia()
log.info('Indexing questions and corresponding wikipedia pages as large docs...')
for page in tqdm.tqdm(documents):
if use_wiki and page in wiki_lookup:
wiki_content = wiki_lookup[page].text
else:
wiki_content = ''
if use_qb:
qb_content = documents[page]
else:
qb_content = ''
answer = self.answer_doc(
page=page,
wiki_content=wiki_content, qb_content=qb_content
)
answer.save(index=self.name)
def build_many_docs(self, pages, documents, use_wiki=True, use_qb=True, rebuild_index=False):
if rebuild_index or bool(int(os.getenv('QB_REBUILD_INDEX', 0))):
log.info(f'Deleting index: {self.name}')
self.delete()
if self.exists():
log.info(f'Index {self.name} exists')
else:
log.info(f'Index {self.name} does not exist')
self.init()
log.info('Indexing questions and corresponding pages as many docs...')
if use_qb:
log.info('Indexing questions...')
for page, doc in tqdm.tqdm(documents):
self.answer_doc(page=page, qb_content=doc).save()
if use_wiki:
log.info('Indexing wikipedia...')
wiki_lookup = Wikipedia()
for page in tqdm.tqdm(pages):
if page in wiki_lookup:
content = word_tokenize(wiki_lookup[page].text)
for i in range(0, len(content), 200):
chunked_content = content[i:i + 200]
if len(chunked_content) > 0:
self.answer_doc(page=page, wiki_content=' '.join(chunked_content)).save()
def search(self, text: str, max_n_guesses: int,
normalize_score_by_length=False,
wiki_boost=1, qb_boost=1):
if not self.exists():
raise ValueError('The index does not exist, you must create it before searching')
if wiki_boost != 1:
wiki_field = 'wiki_content^{}'.format(wiki_boost)
else:
wiki_field = 'wiki_content'
if qb_boost != 1:
qb_field = 'qb_content^{}'.format(qb_boost)
else:
#.........这里部分代码省略.........
示例9: test_create_index
# 需要导入模块: from elasticsearch_dsl import Index [as 别名]
# 或者: from elasticsearch_dsl.Index import exists [as 别名]
def test_create_index(app, admin_idx):
admin_idx.recreate_index()
index = Index(admin_idx.index)
assert (index.exists())
示例10: BurpExtender
# 需要导入模块: from elasticsearch_dsl import Index [as 别名]
# 或者: from elasticsearch_dsl.Index import exists [as 别名]
class BurpExtender(IBurpExtender, IHttpListener, IContextMenuFactory, ITab):
def registerExtenderCallbacks(self, callbacks):
self.callbacks = callbacks
self.helpers = callbacks.getHelpers()
callbacks.setExtensionName("Storing HTTP Requests/Responses into ElasticSearch")
self.callbacks.registerHttpListener(self)
self.callbacks.registerContextMenuFactory(self)
self.out = callbacks.getStdout()
self.lastTimestamp = None
self.confESHost = self.callbacks.loadExtensionSetting("elasticburp.host") or ES_host
self.confESIndex = self.callbacks.loadExtensionSetting("elasticburp.index") or ES_index
self.confBurpTools = int(self.callbacks.loadExtensionSetting("elasticburp.tools") or Burp_Tools)
saved_onlyresp = self.callbacks.loadExtensionSetting("elasticburp.onlyresp")
if saved_onlyresp == "True":
self.confBurpOnlyResp = True
elif saved_onlyresp == "False":
self.confBurpOnlyResp = False
else:
self.confBurpOnlyResp = bool(int(saved_onlyresp or Burp_onlyResponses))
self.callbacks.addSuiteTab(self)
self.applyConfig()
def applyConfig(self):
try:
print("Connecting to '%s', index '%s'" % (self.confESHost, self.confESIndex))
self.es = connections.create_connection(hosts=[self.confESHost])
self.idx = Index(self.confESIndex)
self.idx.doc_type(DocHTTPRequestResponse)
if self.idx.exists():
self.idx.open()
else:
self.idx.create()
self.callbacks.saveExtensionSetting("elasticburp.host", self.confESHost)
self.callbacks.saveExtensionSetting("elasticburp.index", self.confESIndex)
self.callbacks.saveExtensionSetting("elasticburp.tools", str(self.confBurpTools))
self.callbacks.saveExtensionSetting("elasticburp.onlyresp", str(int(self.confBurpOnlyResp)))
except Exception as e:
JOptionPane.showMessageDialog(self.panel, "<html><p style='width: 300px'>Error while initializing ElasticSearch: %s</p></html>" % (str(e)), "Error", JOptionPane.ERROR_MESSAGE)
### ITab ###
def getTabCaption(self):
return "ElasticBurp"
def applyConfigUI(self, event):
#self.idx.close()
self.confESHost = self.uiESHost.getText()
self.confESIndex = self.uiESIndex.getText()
self.confBurpTools = int((self.uiCBSuite.isSelected() and IBurpExtenderCallbacks.TOOL_SUITE) | (self.uiCBTarget.isSelected() and IBurpExtenderCallbacks.TOOL_TARGET) | (self.uiCBProxy.isSelected() and IBurpExtenderCallbacks.TOOL_PROXY) | (self.uiCBSpider.isSelected() and IBurpExtenderCallbacks.TOOL_SPIDER) | (self.uiCBScanner.isSelected() and IBurpExtenderCallbacks.TOOL_SCANNER) | (self.uiCBIntruder.isSelected() and IBurpExtenderCallbacks.TOOL_INTRUDER) | (self.uiCBRepeater.isSelected() and IBurpExtenderCallbacks.TOOL_REPEATER) | (self.uiCBSequencer.isSelected() and IBurpExtenderCallbacks.TOOL_SEQUENCER) | (self.uiCBExtender.isSelected() and IBurpExtenderCallbacks.TOOL_EXTENDER))
self.confBurpOnlyResp = self.uiCBOptRespOnly.isSelected()
self.applyConfig()
def resetConfigUI(self, event):
self.uiESHost.setText(self.confESHost)
self.uiESIndex.setText(self.confESIndex)
self.uiCBSuite.setSelected(bool(self.confBurpTools & IBurpExtenderCallbacks.TOOL_SUITE))
self.uiCBTarget.setSelected(bool(self.confBurpTools & IBurpExtenderCallbacks.TOOL_TARGET))
self.uiCBProxy.setSelected(bool(self.confBurpTools & IBurpExtenderCallbacks.TOOL_PROXY))
self.uiCBSpider.setSelected(bool(self.confBurpTools & IBurpExtenderCallbacks.TOOL_SPIDER))
self.uiCBScanner.setSelected(bool(self.confBurpTools & IBurpExtenderCallbacks.TOOL_SCANNER))
self.uiCBIntruder.setSelected(bool(self.confBurpTools & IBurpExtenderCallbacks.TOOL_INTRUDER))
self.uiCBRepeater.setSelected(bool(self.confBurpTools & IBurpExtenderCallbacks.TOOL_REPEATER))
self.uiCBSequencer.setSelected(bool(self.confBurpTools & IBurpExtenderCallbacks.TOOL_SEQUENCER))
self.uiCBExtender.setSelected(bool(self.confBurpTools & IBurpExtenderCallbacks.TOOL_EXTENDER))
self.uiCBOptRespOnly.setSelected(self.confBurpOnlyResp)
def getUiComponent(self):
self.panel = JPanel()
self.panel.setLayout(BoxLayout(self.panel, BoxLayout.PAGE_AXIS))
self.uiESHostLine = JPanel()
self.uiESHostLine.setLayout(BoxLayout(self.uiESHostLine, BoxLayout.LINE_AXIS))
self.uiESHostLine.setAlignmentX(JPanel.LEFT_ALIGNMENT)
self.uiESHostLine.add(JLabel("ElasticSearch Host: "))
self.uiESHost = JTextField(40)
self.uiESHost.setMaximumSize(self.uiESHost.getPreferredSize())
self.uiESHostLine.add(self.uiESHost)
self.panel.add(self.uiESHostLine)
self.uiESIndexLine = JPanel()
self.uiESIndexLine.setLayout(BoxLayout(self.uiESIndexLine, BoxLayout.LINE_AXIS))
self.uiESIndexLine.setAlignmentX(JPanel.LEFT_ALIGNMENT)
self.uiESIndexLine.add(JLabel("ElasticSearch Index: "))
self.uiESIndex = JTextField(40)
self.uiESIndex.setMaximumSize(self.uiESIndex.getPreferredSize())
self.uiESIndexLine.add(self.uiESIndex)
self.panel.add(self.uiESIndexLine)
uiToolsLine = JPanel()
uiToolsLine.setLayout(BoxLayout(uiToolsLine, BoxLayout.LINE_AXIS))
uiToolsLine.setAlignmentX(JPanel.LEFT_ALIGNMENT)
self.uiCBSuite = JCheckBox("Suite")
uiToolsLine.add(self.uiCBSuite)
uiToolsLine.add(Box.createRigidArea(Dimension(10, 0)))
self.uiCBTarget = JCheckBox("Target")
uiToolsLine.add(self.uiCBTarget)
uiToolsLine.add(Box.createRigidArea(Dimension(10, 0)))
self.uiCBProxy = JCheckBox("Proxy")
uiToolsLine.add(self.uiCBProxy)
#.........这里部分代码省略.........