Repository: DDMAL/CantusDB

Issue:
keyword/search_bar search give different results
If I search for "urbs" in the keyword search of Chant search, I get 318 results:
http://206.12.88.113/chant-search/?op=contains&keyword=urbs&office=&genre=&cantus_id=&mode=&feast=&position=&melodies=
![image](https://user-images.githubusercontent.com/67451875/211896254-5dad814c-8d9e-49a0-a404-2f20b68106f5.png)


BUT if I search the same word in the little magnifying-glass box on the upper right, I now get only 256: http://206.12.88.113/chant-search/?search_bar=urbs 
![image](https://user-images.githubusercontent.com/67451875/211896569-7dc57a16-883f-42ec-ad58-88886745bc7d.png)


If the two searches are going to work differently, it needs to be made obvious somewhere what is different, so people know which one to use.


Discussion:
Possibly some or all of the discrepancy can be explained by published/unpublished status. It seems as if the results in the longer list (=keyword) come from unpublished sources that don't come up in the search bar. 
I don't (personally) mind having a search that does that but it should probably be an opt-in toggle--i.e. you get 256 results in both cases UNLESS you choose some sort of "also search sources still being edited". Otherwise it's just confusing. 

(It's also confusing that in both cases the searches are fuzzy in a way that's hard to predict, but that is an issue discussed in #419 )
Investigating another search-related issue recently, I noticed the `?search_bar` in the url - I don't know why we have both `?search_bar=` and `?keyword=`, and my guess is that we're handling the search differently depending on which query parameter is used. We should probably just use `?keyword` (which would, I think, make the keyword appear in the "keyword" field as well, which is what I expect should happen anyways...)
for future Jacob: `search_bar` comes from `main_app/templates/global_search_bar.js`.

Changing `name="search_bar"` to `name="keyword"` causes the searched keyword to appear in the keyword field on the search page when one searches via the search bar, but breaks the functionality where users can search for Cantus IDs using the search bar.
as I just noted on #515, `?search_bar=` only looks at standard-spelling full text, while `?keyword=` searches all three text fields: standard-spelling, ms-spelling, and incipit. This likely accounts for the difference.
A possible enhancement: It could be nice to give users control over which text fields get searched when using `?keyword`. There could be a section of the search page that says "Search for keyword in the following fields:", followed by three checkboxes, one for ms-spelling, one for std-spelling, and one for incipit.
Something is still strange about this. Searching "Deo" in the search presented on the home page on production (the first one most users will use) gives only 138 chants, where on OldCantus you get 274 (logged out, in both cases). Keyword search on NewCantus gives 248, which I could attribute to data/pulication-status discrepancy, but I assume the other 110 chants are lost because oldCantus searches incipit and NewCantus does not. This needs to be brought into alignment.

<img width="1150" alt="image" src="https://github.com/DDMAL/CantusDB/assets/67451875/b9066fb1-434c-4ee6-918a-32aaa111e438">
<img width="993" alt="image" src="https://github.com/DDMAL/CantusDB/assets/67451875/90a6ebb9-b9e1-41a2-8f4f-c57cc37032aa">


Resolving patch:
diff --git a/django/cantusdb_project/main_app/views/chant.py b/django/cantusdb_project/main_app/views/chant.py
--- a/django/cantusdb_project/main_app/views/chant.py
+++ b/django/cantusdb_project/main_app/views/chant.py
@@ -750,17 +750,29 @@ def get_queryset(self) -> QuerySet:
         display_unpublished = self.request.user.is_authenticated
         # if the search is accessed by the global search bar
         if self.request.GET.get("search_bar"):
-            chant_set = Chant.objects.filter(source__published=True)
-            sequence_set = Sequence.objects.filter(source__published=True)
+            if display_unpublished:
+                chant_set = Chant.objects.all()
+                sequence_set = Sequence.objects.all()
+            else:
+                chant_set = Chant.objects.filter(source__published=True)
+                sequence_set = Sequence.objects.filter(source__published=True)
             if self.request.GET.get("search_bar").replace(" ", "").isalpha():
                 # if search bar is doing incipit search
-                incipit = self.request.GET.get("search_bar")
-                chant_set = chant_set.filter(
-                    manuscript_full_text_std_spelling__istartswith=incipit
-                ).values(*CHANT_SEARCH_TEMPLATE_VALUES)
-                sequence_set = sequence_set.filter(
-                    manuscript_full_text_std_spelling__istartswith=incipit
-                ).values(*CHANT_SEARCH_TEMPLATE_VALUES)
+                search_term = self.request.GET.get("search_bar")
+                ms_spelling_filter = Q(manuscript_full_text__istartswith=search_term)
+                std_spelling_filter = Q(
+                    manuscript_full_text_std_spelling__istartswith=search_term
+                )
+                incipit_filter = Q(incipit__istartswith=search_term)
+                search_term_filter = (
+                    ms_spelling_filter | std_spelling_filter | incipit_filter
+                )
+                chant_set = chant_set.filter(search_term_filter).values(
+                    *CHANT_SEARCH_TEMPLATE_VALUES
+                )
+                sequence_set = sequence_set.filter(search_term_filter).values(
+                    *CHANT_SEARCH_TEMPLATE_VALUES
+                )
                 queryset = chant_set.union(sequence_set, all=True)
             else:
                 # if search bar is doing Cantus ID search


Test patch: