Sparrho/bungiesearch — pull request #23 by ChristopherRabotin (2014-09-22 12:43:39+00:00)
Title: Added search aliases

Allows defining search aliases for multiple `ModelIndex` objects. Search aliases have a prefix, defaulted to `bungie_`. This is useful when a specific search is applicable to different models. For example, if models `Article` and `Tweet` both have an index field called `title`, you can define a single search alias which will return a Bungiesearch object pre-formatted with the title search.

# Code
In `search_aliases` (or which ever module is defined in the settings, e.g. `'ALIASES': ['core.search_aliases'],`):
```
class SearchTitle(SearchAlias):
def alias_for(self, title):
return self.search_instance.query('match', title=title)

class Meta:
models = (Article, Tweet)
_alias_name = 'title_search'
```

Make sure to use Bungiesearch as a manager of your model:
```
class Article(models.Model):
# ...
objects = BungiesearchManager()

class Tweet(models.Model):
# ...
objects = BungiesearchManager()
```

Now, in *any* module, without any additional import, you can call `Tweet.object.bungie_title_search("bungie")` which will return a lazy Bungiesearch object, so that it can receive more queries, e.g. `Tweet.object.bungie_title_search("bungie").filter("term", field="field term")`.