I've recently set up solr and haystack to search one of my django models. I attempted to modify the default solr schema built by haystack to use the NGramTokenizerFactory
:
<fieldType name="text" class="solr.TextField">
<analyzer type="index">
<tokenizer class="solr.NGramTokenizerFactory" minGramSize="3" maxGramSize="32" />
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="solr.NGramTokenizerFactory" minGramSize="3" maxGramSize="32" />
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
I have a bunch of one or two word entries in my database which I would like to match against the user's query. So for example, I might have one object with title "dog" and another with title "cat". If the user searches for "dog cat" then I would like to return both the dog and cat objects for that query.
Similarly, if I search for "my cool website" I would like the field with "website" to be returned.
I tried using the solr admin interface to check to make sure my queries were getting matched. Everything seems okay there:
: The issue is when I use the haystack default search interface to search for that same query:As you can see, no results are found. I tried using KeywordFactory and a bunch of different solr configurations. If I'm not mistaken then the query should be getting matched. 开发者_高级运维I'm not sure why haystack is coming up empty though.
Thanks for any help / suggestions on if this is the best way to go about such a search.
Few month ago I worked with django-haystack
and solr. I also had a problems with making some special queries to solr.
Actually it should be solved by addng next line to settings.py
:
HAYSTACK_DEFAULT_OPERATOR = 'OR' # actually has no effect...
But it does not work for me.
So, in my case it was solved by subclassing SearchView
class. This is small snippet from my project:
# views.py:
from haystack.views import SearchView
class PeriodicalSearchView(SearchView):
def get_results(self):
"""
Fetches the results via the form.
Returns an empty list if there's no query to search with.
"""
if not (self.form.is_valid() and self.form.cleaned_data['q']):
return self.form.no_query_found()
query = self.form.cleaned_data['q']
words = iter(set(query.split()))
word = words.next()
sqs = self.form.searchqueryset.filter(text=word) # actually I have one more field here...
for word in words:
sqs = sqs.filter_or(title=word).filter_or(text=word)
if self.load_all:
sqs = sqs.load_all()
return sqs
def __call__(self, request, template_name=None):
"""
Generates the actual response to the search.
Relies on internal, overridable methods to construct the response.
"""
if template_name:
self.template = template_name
return super(PeriodicalSearchView, self).__call__(request)
And urls.py
# urls.py:
from .views import PeriodicalSearchView
urlpatterns = patterns('',
url(r'^search/$', PeriodicalSearchView(template='template_search.html'),
name='haystack_search'),
)
And that's it.
精彩评论