Skip to content

Commit

Permalink
Show most recent documents by default in Document Explorer
Browse files Browse the repository at this point in the history
- Modified admin_retrieval to support empty queries and time-based sorting
- Updated admin_search to handle empty queries and return most recent docs
- Updated Explorer.tsx to show documents immediately without requiring search

Co-Authored-By: Chris Weaver <chris@onyx.app>
  • Loading branch information
devin-ai-integration[bot] and Chris Weaver committed Jan 19, 2025
1 parent 32a97e5 commit 6754352
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
34 changes: 22 additions & 12 deletions backend/onyx/document_index/vespa/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,27 +743,37 @@ def hybrid_retrieval(

def admin_retrieval(
self,
query: str,
query: str | None,
filters: IndexFilters,
num_to_retrieve: int = NUM_RETURNED_HITS,
offset: int = 0,
most_recent: bool = False,
) -> list[InferenceChunkUncleaned]:
vespa_where_clauses = build_vespa_filters(filters, include_hidden=True)
yql = (
YQL_BASE.format(index_name=self.index_name)
+ vespa_where_clauses
+ '({grammar: "weakAnd"}userInput(@query) '
# `({defaultIndex: "content_summary"}userInput(@query))` section is
# needed for highlighting while the N-gram highlighting is broken /
# not working as desired
+ f'or ({{defaultIndex: "{CONTENT_SUMMARY}"}}userInput(@query)))'
)

if query is None and most_recent:
# For most recent documents, sort by time in descending order
yql = (
f"select * from {self.index_name} where "
+ vespa_where_clauses.strip()
+ " order by time desc"
)
else:
yql = (
YQL_BASE.format(index_name=self.index_name)
+ vespa_where_clauses
+ '({grammar: "weakAnd"}userInput(@query) '
# `({defaultIndex: "content_summary"}userInput(@query))` section is
# needed for highlighting while the N-gram highlighting is broken /
# not working as desired
+ f'or ({{defaultIndex: "{CONTENT_SUMMARY}"}}userInput(@query)))'
)

params: dict[str, str | int] = {
"yql": yql,
"query": query,
"query": query or "", # Convert None to empty string
"hits": num_to_retrieve,
"offset": 0,
"offset": offset,
"ranking.profile": "admin_search",
"timeout": VESPA_TIMEOUT,
}
Expand Down
13 changes: 12 additions & 1 deletion backend/onyx/server/query_and_chat/query_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,18 @@ def admin_search(
status_code=400,
detail="Cannot use admin-search when using a non-Vespa document index",
)
matching_chunks = document_index.admin_retrieval(query=query, filters=final_filters)
# If query is empty or whitespace, return most recent documents
if not query.strip():
matching_chunks = document_index.admin_retrieval(
query=None,
filters=final_filters,
most_recent=True
)
else:
matching_chunks = document_index.admin_retrieval(
query=query,
filters=final_filters
)

documents = chunks_or_sections_to_search_docs(matching_chunks)

Expand Down
25 changes: 13 additions & 12 deletions web/src/app/admin/documents/explorer/Explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,18 @@ export function Explorer({
clearTimeout(timeoutId);
}

if (query && query.trim() !== "") {
router.replace(
`/admin/documents/explorer?query=${encodeURIComponent(query)}`
);

const newTimeoutId = window.setTimeout(() => onSearch(query), 300);
setTimeoutId(newTimeoutId);
} else {
setResults([]);
if (timeoutId !== null) {
clearTimeout(timeoutId);
}

// Always update the URL with the current query (or empty string)
router.replace(
`/admin/documents/explorer?query=${encodeURIComponent(query)}`
);

// Always trigger a search, even with empty query to show most recent documents
const newTimeoutId = window.setTimeout(() => onSearch(query), 300);
setTimeoutId(newTimeoutId);
}, [
query,
filterManager.selectedDocumentSets,
Expand Down Expand Up @@ -217,10 +219,9 @@ export function Explorer({
})}
</div>
)}
{!query && (
{results.length === 0 && (
<div className="flex text-emphasis mt-3">
Search for a document above to modify its boost or hide it from
searches.
No documents found. Try adjusting your search query or filters.
</div>
)}
</div>
Expand Down

0 comments on commit 6754352

Please sign in to comment.