本文整理汇总了Python中elasticsearch_dsl.Index.open方法的典型用法代码示例。如果您正苦于以下问题:Python Index.open方法的具体用法?Python Index.open怎么用?Python Index.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类elasticsearch_dsl.Index
的用法示例。
在下文中一共展示了Index.open方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from elasticsearch_dsl import Index [as 别名]
# 或者: from elasticsearch_dsl.Index import open [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:
#.........这里部分代码省略.........
示例2: BurpExtender
# 需要导入模块: from elasticsearch_dsl import Index [as 别名]
# 或者: from elasticsearch_dsl.Index import open [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)
#.........这里部分代码省略.........