mirror of
https://github.com/viq/NewsBlur.git
synced 2025-08-31 22:20:12 +00:00
Changing from static class var to instantiate once.
This commit is contained in:
parent
8ef854573c
commit
f45b19ed77
1 changed files with 22 additions and 15 deletions
|
@ -189,9 +189,17 @@ class MUserSearch(mongo.Document):
|
||||||
|
|
||||||
class SearchStory:
|
class SearchStory:
|
||||||
|
|
||||||
ES = elasticsearch.Elasticsearch(settings.ELASTICSEARCH_STORY_HOSTS)
|
_es_client = None
|
||||||
name = "stories"
|
name = "stories"
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def ES(cls):
|
||||||
|
if cls._es_client is None:
|
||||||
|
cls._es_client = elasticsearch.Elasticsearch(settings.ELASTICSEARCH_STORY_HOSTS)
|
||||||
|
if not cls._es_client.indices.exists(cls.index_name()):
|
||||||
|
cls.create_elasticsearch_mapping()
|
||||||
|
return cls._es_client
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def index_name(cls):
|
def index_name(cls):
|
||||||
return "%s-index" % cls.name
|
return "%s-index" % cls.name
|
||||||
|
@ -199,9 +207,9 @@ class SearchStory:
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_elasticsearch_mapping(cls, delete=False):
|
def create_elasticsearch_mapping(cls, delete=False):
|
||||||
if delete:
|
if delete:
|
||||||
cls.ES.indices.delete(cls.index_name(), ignore=404)
|
cls.ES().indices.delete(cls.index_name(), ignore=404)
|
||||||
try:
|
try:
|
||||||
cls.ES.indices.create(cls.index_name())
|
cls.ES().indices.create(cls.index_name())
|
||||||
except elasticsearch.exceptions.RequestError as e:
|
except elasticsearch.exceptions.RequestError as e:
|
||||||
if 'already exists' in str(e):
|
if 'already exists' in str(e):
|
||||||
pass
|
pass
|
||||||
|
@ -241,11 +249,11 @@ class SearchStory:
|
||||||
'type': 'date',
|
'type': 'date',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cls.ES.indices.put_mapping(index=cls.index_name(), body={
|
cls.ES().indices.put_mapping(index=cls.index_name(), body={
|
||||||
'properties': mapping,
|
'properties': mapping,
|
||||||
'_source': {'enabled': False},
|
'_source': {'enabled': False},
|
||||||
})
|
})
|
||||||
cls.ES.indices.flush()
|
cls.ES().indices.flush()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def index(cls, story_hash, story_title, story_content, story_tags, story_author, story_feed_id,
|
def index(cls, story_hash, story_title, story_content, story_tags, story_author, story_feed_id,
|
||||||
|
@ -259,25 +267,25 @@ class SearchStory:
|
||||||
"date" : story_date,
|
"date" : story_date,
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
cls.ES.index(body=doc, index=cls.index_name(), id=story_hash)
|
cls.ES().index(body=doc, index=cls.index_name(), id=story_hash)
|
||||||
except elasticsearch.exceptions.ConnectionError:
|
except elasticsearch.exceptions.ConnectionError:
|
||||||
logging.debug(" ***> ~FRNo search server available.")
|
logging.debug(" ***> ~FRNo search server available.")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def remove(cls, story_hash):
|
def remove(cls, story_hash):
|
||||||
try:
|
try:
|
||||||
cls.ES.delete(index=cls.index_name(), id=story_hash)
|
cls.ES().delete(index=cls.index_name(), id=story_hash)
|
||||||
except elasticsearch.exceptions.ConnectionError:
|
except elasticsearch.exceptions.ConnectionError:
|
||||||
logging.debug(" ***> ~FRNo search server available.")
|
logging.debug(" ***> ~FRNo search server available.")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def drop(cls):
|
def drop(cls):
|
||||||
cls.ES.indices.delete(cls.index_name(), ignore=404)
|
cls.ES().indices.delete(cls.index_name(), ignore=404)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def query(cls, feed_ids, query, order, offset, limit, strip=False):
|
def query(cls, feed_ids, query, order, offset, limit, strip=False):
|
||||||
cls.create_elasticsearch_mapping()
|
cls.create_elasticsearch_mapping()
|
||||||
cls.ES.indices.refresh()
|
cls.ES().indices.refresh()
|
||||||
|
|
||||||
if strip:
|
if strip:
|
||||||
query = re.sub(r'([^\s\w_\-])+', ' ', query) # Strip non-alphanumeric
|
query = re.sub(r'([^\s\w_\-])+', ' ', query) # Strip non-alphanumeric
|
||||||
|
@ -286,7 +294,7 @@ class SearchStory:
|
||||||
feed_q = pyes.query.TermsQuery('feed_id', feed_ids[:2000])
|
feed_q = pyes.query.TermsQuery('feed_id', feed_ids[:2000])
|
||||||
q = pyes.query.BoolQuery(must=[string_q, feed_q])
|
q = pyes.query.BoolQuery(must=[string_q, feed_q])
|
||||||
try:
|
try:
|
||||||
results = cls.ES.search(q, indices=cls.index_name(),
|
results = cls.ES().search(q, indices=cls.index_name(),
|
||||||
partial_fields={}, sort=sort, start=offset, size=limit)
|
partial_fields={}, sort=sort, start=offset, size=limit)
|
||||||
except elasticsearch.exceptions.ConnectionError:
|
except elasticsearch.exceptions.ConnectionError:
|
||||||
logging.debug(" ***> ~FRNo search server available.")
|
logging.debug(" ***> ~FRNo search server available.")
|
||||||
|
@ -306,14 +314,14 @@ class SearchStory:
|
||||||
@classmethod
|
@classmethod
|
||||||
def global_query(cls, query, order, offset, limit, strip=False):
|
def global_query(cls, query, order, offset, limit, strip=False):
|
||||||
cls.create_elasticsearch_mapping()
|
cls.create_elasticsearch_mapping()
|
||||||
cls.ES.indices.refresh()
|
cls.ES().indices.refresh()
|
||||||
|
|
||||||
if strip:
|
if strip:
|
||||||
query = re.sub(r'([^\s\w_\-])+', ' ', query) # Strip non-alphanumeric
|
query = re.sub(r'([^\s\w_\-])+', ' ', query) # Strip non-alphanumeric
|
||||||
sort = "date:desc" if order == "newest" else "date:asc"
|
sort = "date:desc" if order == "newest" else "date:asc"
|
||||||
string_q = pyes.query.QueryStringQuery(query, default_operator="AND")
|
string_q = pyes.query.QueryStringQuery(query, default_operator="AND")
|
||||||
try:
|
try:
|
||||||
results = cls.ES.search(string_q, indices=cls.index_name(),
|
results = cls.ES().search(string_q, indices=cls.index_name(),
|
||||||
partial_fields={}, sort=sort, start=offset, size=limit)
|
partial_fields={}, sort=sort, start=offset, size=limit)
|
||||||
except elasticsearch.exceptions.ConnectionError:
|
except elasticsearch.exceptions.ConnectionError:
|
||||||
logging.debug(" ***> ~FRNo search server available.")
|
logging.debug(" ***> ~FRNo search server available.")
|
||||||
|
@ -336,7 +344,6 @@ class SearchFeed:
|
||||||
_es_client = None
|
_es_client = None
|
||||||
name = "feeds"
|
name = "feeds"
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def ES(cls):
|
def ES(cls):
|
||||||
if cls._es_client is None:
|
if cls._es_client is None:
|
||||||
|
|
Loading…
Add table
Reference in a new issue