本文整理汇总了Python中pyelasticsearch.ElasticSearch类的典型用法代码示例。如果您正苦于以下问题:Python ElasticSearch类的具体用法?Python ElasticSearch怎么用?Python ElasticSearch使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ElasticSearch类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main():
#Train the Naive Bayes Classifier
f=open('./data_set/naivebayes_trained_model.pickle')
NBClassifier=pickle.load(f)
#ElasticSearch- Call the es_indexer file to create 'sentiment_analysis' index and store
#the contents of the tweet file in that Index
es=ElasticSearch('http://localhost:9200/')
es_indexer()
############Indexing into Elasticsearch############
i=0
for each in tweet_data():
i+=1
testTweet= each
processedTestTweet=process_tweet(testTweet)
sentiment=NBClassifier.classify(extract_features(build_feature_vector(processedTestTweet)))
es.index("sentiment_analysis","document",{
"text": testTweet,
"sentiment": sentiment
},id=i)
print "Indexing completed."
es.refresh(index="sentiment_analysis")
print "Index refreshed."
f.close()
示例2: _get
def _get(self):
"""Build and run the ES query
"""
opts = self.opts
es = ElasticSearch(opts.url)
query = {'sort': {'@timestamp': 'desc'},
'size': 1}
if opts.query:
query['query'] = {
'filtered': {
'query': {
'query_string': {
'query': opts.query
}
}
}
}
# ElasticSearch allows us to pass an array of indices. However,
# it will throw an exception if any of these don't exist. This
# isn't the right behavior, because there may not actually be
# a logstash index from X days ago. Instead, we need to iterate
# through the daily log indexes in reverse order until we get a
# non-error response.
result = None
for index in self._indexes():
try:
result = es.search(query, index=index)
break
except ElasticHttpNotFoundError, e:
pass
示例3: ESPipeline
class ESPipeline(object):
def __init__(self, *args, **kwargs):
self.client = ElasticSearch('http://localhost:9200/')
def process_item(self, item, spider):
self.client.index('wiki', 'page', dict(item))
return item
示例4: analyze_post
def analyze_post(token, text):
response = {
'post_now': False,
'hours_to_wait': 1,
'total_score': 0,
'time_score': 0,
'text_score': 0,
'hint': "Building index",
}
try:
data = Newsfeed.filter_only_posts_by_people(token)
except Exception, e:
es = ElasticSearch('http://localhost:9200/')
try:
es.create_index(token.lower())
Newsfeed.newsfeed(token, [], 0, None, 1)
t = threading.Thread(target=Newsfeed.newsfeed, args=(token, [], 0, None, 1500))
t.setDaemon(True)
t.start()
except Exception, e:
print e.message
示例5: ESDiffs
class ESDiffs(object):
"""Implementation of Elastic Search as diff backend"""
def __init__(self):
self.es = ElasticSearch(settings.ELASTIC_SEARCH_URLS)
@staticmethod
def to_id(label, old, new):
return "%s/%s/%s" % (label, old, new)
def put(self, label, old_version, new_version, diff):
"""Store a diff between two versions of a regulation node"""
struct = {
'label': label,
'old_version': old_version,
'new_version': new_version,
'diff': diff
}
self.es.index(settings.ELASTIC_SEARCH_INDEX, 'diff', struct,
id=self.to_id(label, old_version, new_version))
def get(self, label, old_version, new_version):
"""Find the associated diff"""
try:
result = self.es.get(settings.ELASTIC_SEARCH_INDEX, 'diff',
self.to_id(label, old_version, new_version))
return result['_source']['diff']
except ElasticHttpNotFoundError:
return None
示例6: get_documents
def get_documents(urls):
host = environ['ELASTICSEARCH_SERVER'] if environ.get('ELASTICSEARCH_SERVER') else 'http://localhost:9200'
es = ElasticSearch(host)
if len(urls) > 0:
results = {}
for url in urls:
query = {
"query": {
"term": {
"url": url
}
},
"fields": ["text"]
}
res = es.search(query,
index=environ['ELASTICSEARCH_INDEX'] if environ.get('ELASTICSEARCH_INDEX') else 'memex',
doc_type=environ['ELASTICSEARCH_DOC_TYPE'] if environ.get('ELASTICSEARCH_DOC_TYPE') else 'page')
hits = res['hits']
try:
results[url] = hits['hits'][0]['fields']['text'][0]
except KeyError, e:
print url, e, " not found in database"
except IndexError, e:
print url, e, " not found in database"
示例7: term_search
def term_search(field, queryStr):
es_server = 'http://localhost:9200/'
if environ.get('ELASTICSEARCH_SERVER'):
es_server = environ['ELASTICSEARCH_SERVER']
es = ElasticSearch(es_server)
if len(queryStr) > 0:
query = {
"query" : {
"match": {
field: {
"query": ' '.join(queryStr),
"minimum_should_match":"100%"
}
}
},
"fields": ["url"]
}
print query
res = es.search(query,
index=environ['ELASTICSEARCH_INDEX'] if environ.get('ELASTICSEARCH_INDEX') else 'memex',
doc_type=environ['ELASTICSEARCH_DOC_TYPE'] if environ.get('ELASTICSEARCH_DOC_TYPE') else 'page',
size=500)
hits = res['hits']
urls = []
for hit in hits['hits']:
urls.append(hit['_id'])
return urls
示例8: search
def search(field, queryStr):
es_server = 'http://localhost:9200/'
es_index = 'memex'
es_doc_type = 'page'
if environ.get('ELASTICSEARCH_SERVER'):
es_server = environ['ELASTICSEARCH_SERVER']
if environ.get('ELASTICSEARCH_INDEX'):
es_index = environ['ELASTICSEARCH_INDEX']
if environ.get('ELASTICSEARCH_DOC_TYPE'):
es_doc_type = environ['ELASTICSEARCH_DOC_TYPE']
es = ElasticSearch(es_server)
if len(queryStr) > 0:
query = {
"query": {
"query_string": {
"fields" : [field],
"query": ' and '.join(queryStr[0:]),
}
},
"fields": [field]
}
print query
res = es.search(query, index=es_index, doc_type=es_doc_type, size=500)
hits = res['hits']
urls = []
for hit in hits['hits']:
urls.append(hit['_id'])
return urls
示例9: ESNotices
class ESNotices(object):
"""Implementation of Elastic Search as notice backend"""
def __init__(self):
self.es = ElasticSearch(settings.ELASTIC_SEARCH_URLS)
def put(self, doc_number, notice):
"""Store a single notice"""
self.es.index(settings.ELASTIC_SEARCH_INDEX, 'notice', notice,
id=doc_number)
def get(self, doc_number):
"""Find the associated notice"""
try:
result = self.es.get(settings.ELASTIC_SEARCH_INDEX, 'notice',
doc_number)
return result['_source']
except ElasticHttpNotFoundError:
return None
def listing(self, part=None):
"""All notices or filtered by cfr_part"""
if part:
query = {'match': {'cfr_part': part}}
else:
query = {'match_all': {}}
query = {'fields': ['effective_on', 'fr_url', 'publication_date'],
'query': query}
notices = []
results = self.es.search(query, doc_type='notice', size=100,
index=settings.ELASTIC_SEARCH_INDEX)
for notice in results['hits']['hits']:
notice['fields']['document_number'] = notice['_id']
notices.append(notice['fields'])
return notices
示例10: get_image
def get_image(url, output_path=""):
es_server = 'http://localhost:9200/'
if environ.get('ELASTICSEARCH_SERVER'):
es_server = environ['ELASTICSEARCH_SERVER']
es = ElasticSearch(es_server)
if output_path:
output_path = output_path+'/'
if url:
query = {
"query": {
"term": {
"url": url
}
},
"fields": ["thumbnail"]
}
res = es.search(query, index='memex', doc_type='page')
hits = res['hits']
if (len(hits) > 0):
img = base64.b64decode(hits['hits'][0]['fields']['thumbnail'][0])
with open(output_path+urllib2.quote(url).replace("/", "%2F")+'.png','wb') as f:
f.write(img)
else:
print "No thumbnail found"
示例11: get_image
def get_image(url, es_index='memex', es_doc_type='page', es=None):
if es is None:
es = ElasticSearch("http://localhost:9200")
if url:
query = {
"query": {
"term": {
"url": url
}
},
"fields": ["thumbnail", "thumbnail_name"]
}
res = es.search(query, index=es_index, doc_type=es_doc_type, size=500)
hits = res['hits']['hits']
if (len(hits) > 0):
try:
img = base64.b64decode(hits[0]['fields']['thumbnail'][0])
img_name = hits[0]['fields']['thumbnail_name'][0]
return [img_name, img]
except KeyError:
print "No thumbnail found"
else:
print "No thumbnail found"
return [None, None]
示例12: get_context
def get_context(terms, es_index='memex', es_doc_type='page', es=None):
if es is None:
es = ElasticSearch("http://localhost:9200")
if len(terms) > 0:
query = {
"query": {
"match": {
"text": {
"query": ' and '.join(terms[0:]),
"operator" : "and"
}
}
},
"highlight" : {
"fields" : {
"text": {
"fragment_size" : 100, "number_of_fragments" : 1
}
}
}
}
print query
res = es.search(query, index=es_index, doc_type=es_doc_type, size=500)
hits = res['hits']
highlights = []
for hit in hits['hits']:
highlights.append(hit['highlight']['text'][0])
return highlights
示例13: range
def range(field, from_val, to_val, ret_fields=[], epoch=None, es_index='memex', es_doc_type='page', es=None):
if es is None:
es = ElasticSearch("http://localhost:9200")
if not (epoch is None):
if epoch:
from_val = datetime.utcfromtimestamp(long(from_val)).strftime('%Y-%m-%dT%H:%M:%S')
to_val = datetime.utcfromtimestamp(long(to_val)).strftime('%Y-%m-%dT%H:%M:%S')
query = {
"query" : {
"range" : {
field : {
"from": from_val,
"to": to_val
}
},
},
"fields": ret_fields
}
res = es.search(query, index=es_index, doc_type=es_doc_type, size=500)
hits = res['hits']['hits']
results=[]
for hit in hits:
results.append(hit['fields'])
return results
示例14: get_documents
def get_documents(terms, term_field, fields=["text"], es_index='memex', es_doc_type='page', es=None):
if es is None:
es = ElasticSearch('http://localhost:9200/')
if len(terms) > 0:
results = {}
for term in terms:
query = {
"query": {
"term": {
term_field: term
}
},
"fields": fields
}
res = es.search(query,
index=es_index,
doc_type=es_doc_type)
if res['hits']['hits']:
hits = res['hits']['hits'][0]
if not hits.get('fields') is None:
hits = hits['fields']
record = {}
for field in fields:
if(not hits.get(field) is None):
record[field] = hits[field][0]
results[term] = record
return results
示例15: set_in_index
def set_in_index(self, documentList):
"""
Store the list of documents in the Elasticsearch index via HTTP APIs
@type documentList: List
@param documentList: List of image layer JSON documents
"""
#Get the Elasticsearch address from the config file
cfg = config.load()
#Store the document list in Elasticsearch
es = ElasticSearch(cfg.search_options.get("address"))
try:
es.bulk_index(cfg.search_options.get("index"), cfg.search_options.get("type"), documentList, id_field='id')
except InvalidJsonResponseError:
logger.debug("InvalidJsonResponseError!")
except Timeout:
logger.debug("Timeout!")
except ConnectionError:
logger.debug("ConnectionError!")
except ElasticHttpError:
logger.debug("ElasticHttpError!")
except InvalidJsonResponseError:
logger.debug("InvalidJsonResponseError!")
except ElasticHttpNotFoundError:
logger.debug("ElasticHttpNotFoundError!")