ShinZ
ShinZ
Jul 24, 2017 · 1 min read

ค้นหาภาษาไทยด้วย Django Haystack + Elasticsearch

Software ที่ใช้ในการทดลองนี้คือ

Django==1.8.11
django-haystack==2.6.1
elasticsearch==1.6.0
elasticstack==0.5.0

ในไฟล์ settings.py ตั้งค่า Haystack เป็น

settings.py

ตัว Engine จะใช้ ConfigurableElasticSearchEngine จาก elasticstack เพื่อให้สามารถตั้งค่า Analyzer ได้เอง

ตัว INDEX_NAME คือชื่อของ Index ซึ่งเราสามารถเปลี่ยนเป็นชื่ออื่นได้

settings.py

จากนั้นเพิ่ม ELASTICSEARCH_INDEX_SETTINGS ซึ่งเป็นการตั้งค่า Analyzer ภาษาไทยลงไป

โดยให้ใช้การตัดคำเป็นภาษาไทย (จากบรรทัด “tokenize”: “thai”)

สุดท่ายตั้ง ELASTICSEARCH_DEFAULT_ANALYZER ให้เป็นตามชื่อ “default” ของเรา


การสร้างฟอร์มค้นหาอย่างง่าย
ให้เพิ่มลงใน urls.py ได้เลย

url(r’^search/’, include(‘haystack.urls’)),

หมายเหตุ: รูปประกอบทำ Custom View ขึ้นมาเอง

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade