Allow disabling advanced search in global search

This commit is contained in:
shamoon 2024-04-09 22:11:52 -07:00
parent 00cfea7f4e
commit 8d7ee4cb1a
7 changed files with 53 additions and 4 deletions

View File

@ -197,6 +197,14 @@
</div> </div>
</div> </div>
<h4 class="mt-4" i18n>Global search</h4>
<div class="row mb-3">
<div class="offset-md-3 col">
<pngx-input-check i18n-title title="Search database only (do not include advanced search results)" formControlName="searchDbOnly"></pngx-input-check>
</div>
</div>
<h4 class="mt-4" i18n>Notes</h4> <h4 class="mt-4" i18n>Notes</h4>
<div class="row mb-3"> <div class="row mb-3">

View File

@ -305,7 +305,7 @@ describe('SettingsComponent', () => {
expect(toastErrorSpy).toHaveBeenCalled() expect(toastErrorSpy).toHaveBeenCalled()
expect(storeSpy).toHaveBeenCalled() expect(storeSpy).toHaveBeenCalled()
expect(appearanceSettingsSpy).not.toHaveBeenCalled() expect(appearanceSettingsSpy).not.toHaveBeenCalled()
expect(setSpy).toHaveBeenCalledTimes(25) expect(setSpy).toHaveBeenCalledTimes(26)
// succeed // succeed
storeSpy.mockReturnValueOnce(of(true)) storeSpy.mockReturnValueOnce(of(true))

View File

@ -99,6 +99,7 @@ export class SettingsComponent
defaultPermsEditUsers: new FormControl(null), defaultPermsEditUsers: new FormControl(null),
defaultPermsEditGroups: new FormControl(null), defaultPermsEditGroups: new FormControl(null),
documentEditingRemoveInboxTags: new FormControl(null), documentEditingRemoveInboxTags: new FormControl(null),
searchDbOnly: new FormControl(null),
notificationsConsumerNewDocument: new FormControl(null), notificationsConsumerNewDocument: new FormControl(null),
notificationsConsumerSuccess: new FormControl(null), notificationsConsumerSuccess: new FormControl(null),
@ -299,6 +300,7 @@ export class SettingsComponent
documentEditingRemoveInboxTags: this.settings.get( documentEditingRemoveInboxTags: this.settings.get(
SETTINGS_KEYS.DOCUMENT_EDITING_REMOVE_INBOX_TAGS SETTINGS_KEYS.DOCUMENT_EDITING_REMOVE_INBOX_TAGS
), ),
searchDbOnly: this.settings.get(SETTINGS_KEYS.SEARCH_DB_ONLY),
savedViews: {}, savedViews: {},
} }
} }
@ -522,6 +524,10 @@ export class SettingsComponent
SETTINGS_KEYS.DOCUMENT_EDITING_REMOVE_INBOX_TAGS, SETTINGS_KEYS.DOCUMENT_EDITING_REMOVE_INBOX_TAGS,
this.settingsForm.value.documentEditingRemoveInboxTags this.settingsForm.value.documentEditingRemoveInboxTags
) )
this.settings.set(
SETTINGS_KEYS.SEARCH_DB_ONLY,
this.settingsForm.value.searchDbOnly
)
this.settings.setLanguage(this.settingsForm.value.displayLanguage) this.settings.setLanguage(this.settingsForm.value.displayLanguage)
this.settings this.settings
.storeSettings() .storeSettings()

View File

@ -56,6 +56,7 @@ export const SETTINGS_KEYS = {
DEFAULT_PERMS_EDIT_GROUPS: 'general-settings:permissions:default-edit-groups', DEFAULT_PERMS_EDIT_GROUPS: 'general-settings:permissions:default-edit-groups',
DOCUMENT_EDITING_REMOVE_INBOX_TAGS: DOCUMENT_EDITING_REMOVE_INBOX_TAGS:
'general-settings:document-editing:remove-inbox-tags', 'general-settings:document-editing:remove-inbox-tags',
SEARCH_DB_ONLY: 'general-settings:search:db-only',
} }
export const SETTINGS: UiSetting[] = [ export const SETTINGS: UiSetting[] = [
@ -219,4 +220,9 @@ export const SETTINGS: UiSetting[] = [
type: 'boolean', type: 'boolean',
default: false, default: false,
}, },
{
key: SETTINGS_KEYS.SEARCH_DB_ONLY,
type: 'boolean',
default: false,
},
] ]

View File

