This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT to_tsvector(content), to_tsquery('english', 'watch') | |
| FROM posts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "'fun':8 'go':3 'much':7" | |
| "'awesom':4 'django':2 'unchain':3 'watch':1" | |
| "'know':4 'nt':3 'pizza':7" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ALTER TABLE posts ADD COLUMN search_index tsvector; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pip install djorm-ext-pgfulltext |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| from djorm_pgfulltext.models import SearchManager | |
| from djorm_pgfulltext.fields import VectorField | |
| ... | |
| class Post(models.Model): | |
| """ | |
| Django Model for a blog entry | |
| """ | |
| title = models.CharField(max_length=250) | |
| body = models.TextField(blank=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def search(request): | |
| """ | |
| Search through the entries | |
| """ | |
| query = request.GET['q'] | |
| results = Post.search_manager.search(query) | |
| return render_to_response('search_index.html', | |
| {'results': results, 'query': query}, | |
| context_instance=RequestContext(request)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE INDEX post_search_index | |
| ON posts | |
| USING gin | |
| (search_index); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import defaultdict | |
| import random, math | |
| def greedy_tour(graph, start_node): | |
| """ | |
| Compute a greedy TSP solution for a given graph | |
| """ | |
| solution = [start_node] | |
| visited = set([start_node]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy | |
| # The link structure | |
| links = { | |
| 'a.com': ['b.com', 'c.com', 'g.com', 'i.com'], | |
| 'b.com': ['d.com', 'c.com', 'a.com'], | |
| 'c.com': ['i.com'], | |
| 'd.com': [], | |
| 'e.com': ['d.com'], | |
| 'f.com': ['c.com'], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy | |
| # The link structure | |
| links = { | |
| 'a.com': ['b.com', 'c.com', 'g.com', 'i.com'], | |
| 'b.com': ['d.com', 'c.com', 'a.com'], | |
| 'c.com': ['i.com'], | |
| 'd.com': [], | |
| 'e.com': ['d.com'], | |
| 'f.com': ['c.com'], |