-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Feature/issue keyword search #9615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HonLuk
wants to merge
7
commits into
makeplane:preview
Choose a base branch
from
HonLuk:feature/issue-keyword-search
base: preview
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cf696d2
release: v1.3.0 #8835
sriramveeraghanta d0a4adc
release: v1.3.1 #8917
sriramveeraghanta 917b23a
release: v1.4.0 #9160
sriramveeraghanta 5662b76
release: v1.4.1 #9545
sriramveeraghanta 3833f73
feat: add issue content search
HonLuk be45479
perf: support multi-keyword search
HonLuk c94936c
fix: address search review feedback
HonLuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
apps/api/plane/tests/contract/app/test_global_search_app.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| """Contract tests for global workspace search.""" | ||
|
|
||
| # Copyright (c) 2023-present Plane Software, Inc. and contributors | ||
| # SPDX-License-Identifier: AGPL-3.0-only | ||
| # See the LICENSE file for details. | ||
|
|
||
| import pytest | ||
| from django.urls import reverse | ||
|
|
||
| from plane.db.models import Issue, Project, ProjectMember | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def project(db, workspace, create_user): | ||
| project = Project.objects.create( | ||
| name="Search Project", | ||
| identifier="SP", | ||
| workspace=workspace, | ||
| created_by=create_user, | ||
| ) | ||
| ProjectMember.objects.create(project=project, member=create_user, role=20, is_active=True) | ||
| return project | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def search_issues(db, workspace, project): | ||
| title_match = Issue.objects.create( | ||
| name="Title needle", | ||
| description_html="<p>Body without the search term.</p>", | ||
| workspace=workspace, | ||
| project=project, | ||
| ) | ||
| description_match = Issue.objects.create( | ||
| name="Unrelated title", | ||
| description_html="<p>This body contains the needle in the description.</p>", | ||
| workspace=workspace, | ||
| project=project, | ||
| ) | ||
| no_match = Issue.objects.create( | ||
| name="Unrelated issue", | ||
| description_html="<p>Nothing relevant here.</p>", | ||
| workspace=workspace, | ||
| project=project, | ||
| ) | ||
| return title_match, description_match, no_match | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def multi_keyword_search_issues(db, workspace, project): | ||
| title_and_description_match = Issue.objects.create( | ||
| name="Alpha title", | ||
| description_html="<p>Body contains beta.</p>", | ||
| workspace=workspace, | ||
| project=project, | ||
| ) | ||
| description_match = Issue.objects.create( | ||
| name="Unrelated title", | ||
| description_html="<p>Alpha and beta are both in the body.</p>", | ||
| workspace=workspace, | ||
| project=project, | ||
| ) | ||
| partial_match = Issue.objects.create( | ||
| name="Alpha only", | ||
| description_html="<p>Only alpha appears here.</p>", | ||
| workspace=workspace, | ||
| project=project, | ||
| ) | ||
| return title_and_description_match, description_match, partial_match | ||
|
|
||
|
|
||
| @pytest.mark.contract | ||
| class TestGlobalSearch: | ||
| @pytest.mark.django_db | ||
| def test_searches_issue_title_or_description(self, session_client, workspace, project, search_issues): | ||
| title_match, description_match, no_match = search_issues | ||
| response = session_client.get( | ||
| reverse("global-search", kwargs={"slug": workspace.slug}), | ||
| {"search": "needle", "project_id": str(project.id), "workspace_search": "false"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| results = {str(result["id"]): result for result in response.data["results"]["issue"]} | ||
|
|
||
| assert str(title_match.id) in results | ||
| assert str(description_match.id) in results | ||
| assert str(no_match.id) not in results | ||
| assert results[str(title_match.id)]["description_snippet"] is None | ||
| assert "needle" in results[str(description_match.id)]["description_snippet"].lower() | ||
| assert "<p>" not in results[str(description_match.id)]["description_snippet"] | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_requires_all_keywords_across_issue_title_and_description( | ||
| self, session_client, workspace, project, multi_keyword_search_issues | ||
| ): | ||
| title_and_description_match, description_match, partial_match = multi_keyword_search_issues | ||
| response = session_client.get( | ||
| reverse("global-search", kwargs={"slug": workspace.slug}), | ||
| {"search": " alpha beta ", "project_id": str(project.id), "workspace_search": "false"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| results = {str(result["id"]): result for result in response.data["results"]["issue"]} | ||
|
|
||
| assert str(title_and_description_match.id) in results | ||
| assert str(description_match.id) in results | ||
| assert str(partial_match.id) not in results | ||
| assert "beta" in results[str(title_and_description_match.id)]["description_snippet"].lower() | ||
| assert "alpha" in results[str(description_match.id)]["description_snippet"].lower() | ||
| assert "beta" in results[str(description_match.id)]["description_snippet"].lower() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| """Tests for issue search helpers.""" | ||
|
|
||
| # Copyright (c) 2023-present Plane Software, Inc. and contributors | ||
| # SPDX-License-Identifier: AGPL-3.0-only | ||
| # See the LICENSE file for details. | ||
|
|
||
| import pytest | ||
|
|
||
| from plane.utils.issue_search import build_search_snippet | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| class TestBuildSearchSnippet: | ||
| def test_returns_none_when_query_is_not_in_content(self): | ||
| assert build_search_snippet("A short description", "missing") is None | ||
|
|
||
| def test_returns_normalized_content_when_it_fits(self): | ||
| assert build_search_snippet(" Search result\ncontent ", "result") == "Search result content" | ||
|
|
||
| def test_returns_content_when_all_keywords_match_the_description(self): | ||
| content = "The alpha keyword and beta keyword are both present." | ||
|
|
||
| assert build_search_snippet(content, " alpha beta ") == content | ||
|
|
||
| def test_centers_on_the_first_matching_keyword(self): | ||
| content = "prefix " + ("x" * 140) + " beta " + ("y" * 140) + " alpha" | ||
|
|
||
| snippet = build_search_snippet(content, "alpha beta") | ||
|
|
||
| assert snippet is not None | ||
| assert "beta" in snippet.lower() | ||
|
|
||
| def test_centers_first_match_and_marks_truncated_edges(self): | ||
| content = "prefix " + ("x" * 140) + " keyword " + ("y" * 140) | ||
|
|
||
| snippet = build_search_snippet(content, "KEYWORD") | ||
|
|
||
| assert snippet is not None | ||
| assert "keyword" in snippet.lower() | ||
| assert snippet.startswith("…") | ||
| assert snippet.endswith("…") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
apps/web/core/components/power-k/ui/modal/search-highlight.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /** | ||
| * Copyright (c) 2023-present Plane Software, Inc. and contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-only | ||
| * See the LICENSE file for details. | ||
| */ | ||
|
|
||
| import { Fragment, type ReactNode } from "react"; | ||
|
|
||
| const REGEX_SPECIAL_CHARACTERS = /[.*+?^${}()|[\]\\]/g; | ||
|
|
||
| const escapeRegExp = (value: string) => value.replace(REGEX_SPECIAL_CHARACTERS, "\\$&"); | ||
|
|
||
| const getPartKey = (part: string, partOccurrences: Map<string, number>) => { | ||
| const occurrence = partOccurrences.get(part) ?? 0; | ||
| partOccurrences.set(part, occurrence + 1); | ||
| return `${part}-${occurrence}`; | ||
| }; | ||
|
|
||
| export function highlightSearchMatches(text: string, query: string): ReactNode { | ||
| const normalizedQuery = query.trim().replace(/\s+/g, " "); | ||
| if (!normalizedQuery) return text; | ||
|
|
||
| const queryPattern = new RegExp(`(${escapeRegExp(normalizedQuery)})`, "gi"); | ||
| const normalizedQueryLowerCase = normalizedQuery.toLowerCase(); | ||
| const partOccurrences = new Map<string, number>(); | ||
|
|
||
| return text.split(queryPattern).map((part) => { | ||
| const isMatch = part.toLowerCase() === normalizedQueryLowerCase; | ||
| const key = getPartKey(part, partOccurrences); | ||
|
|
||
| return isMatch ? ( | ||
| <mark key={key} className="bg-transparent font-medium text-accent-primary"> | ||
| {part} | ||
| </mark> | ||
| ) : ( | ||
| <Fragment key={key}>{part}</Fragment> | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| export function highlightSearchKeywords(text: string, query: string): ReactNode { | ||
| const searchTerms = [...new Set(query.trim().split(/\s+/).filter(Boolean))].toSorted( | ||
| (firstTerm, secondTerm) => secondTerm.length - firstTerm.length | ||
| ); | ||
| if (searchTerms.length === 0) return text; | ||
|
|
||
| const queryPattern = new RegExp(`(${searchTerms.map(escapeRegExp).join("|")})`, "gi"); | ||
| const normalizedSearchTerms = new Set(searchTerms.map((term) => term.toLowerCase())); | ||
| const partOccurrences = new Map<string, number>(); | ||
|
|
||
| return text.split(queryPattern).map((part) => { | ||
| const isMatch = normalizedSearchTerms.has(part.toLowerCase()); | ||
| const key = getPartKey(part, partOccurrences); | ||
|
|
||
| return isMatch ? ( | ||
| <mark key={key} className="bg-transparent font-medium text-accent-primary"> | ||
| {part} | ||
| </mark> | ||
| ) : ( | ||
| <Fragment key={key}>{part}</Fragment> | ||
| ); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Restore deterministic ordering before the result limit.
issueshas noorder_by()before[:100]. The database can return matching issues in any order. Relevant issues can disappear from the first 100 results between requests.Apply
order_by("-created_at")beforedistinct()and slicing. This matches the ordering used by the other global-search result groups.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents