Skip to content

Commit

Permalink
Updating person suggest to include education, created_date
Browse files Browse the repository at this point in the history
  • Loading branch information
yattias committed Oct 31, 2024
1 parent fca1104 commit 2baaf72
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 34 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"program": "${workspaceFolder}/src/manage.py",
"args": [
"runserver",
"[::]:8000"
"[::]:8000",
"--ipv6"
],
"env": {
"USE_DEBUG_TOOLBAR": "True"
Expand Down
18 changes: 16 additions & 2 deletions src/search/documents/person.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class PersonDocument(BaseDocument):
full_name = es_fields.TextField(attr="full_name", analyzer=content_analyzer)
person_types = es_fields.KeywordField(attr="person_types_indexing")
headline = es_fields.ObjectField(
attr="headline",
properties={
"title": es_fields.TextField(),
},
Expand All @@ -32,6 +31,8 @@ class PersonDocument(BaseDocument):
suggestion_phrases = es_fields.Completion()
user_id = es_fields.IntegerField(attr="user_id")
reputation_hubs = es_fields.KeywordField()
education = es_fields.KeywordField()
created_date = es_fields.DateField(attr="created_date")

class Index:
name = "person"
Expand All @@ -47,18 +48,31 @@ class Django:
def should_remove_from_index(self, obj):
return False

def prepare_headline(self, instance):
return instance.build_headline()

def prepare_reputation_hubs(self, instance):
reputation_hubs = []
for rep in instance.reputation_list:
reputation_hubs.append(rep["hub"]["name"])

return reputation_hubs

def prepare_education(self, instance):
education = []
for edu in instance.education:
education.append(edu["name"])

return education

def prepare_suggestion_phrases(self, instance):
suggestions = []

if instance.full_name:
suggestions.append({"input": instance.full_name, "weight": 10})
if instance.user:
suggestions.append({"input": instance.full_name, "weight": 15})
else:
suggestions.append({"input": instance.full_name, "weight": 10})

if instance.first_name:
suggestions.append({"input": instance.first_name, "weight": 5})
Expand Down
33 changes: 33 additions & 0 deletions src/user/related_models/author_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,39 @@ def __str__(self):
f"{university_city}"
)

def build_headline(self):
from collections import Counter

if self.headline:
return self.headline

try:
all_topics = []
authored_papers = self.authored_papers.all()

for p in authored_papers:
unified_document = p.unified_document
all_topics += list(unified_document.topics.all())

topic_counts = Counter(all_topics)

# Sort topics by frequency
sorted_topics = sorted(
topic_counts.items(), key=lambda x: x[1], reverse=True
)

# Extract topics from sorted list
sorted_topics = [topic for topic, _ in sorted_topics]

if not sorted_topics:
return None

return {
"title": "Author with expertise in " + sorted_topics[0].display_name
}
except Exception:
return None

@property
def full_name(self):
return self.first_name + " " + self.last_name
Expand Down
32 changes: 1 addition & 31 deletions src/user/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,37 +1092,7 @@ def get_achievements(self, author):
return author.achievements

def get_headline(self, author):
from collections import Counter

if author.headline:
return author.headline

try:
all_topics = []
authored_papers = author.authored_papers.all()

for p in authored_papers:
unified_document = p.unified_document
all_topics += list(unified_document.topics.all())

topic_counts = Counter(all_topics)

# Sort topics by frequency
sorted_topics = sorted(
topic_counts.items(), key=lambda x: x[1], reverse=True
)

# Extract topics from sorted list
sorted_topics = [topic for topic, _ in sorted_topics]

if not sorted_topics:
return None

return {
"title": "Author with expertise in " + sorted_topics[0].display_name
}
except Exception:
return None
return author.build_headline()

def get_user(self, author):
user = author.user
Expand Down

0 comments on commit 2baaf72

Please sign in to comment.