Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Add user IDs and last crawled date to the index

Daoud Clarkec9d68373c4a3
diff --git a/mwmbl/crawler/app.py b/mwmbl/crawler/app.py
index 3ee48a1..662e4c1 100644
--- a/mwmbl/crawler/app.py
+++ b/mwmbl/crawler/app.py
@@ -265,11 +265,17 @@ def _register_routes(r: Router | NinjaAPI, batch_cache: BatchCache, queued_batch
if api_key is None:
return 401, {"message": "Invalid API key or insufficient scope (crawl scope required)."}
- documents = [Document(url=result.url, title=result.title, extract=result.extract) for result in results.results]
+ now = datetime.now(timezone.utc)
+ documents = [
+ Document(
+ url=result.url, title=result.title, extract=result.extract,
+ user_ids=[api_key.user.id],
+ last_crawled=int(now.timestamp()),
+ )
+ for result in results.results
+ ]
index_path = f"{settings.DATA_PATH}/{settings.INDEX_NAME}"
index_documents(documents, index_path)
-
- now = datetime.now(timezone.utc)
filename = upload_object(results, now, api_key.user.username, "results")
# Update stats for the user
diff --git a/mwmbl/indexer/index_batches.py b/mwmbl/indexer/index_batches.py
index 47a5feb..de7b578 100644
--- a/mwmbl/indexer/index_batches.py
+++ b/mwmbl/indexer/index_batches.py
@@ -6,7 +6,7 @@ from collections import defaultdict, Counter
from datetime import datetime
from functools import reduce
from logging import getLogger
-from typing import Collection, Iterable
+from typing import Collection, Iterable, Optional
from mwmbl.crawler.batch import HashedBatch, Item
from mwmbl.crawler.urls import URLStatus
@@ -20,12 +20,28 @@ from mwmbl.utils import add_term_infos
logger = getLogger(__name__)
+MAX_USER_IDS = 2
-def get_documents_from_batches(batches: Collection[HashedBatch]) -> Iterable[tuple[str, str, str]]:
+
+def _merge_user_ids(
+ existing: Optional[list[int]], incoming: Optional[list[int]]
+) -> Optional[list[int]]:
+ combined = list(existing or [])
+ for uid in (incoming or []):
+ if uid in combined:
+ combined.remove(uid)
+ combined.append(uid)
+ return combined[-MAX_USER_IDS:] or None
+
+
+def get_documents_from_batches(batches: Collection[HashedBatch]) -> Iterable[Document]:
for batch in batches:
for item in batch.items:
if item.content is not None and not item.content.links_only:
- yield item.content.title, item.url, item.content.extract
+ yield Document(
+ item.content.title, item.url, item.content.extract,
+ last_crawled=int(item.timestamp),
+ )
def run(batch_cache: BatchCache, index_path: str):
@@ -44,8 +60,7 @@ def get_url_score(url):
def index_batches(batch_data: Collection[HashedBatch], index_path: str) -> Counter:
start_time = datetime.utcnow()
- document_tuples = list(get_documents_from_batches(batch_data))
- documents = [Document(title, url, extract) for title, url, extract in document_tuples]
+ documents = list(get_documents_from_batches(batch_data))
end_time, new_page_doc_counts = index_documents(documents, index_path)
logger.info(f"Indexing took {end_time - start_time}")
return new_page_doc_counts
@@ -75,6 +90,14 @@ def index_pages(index_path: str, page_documents: dict[int, list[Document]], mark
def combine_documents(existing_documents, documents, mark_synced, ranker):
sorted_documents = sort_documents(documents, existing_documents, ranker)
+
+ url_user_ids = {}
+ url_last_crawled = {}
+ for doc in sorted_documents:
+ url_user_ids[doc.url] = _merge_user_ids(url_user_ids.get(doc.url), doc.user_ids)
+ if doc.last_crawled is not None:
+ url_last_crawled[doc.url] = max(url_last_crawled.get(doc.url, 0), doc.last_crawled)
+
seen_urls = set()
seen_titles = set()
combined_documents = []
@@ -83,6 +106,8 @@ def combine_documents(existing_documents, documents, mark_synced, ranker):
continue
if mark_synced:
document.state = DocumentState.SYNCED_WITH_MAIN_INDEX
+ document.user_ids = url_user_ids.get(document.url)
+ document.last_crawled = url_last_crawled.get(document.url)
combined_documents.append(document)
seen_urls.add(document.url)
seen_titles.add(document.title)
@@ -127,7 +152,12 @@ def preprocess_documents(documents, index_path):
tokenized = tokenize_document(document.url, document.title, document.extract, document.score)
for token in tokenized.tokens:
page = indexer.get_key_page_index(token)
- term_document = Document(document.title, document.url, document.extract, term=token)
+ term_document = Document(
+ document.title, document.url, document.extract,
+ term=token,
+ user_ids=document.user_ids,
+ last_crawled=document.last_crawled,
+ )
page_documents[page].append(term_document)
print(f"Preprocessed for {len(page_documents)} pages")
return page_documents
diff --git a/mwmbl/tinysearchengine/indexer.py b/mwmbl/tinysearchengine/indexer.py
index 90416c2..ee1d431 100644
--- a/mwmbl/tinysearchengine/indexer.py
+++ b/mwmbl/tinysearchengine/indexer.py
@@ -46,6 +46,8 @@ class Document:
score: Optional[float] = None
term: Optional[str] = None
state: Optional[int] = None
+ user_ids: Optional[List[int]] = None
+ last_crawled: Optional[int] = None
def __init__(
self,
@@ -54,7 +56,9 @@ class Document:
extract: str,
score: Optional[float] = None,
term: Optional[str] = None,
- state: Optional[int | DocumentState] = None
+ state: Optional[int | DocumentState] = None,
+ user_ids: Optional[List[int]] = None,
+ last_crawled: Optional[int] = None,
):
# Sometimes the title or extract may be None, probably because of user generated content
# It's not allowed to be None though, or things will break
@@ -64,15 +68,23 @@ class Document:
self.score = score
self.term = term
self.state = None if state is None else DocumentState(state)
+ self.user_ids = user_ids
+ self.last_crawled = last_crawled
def as_tuple(self):
"""
Convert a type to a tuple - values at the end that are None can be truncated.
"""
- values = list(self.__dict__.values())
- if values[-1] is not None:
- values[-1] = values[-1].value
-
+ values = [
+ self.title,
+ self.url,
+ self.extract,
+ self.score,
+ self.term,
+ None if self.state is None else self.state.value,
+ self.user_ids,
+ self.last_crawled,
+ ]
while values[-1] is None:
values = values[:-1]
return tuple(values)
diff --git a/mwmbl/tinysearchengine/search.py b/mwmbl/tinysearchengine/search.py
index e9336fa..c26d34d 100644
--- a/mwmbl/tinysearchengine/search.py
+++ b/mwmbl/tinysearchengine/search.py
@@ -95,6 +95,8 @@ class RawDocument(Schema):
score: Optional[float] = None
term: Optional[str] = None
state: Optional[int] = None
+ user_ids: Optional[list[int]] = None
+ last_crawled: Optional[int] = None
class Config:
json_schema_extra = {