검색 기능 만들기

2024년 6월 11일 화요일

Today I Learned

날짜

2024년 6월 11일 화요일

내용

검색 쿼리

유저가 여러 댓글 중 무언가를 검색하거나, 데이터를 정렬하는 기능을 만들어야 했다. 기존 프로덕트 리뷰에 있는 건 너어어어무 복잡하고, 온갖 기능이 추가되어 유지보수가 힘들었다. 아예 다른 곳에서 쓰이는 것일 뿐더러 기능이나 검색 단위, 정렬 단위도 더 작은 범위라 최대한 깔끔하게 만들려고 했다.

정렬 방식이 조금 난해했는데, 정렬 기준이 2개인 경우가 있었기 떄문이다. 이번 인스타그램 댓글 가져오는 앱의 테이블 구조는 샵 → 페이스북 페이지 → 게시글 → 댓글 순서로 이루어졌으며 각각 일대다관계다. 댓글의 작성일자로 정렬할 때는 별 어려움이 없으나, 게시글 작성일자로 정렬할 떄는 꽤 어려웠다. 최근에 작성된 게시글 내의 댓글들을 먼저 가져오되, 같은 게시글 내에선 최근에 작성된 댓글이 먼저와야 했다.

고민이 무색할정도로 쉽게 해결되었는데, order_by 에 넣는 순서로 해결되버렸다.. 페이지네이션을 위해 전체 데이터 갯수를 조회해야 했는데 하나의 쿼리로 처리할 수 없었다. 그냥 별개의 쿼리를 작성해서 데이터베이스에 2번 접근(데이터 조회를 위해 한번, 데이터 갯수를 세기 위해 한번)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def get_all_instagram_comments(
    shop_platform_id: int,
    skip: int = 0,
    limit: int = 10,
    search_for: Optional[str] = None,
    search_keyword: Optional[str] = None,
    sort_by: Optional[str] = "newest_posts",
    is_published: Optional[bool] = False,
    db: Session = Depends(get_db),
):
    # 기본 쿼리 설정
    stmt = (
        select(models.InstagramCommentsComment)
        .join(models.InstagramCommentsComment.instagram_comments_media)
        .join(models.InstagramCommentsMedia.instagram_comments_facebook_page)
        .join(models.InstagramCommentsFacebookPage.instagram_comments_shop)
        .where(
            models.InstagramCommentsShop.shop_platform_id == shop_platform_id
            and models.InstagramCommentsFacebookPage.is_selected == True
        )
    )

    count_stmt = (
        select(func.count())
        .join(models.InstagramCommentsComment.instagram_comments_media)
        .join(models.InstagramCommentsMedia.instagram_comments_facebook_page)
        .join(models.InstagramCommentsFacebookPage.instagram_comments_shop)
        .where(
            models.InstagramCommentsShop.shop_platform_id == shop_platform_id
            and models.InstagramCommentsFacebookPage.is_selected == True
        )
    )

    # 검색 조건 추가
    if search_keyword:
		    # 댓글 작성자명을 검색할 때
        if search_for == "username":
            stmt = stmt.where(
                models.InstagramCommentsComment.username.like(f"%{search_keyword}%")
            )
            count_stmt = count_stmt.where(
                models.InstagramCommentsComment.username.like(f"%{search_keyword}%")
            )
        # 댓글 내용을 검색할 때
        elif search_for == "content":
            stmt = stmt.where(
                models.InstagramCommentsComment.text.like(f"%{search_keyword}%")
            )
            count_stmt = count_stmt.where(
                models.InstagramCommentsComment.text.like(f"%{search_keyword}%")
            )

    # 게시물 및 코멘트 정렬 기준 설정
    if sort_by == "newest_comments":
        stmt = stmt.order_by(models.InstagramCommentsComment.timestamp.desc())
    elif sort_by == "oldest_comments":
        stmt = stmt.order_by(models.InstagramCommentsComment.timestamp.asc())
    elif sort_by == "newest_posts":
        stmt = stmt.order_by(
            models.InstagramCommentsMedia.timestamp.desc(),
            models.InstagramCommentsComment.timestamp.desc(),
        )
    elif sort_by == "oldest_posts":
        stmt = stmt.order_by(
            models.InstagramCommentsMedia.timestamp.asc(),
            models.InstagramCommentsComment.timestamp.desc(),
        )

    # 게시 여부 필터 추가
    if is_published:
        stmt = stmt.where(models.InstagramCommentsComment.is_published == True)
        count_stmt = count_stmt.where(
            models.InstagramCommentsComment.is_published == True
        )

    # 페이징 설정
    stmt = stmt.offset(skip).limit(limit)

    # 쿼리 실행
    result = db.execute(stmt).scalars().all()
    comments = []
    for c in result:
        comment = InstagramCommentsCommentOutput.from_orm(c)
        comments.append(comment)

    length = db.execute(count_stmt).scalar()

    return {
        "data": comments,
        "length": length,
    }

회고

사랑니 에반데.