我正在使用Haystack和Whoosh用我正在建造的django网站进行搜索。我想在搜索条件上使用OR运算符(例如“搜索字符串”将查找带有文本“搜索”或“字符串”而不是“搜索”和“字符串”的对象)
这似乎非常简单,因为haystack允许您通过在settings.py文件中设置HAYSTACK_DEFAULT_OPERATOR = 'OR'来覆盖默认的“AND”运算符。
不幸的是,将此添加到我的settings.py中没有任何效果。我在stackoverflow上找到了一些对此行为的切向引用,但没有解决方案。我也找到了issue posted on github,但自去年以来一直没有评论或分类。
我可能做错了,所以我想在这里发帖,看看是否有解决方案。没有人,我有点被困!
我在settings.py中的干草堆设置:
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
},
}
HAYSTACK_DEFAULT_OPERATOR = 'OR'
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
我的观点:
from haystack import views as hsviews
def search_test(request):
return hsviews.basic_search(request)
我的search_indexes.py文件:
import datetime
from haystack import indexes
from myApp.models import MyModel
from django.contrib.auth.models import User
class MyModelIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.NgramField(document=True, use_template= True)
isPublic = indexes.BooleanField(model_attr='isPubliclyVisible')
brand = indexes.CharField(model_attr='brand')
model = indexes.CharField(model_attr='model')
owner = indexes.CharField(model_attr='owner')
owner_username = indexes.CharField()
obj_type = indexes.CharField()
def get_model(self):
return MyModel
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(isPubliclyVisible = True)
def prepare_owner_username(self, obj):
return obj.owner.user.username
def prepare_obj_type(self,obj):
return 'MyModel'
我确实找到了这个workaround(我尚未对我的解决方案进行过测试/思考),但我认为如果我/我们做错了什么,这保证了它自己的问题。
答案 0 :(得分:0)
我建议您编写自己的视图,以便更好地控制搜索查询的执行方式,而不是使用Haystack内置的 basic_search 函数。这样,您可以通过扩展视图或自定义搜索查询功能来处理更复杂的搜索,此外还可以更轻松地进行测试。
例如,您可以构建单独的SearchQuerySet过滤器来执行您正在搜索的每个关键字,然后" OR"他们在一起,像这样:
def get_query(request):
"""
This function retrieves any query terms (e.g q=search+term)
from the request object.
:param request: request object
:returns: query terms as a list (split on whitespace)
"""
query = None
qs_keyword = 'q'
if (qs_keyword in request.GET) and request.GET[qs_keyword].strip():
query_string = request.GET[qs_keyword]
query = query_string.split()
return query
def perform_query(request):
"""
This is a helper function to perform the actual query.
You can extend this to handle more complicated searches using AND,
OR, boolean qualifiers, etc.
:param request: request object
:returns: SearchQuerySet results
"""
query = get_query(request)
if not query:
results = EmptySearchQuerySet()
else:
results = SearchQuerySet()
for search_term in query:
# you can use the "|" (or) operator
results |= results.filter(content=search_term)
# or else use "filter_or"
# results = results.filter_or(content=search_term)
return results
def your_search_view(request, *args, **kwargs):
"""
This is your search view to process the query and display your results.
"""
# call "perform_query" to do the actual search
results = perform_query(request)
# do the rest of your view processing ...
return render_to_response(etc.)