-
-
Save mlissner/d1e32a5572932665c15541fdb2e2c7af to your computer and use it in GitHub Desktop.
This extends the normal SolrInterface in Scorched so that you can add extra parameters to your query.
This file contains 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 scorched import SolrInterface | |
from scorched.search import Options, SolrSearch | |
class ExtraSolrInterface(SolrInterface): | |
"""Extends the SolrInterface class so that it uses the ExtraSolrSearch | |
class. | |
""" | |
def __init__(self, *args, **kwargs): | |
super(ExtraSolrInterface, self).__init__(*args, **kwargs) | |
def query(self, *args, **kwargs): | |
""" | |
:returns: SolrSearch -- A solrsearch. | |
Build a solr query | |
""" | |
# Change this line to hit our class instead of SolrSearch. All the rest | |
# of this class is the same. | |
q = ExtraSolrSearch(self) | |
if len(args) + len(kwargs) > 0: | |
return q.query(*args, **kwargs) | |
else: | |
return q | |
class ExtraSolrSearch(SolrSearch): | |
"""Base class for common search options management""" | |
option_modules = ('query_obj', 'filter_obj', 'paginator', | |
'more_like_this', 'highlighter', 'faceter', | |
'sorter', 'facet_querier', 'field_limiter', | |
'extra') | |
def _init_common_modules(self): | |
super(ExtraSolrSearch, self)._init_common_modules() | |
self.extra = ExtraOptions() | |
def add_extra(self, **kwargs): | |
newself = self.clone() | |
newself.extra.update(kwargs) | |
return newself | |
class ExtraOptions(Options): | |
def __init__(self, original=None): | |
if original is None: | |
self.option_dict = {} | |
else: | |
self.option_dict = original.option_dict.copy() | |
def update(self, extra_options): | |
self.option_dict.update(extra_options) | |
def options(self): | |
return self.option_dict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment