未加星标

Peter Bengtsson: Autocompeter is Dead. Long live Autocompeter!

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

About 2 years ago I launched Autocompeter.com . It was two parts:

1) A autocompeter.js pure javascript solution to add autocomplete to a search input field.

2) A REST API where you can submit titles with a HTTP header key, and a fancy autocomplete search.

Only Rewrote the Go + Redis part

The second part has now been completely re-written. The server was originally written in Go and used Redis. Now it's Django and ElasticSearch.

The ultimate reason for this was that Redis was, by far, the biggest memory consumer on my shared DigitalOcean server. The way it worked was that every prefix of every word in every title was indexes as a key. For example the words p , pe , pet , pete , peter and peter$ are all keys and they point to an array of IDs that you then look up to get the distinct set of titles and their URLs. This makes it really really fast but since redis doesn't support namespaces, or multiple columns it means that for every prefix it needs a prefix of its own for the domain they belong to. So the hash for www.peterbe.com is eb9f747 so the strings to store are instead eb9f747p , eb9f747pe , eb9f747pet , eb9f747pete , eb9f747peter and eb9f747peter$ .

ElasticSearch on the other hand has ALL of this built in deep in Lucene. AND you can filter. So the way it's queried now instead is something like this:

search = TitleDoc.search() search = search.filter('term', domain=domain.name) search = search.query(Q('match_phrase', title=request.GET['q'])) search = search.sort('-popularity', '_score') search = search[:size] response = search.execute() ...

And here's how the mapping is defined:

from elasticsearch_dsl import ( DocType, Float, Text, Index, analyzer, Keyword, token_filter, ) edge_ngram_analyzer = analyzer( 'edge_ngram_analyzer', type='custom', tokenizer='standard', filter=[ 'lowercase', token_filter( 'edge_ngram_filter', type='edgeNGram', min_gram=1, max_gram=20 ) ] ) class TitleDoc(DocType): id = Keyword() domain = Keyword(required=True) url = Keyword(required=True, index=False) title = Text( required=True, analyzer=edge_ngram_analyzer, search_analyzer='standard' ) popularity = Float() group = Keyword()

I'm learning ElasticSearch rapidly but I still feel like I have so much to learn. This solution I have here is quite good and I'm pretty happy with the results but I bet there's a lot of things I can learn to make it even better.

Why Ditch Go?

I actually had a lot of fun building the first server version of Autocompeter in Go but Django is just so many times more convenient. It's got management commands, ORM, authentication system, CSRF protection, awesome error reporting, etc. All built in! With Go I had to build everything from scratch.

Also, I felt like the important thing here is the JavaScript client and the database. Now that I've proven this to work with Django and elasticsearch-dsl I think it wouldn't be too hard to re-write the critical query API in Go or in something like Sanic for maximum performance.

All Dockerized

Oh, one of the reasons I wanted to do this new server in python is because I want to learn Docker better and in particular Docker with Python projects.

The project is now entirely contained in Docker so you can start the PostgreSQL, ElasticSearch 5.1.1 and Django with docker-compose up . There might be a couple of things I've forgot to document for how to configure things but this is actually the first time I've developed something entirely in Docker.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

分页:12
转载请注明
本文标题:Peter Bengtsson: Autocompeter is Dead. Long live Autocompeter!
本站链接:http://www.codesec.net/view/522152.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(84)