farming-paper / farming-paper

farming-paper.vercel.app
1 stars 0 forks source link

Add search feature #105

Open echoja opened 6 months ago

echoja commented 6 months ago

https://velog.io/@seunghyun-bloom/MongoDB-Atlas-Search-%ED%95%9C%EA%B8%80-%EA%B2%80%EC%83%89-%EC%84%A4%EC%A0%95

LET'S DO IT!

   const connectionString = 'mongodb+srv://seunghyun_bloomlab:(여기에 비밀번호를 입력)@cluster0.36jdvjx.mongodb.net/?retryWrites=true&w=majority';
    const mongoClient = new MongoClient(connectionString);
    const idolCollection = mongoClient.db('sample_search').collection('idols')

    return idolCollection.aggregate([
      {
        $search: {
          index: 'idol_search_index', // 사용할 search index 의 index명 입력
          compound: { // 다중 조건 Querying 할 때 쓰는 operator
            should: [
              {
                text: {
                  query: searchValue,
                  path: "name",
                  score: { constant: { value: 10 } }, // 이름에 검색어가 있으면 10점
                },
              },
              {
                text: {
                  query: searchValue,
                  path: "gruop",
                  score: { constant: { value: 5 } }, // 그룹명에 검색어가 있으면 5점
                },
              },
              {
                text: {
                  query: searchValue,
                  path: "introduction",
                  score: { constant: { value: 3 } }, // 소개글에 검색어가 있으면 3점
                },
              },
              {
                text: {
                  query: searchValue,
                  path: "favorite_food",
                  score: { constant: { value: 1 } }, // 최애음식 목록에 검색어가 있으면 1점
                },
              },
            ],
            minimumShouldMatch: 1, // 최소 하나는 일치해야 리턴
          },
        },
      },
      {
        $project: { // 표시할 데이터 정의 (1 이면 보이고, 0이면 보이지 않음)
          _id: 0,
          name: 1,
          group: 1,
          introduction: 1,
          favorite_food: 1,
          score: { $meta: "searchScore" }
        },
      },
    ]).toArray();
echoja commented 1 month ago

need to look for some algorithms related to syncing questions.