Repository: LemmyNet/lemmy (language: rust)

Issue:
Include image alt text in post search
### Requirements

- [x] Is this a feature request? For questions or discussions use https://lemmy.ml/c/lemmy_support
- [x] Did you check to see if this issue already exists?
- [x] Is this only a feature request? Do not put multiple feature requests in one issue.
- [x] Is this a backend issue? Use the [lemmy-ui](https://github.com/LemmyNet/lemmy-ui) repo for UI / frontend issues.
- [x] Do you agree to follow the rules in our [Code of Conduct](https://join-lemmy.org/docs/code_of_conduct.html)?

### Is your proposal related to a problem?

Images are hard to find, they often aren't well described in the post title or body. The only field that is really meant for image descriptions is the alt text field.

### Describe the solution you'd like.

Image posts should be returned if the search query matches the alt text of an image

### Describe alternatives you've considered.

Implement a dedicated image search feature?

### Additional context

_No response_


Discussion / hints:
This is easy to add, only requires a single line added here: https://github.com/LemmyNet/lemmy/blob/main/crates/db_views/src/post/post_view.rs#L491

Fix patch:
diff --git a/crates/db_views/src/post/post_view.rs b/crates/db_views/src/post/post_view.rs
index 68c838f8af..8539896213 100644
--- a/crates/db_views/src/post/post_view.rs
+++ b/crates/db_views/src/post/post_view.rs
@@ -489,10 +489,11 @@ impl<'a> PostQuery<'a> {
         let searcher = fuzzy_search(search_term);
         let name_filter = post::name.ilike(searcher.clone());
         let body_filter = post::body.ilike(searcher.clone());
+        let alt_text_filter = post::alt_text.ilike(searcher.clone());
         query = if o.title_only.unwrap_or_default() {
           query.filter(name_filter)
         } else {
-          query.filter(name_filter.or(body_filter))
+          query.filter(name_filter.or(body_filter).or(alt_text_filter))
         }
         .filter(not(post::removed.or(post::deleted)));
       }
diff --git a/migrations/2025-02-24-173152_search-alt-text-of-posts/down.sql b/migrations/2025-02-24-173152_search-alt-text-of-posts/down.sql
new file mode 100644
index 0000000000..5d1b726118
--- /dev/null
+++ b/migrations/2025-02-24-173152_search-alt-text-of-posts/down.sql
@@ -0,0 +1,4 @@
+DROP INDEX idx_post_trigram;
+
+CREATE INDEX IF NOT EXISTS idx_post_trigram ON post USING gin (name gin_trgm_ops, body gin_trgm_ops);
+
diff --git a/migrations/2025-02-24-173152_search-alt-text-of-posts/up.sql b/migrations/2025-02-24-173152_search-alt-text-of-posts/up.sql
new file mode 100644
index 0000000000..21eb206337
--- /dev/null
+++ b/migrations/2025-02-24-173152_search-alt-text-of-posts/up.sql
@@ -0,0 +1,4 @@
+DROP INDEX idx_post_trigram;
+
+CREATE INDEX IF NOT EXISTS idx_post_trigram ON post USING gin (name gin_trgm_ops, body gin_trgm_ops, alt_text gin_trgm_ops);
+


Test patch: