Recent Posts
Recent Comments
Link
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

kohigowild

ํŒŒ์ด์–ด๋ฒ ์ด์Šค๋กœ ํ‚ค์›Œ๋“œ ๊ฒ€์ƒ‰ ๊ตฌํ˜„ํ•˜๊ธฐ ๋ณธ๋ฌธ

mitmitwiki

ํŒŒ์ด์–ด๋ฒ ์ด์Šค๋กœ ํ‚ค์›Œ๋“œ ๊ฒ€์ƒ‰ ๊ตฌํ˜„ํ•˜๊ธฐ

kohi โ˜• 2023. 4. 7. 23:08
๐ŸŒผ ํ˜„์žฌ ํŒŒ์ด์–ด์Šคํ† ์–ด์—์„œ๋Š” ์ „์ฒด ํ…์ŠคํŠธ ๊ฒ€์ƒ‰์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค. ํŒŒ์ด์–ด๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๊ฒ€์ƒ‰์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ฟผ๋ฆฌ๋ฌธ์„ ๋ณ€ํ˜•ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ํƒ€์‚ฌ ์†”๋ฃจ์…˜์ธ Algolia๋‚˜ Elastic ๊ฐ™์€ ๊ฒ€์ƒ‰ ์—”์ง„ API๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

ํŒŒ์ด์–ด๋ฒ ์ด์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์œผ๋กœ๋งŒ ํ‚ค์›Œ๋“œ ๊ฒ€์ƒ‰์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์ฟผ๋ฆฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด ๋ณด์ž.

 

๐Ÿ—’๏ธ ๊ธฐ์กด ์ฝ”๋“œ์˜ ๋ฌธ์ œ์ 

const q = query(collection(db, 'feed'), where('address', '>=', keyword), where('address', '<=', keyword + '\uf8ff'));
  const querySnapshot = await getDocs(q);
  setState(
    querySnapshot.docs.map((doc) => ({
      ...doc.data(),
    })),
  );

  • ํ•ด๋‹น ์ฟผ๋ฆฌ๋ฌธ์€ ๋ฌด์กฐ๊ฑด prefix ๊ฒ€์ƒ‰๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • address๊ฐ€ ์ „๋ผ๋‚จ๋„ ์—ฌ์ˆ˜์‹œ์ธ ๋ฐ์ดํ„ฐ์˜ ๊ฒฝ์šฐ “์ „๋ผ๋‚จ๋„” ํ˜น์€ “์ „๋ผ๋‚จ๋„ ์—ฌ์ˆ˜์‹œ” ๋กœ ๊ฒ€์ƒ‰ํ•˜๋ฉด ์กฐํšŒ๊ฐ€ ๊ฐ€๋Šฅํ•œ๋ฐ, “์—ฌ์ˆ˜์‹œ” ๋กœ๋Š” ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์—†๋‹ค.

 

๐Ÿ—’๏ธ ๊ทธ๋ž˜์„œ ์–ด๋–ป๊ฒŒ ํ–ˆ๋ƒ

refer

https://medium.com/@ken11zer01/firebase-firestore-text-search-and-pagination-91a0df8131ef

 

Firebase Firestore Text Search and Pagination

Implementing text search using array-contains and lazy loading.

medium.com

 

1. ๊ฒ€์ƒ‰ ๋Œ€์ƒ์ด ๋˜๋Š” ์ƒ‰์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด์„œ keyword ํ•„๋“œ์— ๋”ฐ๋กœ ์ €์žฅํ•œ๋‹ค. ํ•ด๋‹น ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ๊ฒ€์ƒ‰ ๋Œ€์ƒ์ด ๋˜๋Š” ํ•„๋“œ ๊ฐ’์ธ address, addressDetail์„ ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„๋กœ ์ชผ๊ฐœ ํ•ด๋‹น ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ชจ๋“  ๋ฌธ์„œ์— ์ƒ‰์ธ์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

useEffect(() => {
    setKeyword(address.split(' '));
    {
      addressDetail && setKeyword((prev) => [...prev, addressDetail]);
    }
  }, [address, addressDetail]);

 

2. ๊ฒ€์ƒ‰ ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ array-contains ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 1๋ฒˆ์—์„œ ์ถ”๊ฐ€ํ•œ keyword ํ•„๋“œ์˜ ๋ฐฐ์—ด์— ํ‚ค์›Œ๋“œ๊ฐ€ ํฌํ•จ๋œ ๊ฒฝ์šฐ ๊ฐ’์„ ๋ชจ๋‘ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.

export const searchFeedList = async (keyword: string, setState: any) => {
  const q = query(collection(db, 'feed'), where('keyword', 'array-contains', keyword));
  const querySnapshot = await getDocs(q);
  setState(
    querySnapshot.docs.map((doc) => ({
      ...doc.data(),
    })),
  );
};

 

๐Ÿ—’๏ธ ํ˜„์žฌ ์ฝ”๋“œ์˜ ๋ฌธ์ œ์ 

  • ์ธ๋ฑ์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ ๋””ํ…Œ์ผํ•˜๊ฒŒ ๊ฐ’์„ ์ „๋‹ฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— -postcode๋กœ ๋ฐ›์€ ์ฃผ์†Œ๋ฅผ ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„๋กœ ์ชผ๊ฐ  ๊ฑฐ + ์œ ์ €๊ฐ€ ์ž…๋ ฅํ•œ ์ƒ์„ธ ์ฃผ์†Œ ๋‹จ์œ„๋กœ ์ €์žฅ- ์™„์ „ํ•œ ๊ฒ€์ƒ‰์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
    • ์—ฌ์ˆ˜ ์•ˆ ๋จ ์—ฌ์ˆ˜์‹œ ๋จ
    • ์ „๋‚จ ์•ˆ ๋จ ์ „๋ผ๋‚จ๋„ ๋จ
    • ์ˆ˜์ • ์ „์—๋Š” ๋์—ˆ๋˜ ์ „๋ผ๋‚จ๋„ ์—ฌ์ˆ˜์‹œ๋„ ์•ˆ ๋จใ…‹ใ…‹ใ…‹ใ…‹
  • ํ‚ค์›Œ๋“œ๋ฅผ ์–ด๋–ป๊ฒŒ ๋””ํ…Œ์ผํ•˜๊ฒŒ ๋ฝ€๊ฐœ์„œ ์ €์žฅํ•  ๊ฒƒ์ธ๊ฐ€๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค๋ฉด ์˜คํžˆ๋ ค ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์˜ ์™„์„ฑ๋„๊ฐ€ ๋†’์•„์งˆ ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๊ฐ€ ๋๊ธฐ ๋•Œ๋ฌธ์— ์œ ์˜๋ฏธํ•œ ๋ณ€ํ™”๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค.