Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Redis

ปลดล็อค RediSearch: Advanced Indexing สำหรับ Powerful Redis Queries

Redis มีชุด data structures ที่ versatile ตั้งแต่ Strings เรียบง่าย จนถึง powerful abstractions อย่าง Redis Streams Native data types พาคุณไปไกลได้ แต่มี use cases บางอย่างที่อาจต้องการ workaround ตัวอย่างคือ requirement ใช้ secondary indexes ใน Redis เพื่อไป beyond key-based search/lookup เพื่อ richer query capabilities แม้คุณใช้ Sorted Sets, Lists ฯลฯ ทำได้ คุณต้อง factor in trade-offs บางอย่าง

เข้ามา RediSearch! Available เป็น Redis module RediSearch ให้ flexible search capabilities ขอบคุณ first-class secondary indexing engine มันเสนอ powerful features เช่น full-text Search, auto completion, geographical indexing และอีกมากมาย

เพื่อสาธิตพลังของ RediSearch บล็อกนี้เสนอตัวอย่างปฏิบัติจริงการใช้ RediSearch กับ Azure Cache for Redis ด้วยความช่วยเหลือของ Go service สร้างด้วย RediSearch Go client ออกแบบมาให้คุณได้ set ของ applications ที่ ingest tweets แบบ real-time และ query ได้อย่างยืดหยุ่นด้วย RediSearch

โดยเฉพาะ คุณจะเรียนรู้วิธี:

  • ทำงานกับ RediSearch indexes
  • ใช้ RediSearch data types ต่างๆ เช่น TEXT, NUMERIC, TAG และอื่นๆ
  • วิธีสร้างแอปพลิเคชันแสดง RediSearch capabilities
  • วิธีปรับใช้ service components ไป Azure ด้วยเพียงไม่กี่คำสั่ง
  • วิเคราะห์ tweet data ด้วยการ query RediSearch

Application overview

какกล่าว ตัวอย่าง service ให้คุณ consume tweets แบบ real-time และทำให้พร้อมสำหรับ querying ผ่าน RediSearch

ปลดล็อค RediSearch: Advanced Indexing สำหรับ Powerful Redis Queries

มี 2 components:

  1. Consumer/Indexer: อ่านจาก Twitter Streaming API สร้าง index และเพิ่ม tweet data (ใน Redis HASHes) ต่อเนื่องเมื่อมาถึง
  2. Search service: REST API ให้คุณ search tweets ด้วย RediSearch query syntax

ในจุดนี้ เราจะดำดิ่งสู่วิธี get solution up and running ให้คุณเห็นใน action อย่างไร อย่างไรก็ตาม หากสนใจเข้าใจว่าองค์ประกอบแต่ละตัวทำงานอย่างไร โปรดดู Code walk through section ด้านล่าง และ GitHub repo สำหรับบล็อกนี้: https://github.com/abhirockzz/redisearch-tweet-analysis

Prerequisites

  1. เริ่มต้น คุณต้องการ Microsoft Azure account: สร้างฟรีได้ที่นี่!
  2. Service components ด้านบนจะปรับใช้ไป Azure Container Instances ใช้ native Docker CLI commands ความสามารถนี้ enabled โดย integration ระหว่าง Docker และ Azure
  3. คุณต้องการ Docker Desktop version 2.3.0.5 หรือใหม่กว่า สำหรับ Windows, macOS หรือติดตั้ง Docker ACI Integration CLI สำหรับ Linux เพื่อใช้ Twitter Streaming API คุณยังต้องการ Twitter developer account หากยังไม่มี โปรดทำตามคำแนะนำเหล่านี้ RediSearch in action!

เริ่มด้วย quick-start tutorial นี้เพื่อตั้งค่า Redis Enterprise tier cache บน Azure เสร็จ setup ให้แน่ใจว่าคุณมี Redis host name และ access key อยู่มือ:

ปลดล็อค RediSearch: Advanced Indexing สำหรับ Powerful Redis Queries

ทั้ง 2 components ของ service เรา available เป็น Docker containers: Tweet indexing service และ Search API service (หากต้องสร้าง Docker images เอง ใช้ Dockerfile แต่ละตัวบน GitHub repo)

ตอนนี้คุณจะเห็นว่าปรับใช้ไป Azure Container Instances สะดวกแค่ไหน ซึ่งรัน Docker containers on-demand ใน managed serverless Azure environment

Deploy to Azure

ไฟล์ docker-compose.yml กำหนด components (tweets-search และ tweets-indexer) ทั้งหมดที่คุณต้องทำคือ update มันเพื่อแทนที่ค่าสำหรับ Azure Redis instance ของคุณ และ Twitter developer account credentials นี่คือไฟล์ทั้งหมด:

Create an Azure context:

Clone the GitHub repo:

Deploy both the service components as part of a container group:

(Note that Docker Compose commands currently available in an ACI context start with docker compose. That is NOT the same as docker-compose with a hyphen.)

คุณจะเห็น output คล้ายนี้:

รอ services เริ่มต้น คุณยัง check Azure portal ได้ เมื่อทั้ง 2 services up and running แล้ว คุณ check logs ของแต่ละ service:

ถ้าทุกอย่าง順利 tweet-consumer service ควร kicked off มันจะอ่าน stream ของ tweets และ persist ไป Redis

The moment of truth!

ถึงเวลา query tweet data เพื่อทำเช่นนั้น คุณเข้าถึง REST API ใน Azure Container Instances ด้วย IP address และ fully qualified domain name (FQDN) (อ่านเพิ่มใน Container Access) หา IP รัน docker ps และ check PORTS section ใน output (แสดงด้านล่าง):