@ -6,10 +6,13 @@ import { Subscription } from 'rxjs'
import { TestBed } from '@angular/core/testing' import { TestBed } from '@angular/core/testing'
import { environment } from 'src/environments/environment' import { environment } from 'src/environments/environment'
import { SearchService } from './search.service' import { SearchService } from './search.service'
import { SettingsService } from '../settings.service'
import { SETTINGS_KEYS } from 'src/app/data/ui-settings'
let httpTestingController: HttpTestingController let httpTestingController: HttpTestingController
let service: SearchService let service: SearchService
let subscription: Subscription let subscription: Subscription
let settingsService: SettingsService
const endpoint = 'search/autocomplete' const endpoint = 'search/autocomplete'
describe('SearchService', () => { describe('SearchService', () => {
@ -20,6 +23,7 @@ describe('SearchService', () => {
}) })
httpTestingController = TestBed.inject(HttpTestingController) httpTestingController = TestBed.inject(HttpTestingController)
settingsService = TestBed.inject(SettingsService)
service = TestBed.inject(SearchService) service = TestBed.inject(SearchService)
}) })
@ -36,4 +40,18 @@ describe('SearchService', () => {
) )
expect(req.request.method).toEqual('GET') expect(req.request.method).toEqual('GET')
}) })
it('should call correct api endpoint on globalSearch', () => {
const query = 'apple'
service.globalSearch(query).subscribe()
httpTestingController.expectOne(
`${environment.apiBaseUrl}search/?query=${query}`
)
settingsService.set(SETTINGS_KEYS.SEARCH_DB_ONLY, true)
subscription = service.globalSearch(query).subscribe()
httpTestingController.expectOne(
`${environment.apiBaseUrl}search/?query=${query}&db_only=true`
)
})
}) })

View File

@ -13,6 +13,8 @@ import { StoragePath } from 'src/app/data/storage-path'
import { Tag } from 'src/app/data/tag' import { Tag } from 'src/app/data/tag'
import { User } from 'src/app/data/user' import { User } from 'src/app/data/user'
import { Workflow } from 'src/app/data/workflow' import { Workflow } from 'src/app/data/workflow'
import { SettingsService } from '../settings.service'
import { SETTINGS_KEYS } from 'src/app/data/ui-settings'
export interface GlobalSearchResult { export interface GlobalSearchResult {
total: number total: number
@ -33,7 +35,10 @@ export interface GlobalSearchResult {
providedIn: 'root', providedIn: 'root',
}) })
export class SearchService { export class SearchService {
constructor(private http: HttpClient) {} constructor(
private http: HttpClient,
private settingsService: SettingsService
) {}
autocomplete(term: string): Observable<string[]> { autocomplete(term: string): Observable<string[]> {
return this.http.get<string[]>( return this.http.get<string[]>(
@ -43,9 +48,13 @@ export class SearchService {
} }
globalSearch(query: string): Observable<GlobalSearchResult> { globalSearch(query: string): Observable<GlobalSearchResult> {
let params = new HttpParams().set('query', query)
if (this.settingsService.get(SETTINGS_KEYS.SEARCH_DB_ONLY)) {
params = params.set('db_only', true)
}
return this.http.get<GlobalSearchResult>( return this.http.get<GlobalSearchResult>(
`${environment.apiBaseUrl}search/`, `${environment.apiBaseUrl}search/`,
{ params: new HttpParams().set('query', query) } { params }
) )
} }
} }

View File

@ -1110,6 +1110,8 @@ class GlobalSearchView(PassUserMixin):
elif len(query) < 3: elif len(query) < 3:
return HttpResponseBadRequest("Query must be at least 3 characters") return HttpResponseBadRequest("Query must be at least 3 characters")
db_only = request.query_params.get("db_only", False)
OBJECT_LIMIT = 3 OBJECT_LIMIT = 3
docs = [] docs = []
if request.user.has_perm("documents.view_document"): if request.user.has_perm("documents.view_document"):
@ -1120,7 +1122,7 @@ class GlobalSearchView(PassUserMixin):
) )
# First search by title # First search by title
docs = all_docs.filter(title__icontains=query)[:OBJECT_LIMIT] docs = all_docs.filter(title__icontains=query)[:OBJECT_LIMIT]
if len(docs) < OBJECT_LIMIT: if not db_only and len(docs) < OBJECT_LIMIT:
# If we don't have enough results, search by content # If we don't have enough results, search by content
from documents import index from documents import index