2012-03-11 16:13:05 -07:00
|
|
|
import pyes
|
|
|
|
from django.conf import settings
|
|
|
|
|
2012-07-13 14:33:16 -07:00
|
|
|
class SearchStarredStory:
|
2012-03-11 16:13:05 -07:00
|
|
|
|
2012-07-13 14:33:16 -07:00
|
|
|
ES = pyes.ES(settings.ELASTICSEARCH_HOSTS)
|
|
|
|
name = "starred-stories"
|
2012-03-11 16:13:05 -07:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_elasticsearch_mapping(cls):
|
2012-07-13 14:33:16 -07:00
|
|
|
cls.ES.create_index("%s-index" % cls.name)
|
2012-03-11 16:13:05 -07:00
|
|
|
mapping = {
|
|
|
|
'title': {
|
|
|
|
'boost': 2.0,
|
|
|
|
'index': 'analyzed',
|
|
|
|
'store': 'yes',
|
|
|
|
'type': 'string',
|
|
|
|
"term_vector" : "with_positions_offsets"
|
|
|
|
},
|
|
|
|
'content': {
|
|
|
|
'boost': 1.0,
|
|
|
|
'index': 'analyzed',
|
|
|
|
'store': 'yes',
|
|
|
|
'type': 'string',
|
|
|
|
"term_vector" : "with_positions_offsets"
|
|
|
|
},
|
|
|
|
'author': {
|
|
|
|
'boost': 1.0,
|
|
|
|
'index': 'analyzed',
|
|
|
|
'store': 'yes',
|
2012-12-20 16:07:22 -08:00
|
|
|
'type': 'string',
|
|
|
|
},
|
|
|
|
'db_id': {
|
|
|
|
'index': 'not_analyzed',
|
|
|
|
'store': 'yes',
|
|
|
|
'type': 'string',
|
2012-03-11 16:13:05 -07:00
|
|
|
},
|
|
|
|
'feed_id': {
|
|
|
|
'store': 'yes',
|
|
|
|
'type': 'integer'
|
|
|
|
},
|
|
|
|
'date': {
|
|
|
|
'store': 'yes',
|
|
|
|
'type': 'date',
|
2012-07-13 14:33:16 -07:00
|
|
|
},
|
|
|
|
'user_ids': {
|
|
|
|
'index': 'not_analyzed',
|
|
|
|
'store': 'yes',
|
|
|
|
'type': 'integer',
|
|
|
|
'index_name': 'user_id'
|
2012-03-11 16:13:05 -07:00
|
|
|
}
|
|
|
|
}
|
2012-07-13 14:33:16 -07:00
|
|
|
cls.ES.put_mapping("%s-type" % cls.name, {'properties': mapping}, ["%s-index" % cls.name])
|
2012-03-11 16:13:05 -07:00
|
|
|
|
|
|
|
@classmethod
|
2012-12-20 16:07:22 -08:00
|
|
|
def index(cls, user_id, story_id, story_title, story_content, story_author, story_date, db_id):
|
2012-03-11 16:13:05 -07:00
|
|
|
doc = {
|
|
|
|
"content": story_content,
|
|
|
|
"title": story_title,
|
|
|
|
"author": story_author,
|
2012-07-13 14:33:16 -07:00
|
|
|
"date": story_date,
|
|
|
|
"user_ids": user_id,
|
2012-12-20 16:07:22 -08:00
|
|
|
"db_id": db_id,
|
2012-03-11 16:13:05 -07:00
|
|
|
}
|
2012-07-13 14:33:16 -07:00
|
|
|
cls.ES.index(doc, "%s-index" % cls.name, "%s-type" % cls.name, story_id)
|
2012-03-11 16:13:05 -07:00
|
|
|
|
|
|
|
@classmethod
|
2012-07-13 14:33:16 -07:00
|
|
|
def query(cls, user_id, text):
|
|
|
|
cls.ES.refresh()
|
2012-03-11 16:13:05 -07:00
|
|
|
q = pyes.query.StringQuery(text)
|
|
|
|
results = cls.ES.search(q)
|
2012-12-20 16:07:22 -08:00
|
|
|
|
|
|
|
if not results.total:
|
|
|
|
q = pyes.query.FuzzyQuery('title', text)
|
|
|
|
results = cls.ES.search(q)
|
|
|
|
|
|
|
|
if not results.total:
|
|
|
|
q = pyes.query.FuzzyQuery('content', text)
|
|
|
|
results = cls.ES.search(q)
|
|
|
|
|
|
|
|
if not results.total:
|
|
|
|
q = pyes.query.FuzzyQuery('author', text)
|
|
|
|
results = cls.ES.search(q)
|
|
|
|
|
2012-03-11 16:13:05 -07:00
|
|
|
return results
|