mavixM
Convex Community9mo ago
9 replies
mavix

Searching posts that contains "#" symbol does not work

I'm trying to make a query that returns a list of hasthags based on the current posts, so I could get the trending topics. I have the following query:

export const getHashtagWordsWithFilterAndCount = query({
  args: {
    limit: v.optional(v.number()),
  },
  handler: async (ctx, args) => {
    // Get all posts that contain hashtags
    const posts = await ctx.db
      .query("posts")
      .withSearchIndex("search_post", (q) => q.search("content", "#"))
      .collect();
    // ^ This does not return anything

    // Create a map to count hashtag occurrences
    const hashtagCountMap = new Map<string, number>();

    // Process each post to extract hashtags
    posts.forEach((post) => {
      // Use regex to find all hashtags in the content
      const hashtagRegex = /#[\w\u0590-\u05ff]+/g;
      const hashtags = post.content.match(hashtagRegex) || [];

      // Count each hashtag
      hashtags.forEach((hashtag) => {
        const normalizedHashtag = hashtag.toLowerCase();
        hashtagCountMap.set(
          normalizedHashtag,
          (hashtagCountMap.get(normalizedHashtag) || 0) + 1,
        );
      });
    });

    // Convert map to array and sort by count
    const hashtagCountArray: HashtagCount[] = Array.from(
      hashtagCountMap,
      ([hashtag, count]) => ({ hashtag, count }),
    ).sort((a, b) => b.count - a.count);

    // Apply limit if provided
    return args.limit
      ? hashtagCountArray.slice(0, args.limit)
      : hashtagCountArray;
  },
});


But is not returning anything, even though there are posts that contains "#" in their content, as shown in the attached image. I don't know if has something to do with the character itself, because if I change the search term, for let's say, the character 'b', it does work.
image.png
Was this page helpful?