Merge branch 'master' into pro

* master:
  Another Elasticsearch More Like This implementation, but it still doesn't surface anything close to correct.
This commit is contained in:
Samuel Clay 2022-05-05 14:26:18 -04:00
commit 6b0250ab55
4 changed files with 87 additions and 1 deletions

View file

@ -431,6 +431,56 @@ class SearchStory:
return result_ids
@classmethod
def more_like_this(cls, feed_ids, story_hash, order, offset, limit):
try:
cls.ES().indices.flush(cls.index_name())
except elasticsearch.exceptions.NotFoundError as e:
logging.debug(f" ***> ~FRNo search server available: {e}")
return []
body = {
"query": {
"bool": {
"filter": [{
"more_like_this": {
"fields": [ "title", "content" ],
"like": [
{
"_index": cls.index_name(),
"_id": story_hash,
}
],
"min_term_freq": 3,
"min_doc_freq": 2,
"min_word_length": 4,
},
},{
"terms": { "feed_id": feed_ids[:2000] }
}],
}
},
'sort': [{'date': {'order': 'desc' if order == "newest" else "asc"}}],
'from': offset,
'size': limit
}
try:
results = cls.ES().search(body=body, index=cls.index_name(), doc_type=cls.doc_type())
except elasticsearch.exceptions.RequestError as e:
logging.debug(" ***> ~FRNo search server available for querying: %s" % e)
return []
logging.info(" ---> ~FG~SNMore like this ~FCstories~FG for: ~SB%s~SN, ~SB%s~SN results (across %s feed%s)" %
(story_hash, len(results['hits']['hits']), len(feed_ids), 's' if len(feed_ids) != 1 else ''))
try:
result_ids = [r['_id'] for r in results['hits']['hits']]
except Exception as e:
logging.info(" ---> ~FRInvalid search query \"%s\": %s" % (query, e))
return []
return result_ids
class SearchFeed:

7
apps/search/urls.py Normal file
View file

@ -0,0 +1,7 @@
from django.conf.urls import *
from apps.search import views
urlpatterns = [
# url(r'^$', views.index),
url(r'^more_like_this', views.more_like_this, name='more-like-this'),
]

View file

@ -1 +1,29 @@
# Create your views here.
from apps.rss_feeds.models import Feed, MStory
from apps.reader.models import UserSubscription
from apps.search.models import SearchStory
from utils import json_functions as json
from utils.view_functions import required_params
from utils.user_functions import get_user, ajax_login_required
# @required_params('story_hash')
@json.json_view
def more_like_this(request):
user = get_user(request)
get_post = getattr(request, request.method)
order = get_post.get('order', 'newest')
page = int(get_post.get('page', 1))
limit = int(get_post.get('limit', 10))
offset = limit * (page-1)
story_hash = get_post.get('story_hash')
feed_ids = [us.feed_id for us in UserSubscription.objects.filter(user=user)]
feed_ids, _ = MStory.split_story_hash(story_hash)
story_ids = SearchStory.more_like_this([feed_ids], story_hash, order, offset=offset, limit=limit)
stories_db = MStory.objects(
story_hash__in=story_ids
).order_by('-story_date' if order == "newest" else 'story_date')
stories = Feed.format_stories(stories_db)
return {
"stories": stories,
}

View file

@ -36,6 +36,7 @@ urlpatterns = [
url(r'^notifications/?', include('apps.notifications.urls')),
url(r'^statistics/', include('apps.statistics.urls')),
url(r'^social/', include('apps.social.urls')),
url(r'^search/', include('apps.search.urls')),
url(r'^oauth/', include('apps.oauth.urls')),
url(r'^mobile/', include('apps.mobile.urls')),
url(r'^m/', include('apps.mobile.urls')),