Repository: apache/airflow

Issue:
Airflow 2.3.0: can't filter by owner if selected from dropdown
### Apache Airflow version

2.3.0 (latest released)

### What happened

On a clean install of 2.3.0, whenever I try to filter by owner, if I select it from the dropdown (which correctly detects the owner's name) it returns the following error:

`DAG "ecodina" seems to be missing from DagBag.`
Webserver's log:
```
127.0.0.1 - - [12/May/2022:12:27:47 +0000] "GET /dagmodel/autocomplete?query=ecodin&status=all HTTP/1.1" 200 17 "http://localhost/home?search=ecodina" "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0"
127.0.0.1 - - [12/May/2022:12:27:50 +0000] "GET /dags/ecodina/grid?search=ecodina HTTP/1.1" 302 217 "http://localhost/home?search=ecodina" "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0"
127.0.0.1 - - [12/May/2022:12:27:50 +0000] "GET /home HTTP/1.1" 200 35774 "http://localhost/home?search=ecodina" "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0"
127.0.0.1 - - [12/May/2022:12:27:50 +0000] "POST /blocked HTTP/1.1" 200 2 "http://localhost/home" "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0"
127.0.0.1 - - [12/May/2022:12:27:50 +0000] "POST /last_dagruns HTTP/1.1" 200 402 "http://localhost/home" "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0"
127.0.0.1 - - [12/May/2022:12:27:50 +0000] "POST /dag_stats HTTP/1.1" 200 333 "http://localhost/home" "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0"
127.0.0.1 - - [12/May/2022:12:27:50 +0000] "POST /task_stats HTTP/1.1" 200 1194 "http://localhost/home" "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0"
```

Instead, if I write the owner's name fully and avoid selecting it from the dropdown, it works as expected since it constructs the correct URL:

`my.airflow.com/home?search=ecodina`

### What you think should happen instead

The DAGs table should only show the selected owner's DAGs.

### How to reproduce

- Start the Airflow Webserver
- Connect to the Airflow webpage
- Type an owner name in the _Search DAGs_ textbox and select it from the dropdown

### Operating System

CentOS Linux 8

### Versions of Apache Airflow Providers

_No response_

### Deployment

Other

### Deployment details

Installed on a conda environment, as if it was a virtualenv:

- `conda create -c conda-forge -n airflow python=3.9`
- `conda activate airflow`
- `pip install "apache-airflow[postgres]==2.3.0" --constraint "https://raw.githubusercontent.com/apache/airflow/constraints-2.3.0/constraints-3.9.txt"`

Database: PostgreSQL 13


### Anything else

_No response_

### Are you willing to submit PR?

- [ ] Yes I am willing to submit a PR!

### Code of Conduct

- [x] I agree to follow this project's [Code of Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)



Discussion:
Thanks for opening your first issue here! Be sure to follow the issue template!

I can reproduce it on main branch.
I didn't know we can search for owners in "Search DAGs" box? If so then the text in the box needs to be edited.
Choosing owner there indeed doens't work

It raised error:
`DAG "airflow" seems to be missing from DagBag.`

![Screen Shot 2022-05-12 at 19 15 50](https://user-images.githubusercontent.com/45845474/168121516-205ce613-5abc-41c4-96d1-6acdb50b6b22.png)


![Screen Shot 2022-05-12 at 19 15 36](https://user-images.githubusercontent.com/45845474/168121538-452b57fb-8087-441e-8359-d21ceb051b6d.png)


As a workaround you can filter by using the clicking on owner as added in https://github.com/apache/airflow/pull/11121
 it uses `/home?search=airflow` and filter the DAGs by owner as expected.


I accidentally found we could filter by owner from this textbox on Airflow 2.2.5, where it worked as expected. Thanks for mentioning the workaround!
Yes, in 2.2.5 clicking on an autocompletion match would take you to home-with-search and in 2.3.0 it tries to take you to the matching DAG. Except it's not a DAG. Now if I can just track down the code that does that I can fix it. :)

Resolving patch:
diff --git a/airflow/www/views.py b/airflow/www/views.py
--- a/airflow/www/views.py
+++ b/airflow/www/views.py
@@ -5127,12 +5127,18 @@ def autocomplete(self, session=None):
             return wwwutils.json_response([])
 
         # Provide suggestions of dag_ids and owners
-        dag_ids_query = session.query(DagModel.dag_id.label('item')).filter(
-            ~DagModel.is_subdag, DagModel.is_active, DagModel.dag_id.ilike('%' + query + '%')
-        )
+        dag_ids_query = session.query(
+            sqla.literal('dag').label('type'),
+            DagModel.dag_id.label('name'),
+        ).filter(~DagModel.is_subdag, DagModel.is_active, DagModel.dag_id.ilike('%' + query + '%'))
 
-        owners_query = session.query(func.distinct(DagModel.owners).label('item')).filter(
-            ~DagModel.is_subdag, DagModel.is_active, DagModel.owners.ilike('%' + query + '%')
+        owners_query = (
+            session.query(
+                sqla.literal('owner').label('type'),
+                DagModel.owners.label('name'),
+            )
+            .distinct()
+            .filter(~DagModel.is_subdag, DagModel.is_active, DagModel.owners.ilike('%' + query + '%'))
         )
 
         # Hide DAGs if not showing status: "all"
@@ -5149,8 +5155,9 @@ def autocomplete(self, session=None):
         dag_ids_query = dag_ids_query.filter(DagModel.dag_id.in_(filter_dag_ids))
         owners_query = owners_query.filter(DagModel.dag_id.in_(filter_dag_ids))
 
-        payload = [row[0] for row in dag_ids_query.union(owners_query).limit(10).all()]
-
+        payload = [
+            row._asdict() for row in dag_ids_query.union(owners_query).order_by('name').limit(10).all()
+        ]
         return wwwutils.json_response(payload)
 
 


Test patch:
diff --git a/tests/www/views/test_views_acl.py b/tests/www/views/test_views_acl.py
--- a/tests/www/views/test_views_acl.py
+++ b/tests/www/views/test_views_acl.py
@@ -244,11 +244,15 @@ def test_index_failure(dag_test_client):
 
 def test_dag_autocomplete_success(client_all_dags):
     resp = client_all_dags.get(
-        'dagmodel/autocomplete?query=example_bash',
+        'dagmodel/autocomplete?query=flow',
         follow_redirects=False,
     )
-    check_content_in_response('example_bash_operator', resp)
-    check_content_not_in_response('example_subdag_operator', resp)
+    assert resp.json == [
+        {'name': 'airflow', 'type': 'owner'},
+        {'name': 'test_mapped_taskflow', 'type': 'dag'},
+        {'name': 'tutorial_taskflow_api_etl', 'type': 'dag'},
+        {'name': 'tutorial_taskflow_api_etl_virtualenv', 'type': 'dag'},
+    ]
 
 
 @pytest.mark.parametrize(