ตอนนี้คุณรัน queries ทุกประเภทได้! ก่อนดำดิ่ง นี่คือ quick idea ของ indexed attributes ที่ใช้ใน search queries ได้:

(Note, I use curl ในตัวอย่างด้านล่าง แต่แนะนำ "REST Client" สำหรับ VS Code)

Set base URL สำหรับ search service API:

เริ่มง่าย query all documents (ใช้ * ):

คุณจะเห็น output คล้ายนี้:

สังเกต headers Page-Size และ Search-Hits: นี่คือ custom headers ส่งจาก application เพื่อ demonstrate pagination และ limits ตอบสนอง "get me all the documents" query เราพบ 12 results ใน Redis แต JSON body return 10 entries เพราะ default behavior ของ RediSearch Go API ซึ่งเปลี่ยนได้ด้วย query parameters ต่างๆ เช่น:

หรือ เช่น search tweets ส่งจาก iPhone:

คุณอาจไม่ต้องการ attributes ทั้งหมดใน query result ตัวอย่าง เช่น วิธีได้แค่ user (Twitter screen name) และ tweet text:

Query บน user name (เช่น เริ่มต้นด้วย jo):

คุณยังใช้ combination ของ attributes ใน query ได้:

ลองดู tweets ที่มี hashtags เฉพาะ? สามารถใช้ multiple hashtags (คั่นด้วย |) ได้?

อยากรู้ว่ามี tweets กี่อันที่มี hashtag biden สร้าง gầnๆ นี้? ใช้ range query:

หากโชคดีได้ coordinates info บน tweets คุณลอง extract พวกมัน และ query บน attribute coordinates:

นี่แค่ตัวอย่างเล็กน้อย รู้สึกอิสระ experiment เพิ่ม และลอง queries อื่นๆ Section นี้ใน RediSearch documentation อาจช่วย!

Important: หลังจบ อย่าลืมหยุด services และ containers ใน Azure Container Instances:

ใช้ Azure Portal ลบ Azure Redis instance ที่สร้าง

Code walk through

Section นี้ให้ high-level overview ของโค้ดสำหรับ components แต่ละตัว ควรทำให้ navigate source code ใน GitHub repo ง่ายขึ้น

Tweets consumer/indexer:

go-twitter library ใช้ interact กับ Twitter

มัน authenticate ไป Twitter Streaming API:

และ listen to stream ของ tweets ใน goroutine แยก:

สังเกต go index.AddData(tweetToMap(tweet))— นี่คือที่ indexing component ถูก invoke มัน connect ไป Azure Cache for Redis:

มัน drop index (และ existing documents ด้วย) ก่อน re-create:

Index และ associated documents ถูก drop เพื่อให้คุณเริ่ม clean state ทำให้ experiment/demo ง่ายขึ้น คุณ comment out ส่วนนี้ได้ถ้าอยาก

Information สำหรับแต่ละ tweet เก็บใน HASH (named tweet:<tweet ID>) ใช้ operation HSET:

Tweets search expose REST API query RediSearch All options (รวม query ฯลฯ) ส่งในรูป query parameters ตัวอย่าง https://localhost:8080/search?q=@source:iphone มัน extract required query parameters:

q parameter mandatory แต่คุณใช้ parameters ต่อไปนี้สำหรับ search ได้:

  • fields: เพื่อระบุ attributes ที่อยาก return ใน result, และ
  • offset_limit: หากอยากระบุ offset จากที่อยากค้นหา และจำนวน documents ที่อยาก include ใน result (by default offset 0 และ limit 10 — ตาม RediSearch Go client)

ตัวอย่าง:

สุดท้าย results iterated over และส่งกลับเป็น JSON (array of documents):

Section นี้จบ!

Redis Enterprise tiers บน Azure Cache for Redis

Redis Enterprise available เป็น native service บน Azure ในรูป 2 tiers ใหม่สำหรับ Azure Cache for Redis ซึ่ง operated และ supported โดย Microsoft และ Redis Service นี้ให้นักพัฒนาเข้าถึง rich set ของ Redis Enterprise features รวม modules อย่าง RediSearch สำหรับข้อมูลเพิ่มเติม ดู resources นี้:

  • Azure Cache for Redis, Enterprise Tiers Are Now Generally Available
  • Deepening Our Partnership with Microsoft to Grow Redis Enterprise in the Cloud
  • Microsoft and Redis collaborate to give developers new Azure Cache for Redis capabilities
  • Redis Enterprise features on Azure Cache for Redis

Conclusion

End-to-end application นี้สาธิตวิธีทำงานกับ indexes, ingest real-time data สร้าง documents (tweet information) indexed โดย RediSearch engine และใช้ versatile query syntax ดึง insights บน tweets เหล่านั้น

อยากเข้าใจว่ากลับหลังเกิดอะไรเมื่อคุณ search topic บน Redis documentation? ดูบล็อกนี้เรียนรู้ว่า Redis site incorporated full-text search ด้วย RediSearch! หรืออาจสนใจสำรวจใช้ RediSearch ใน serverless application?

หากเพิ่งเริ่ม เยี่ยม RediSearch Quick Start page

หากอยากเรียนรู้ enterprise capabilities ใน Azure Cache for Redis เพิ่มเติม ดู resources ต่อไปนี้:

  • Multi Zone redundancy และวิธีตั้งค่า
  • Private Link support สำหรับ network isolation
  • Choosing the right caching tier
  • High availability สำหรับ Enterprise tiers