[英]How to config Django-haystack so “content” searched still shows results in the search field
I am using django-haystack-solr and ajax. 我正在使用django-haystack-solr和ajax。 When a client types in the search field, results matching it dispaly in a drop down box.
当客户在搜索字段中键入内容时,与之匹配的结果会在下拉框中显示。 the thing is solr also searches my content.
事情是solr也在搜索我的内容。 So if a client types in pasta primavera and its in the content and not the title it doesnt show any result for that.
因此,如果客户输入面食primavera及其内容而不是标题,则不会显示任何结果。 How do I make it so the post title shows even though the query is in the content?
我如何做到即使查询在内容中也能显示帖子标题? To be clear, if I type in pasta primavera and its not a post.title but in the post.content of the post, I still want the post.title to be displayed.
需要明确的是,如果我键入primavera意大利面,而不是post.title,而是在post.content中输入,我仍然希望显示post.title。 What is the correct syntax to get this result?
获得此结果的正确语法是什么? The following is my code
以下是我的代码
my search_index.py 我的search_index.py
class PostIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
title = indexes.CharField(model_attr='title')
content = indexes.CharField(model_attr='content')
publish = indexes.DateTimeField(model_attr='publish')
slug = indexes.CharField(model_attr='slug')
content_auto = indexes.EdgeNgramField(model_attr='title')
def get_model(self):
return Post
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.all()
my views.py 我的views.py
def search_title(request):
posts = SearchQuerySet().autocomplete(content_auto=request.GET.get('search_text', '')).order_by('-publish')
context = {
"posts": posts
}
# context.update(csrf(request))
return render(request, "posts/ajax_search.html", {"posts": posts})
my post_text.txt 我的post_text.txt
{{ object.title }}
{{ object.content }}
my ajax_search.html 我的ajax_search.html
{% if posts.count > 0 %}
<div class="result-box" id="cell">
{% for post in posts %}
<li><a href="{% url 'posts:detail' post.slug %}" id="fix" class="list-group-item">{{ post.title }}</a></li>
{% endfor %}
{% else %}
<li class="list-group-item" id="none"> None to show</li>
{% endif %}
</div>
all guidance is welcome 欢迎所有指导
The field that is searched during autocomplete search is configured with this line of your code: 在自动完成搜索过程中搜索的字段使用以下代码行配置:
content_auto = indexes.EdgeNgramField(model_attr='title')
I have not done it using the haystack classes (always configuring my own handler on the SOLR side), but I think you just have to configure two fields: 我没有使用haystack类完成此操作(始终在SOLR端配置我自己的处理程序),但是我认为您只需要配置两个字段:
title_auto = indexes.EdgeNgramField(model_attr='title')
content_auto = indexes.EdgeNgramField(model_attr='content')
And add another field to your query: 并将另一个字段添加到您的查询:
query = request.GET.get('search_text', None)
if query:
# updated according to losee's comment
sqs1 = sqs.autocomplete(title_auto=query).order_by('-publish')
sqs2 = sqs.autocomplete(content_auto=query).order_by('-publish')
posts = sqs1 | sqs2
SearchQuerySet.autocomplete() source code: https://github.com/django-haystack/django-haystack/blob/master/haystack/query.py#L448 SearchQuerySet.autocomplete()源代码: https : //github.com/django-haystack/django-haystack/blob/master/haystack/query.py#L448
Note: You will have to add that new field into your SOLR schema. 注意:您将必须将该新字段添加到SOLR模式中。
If you are familiar enough with SOLR, here another way to do it: 如果您对SOLR足够熟悉,则可以使用另一种方法:
You already have the field content_auto
in your solr schema. 您的solr模式中已经包含了
content_auto
字段。 Add the following to your schema: 将以下内容添加到您的架构:
<copyField source="content" dest="content_auto" />
Reindex. 重新编制。 Then your current code should work.
然后,您当前的代码应该可以正常工作。 https://cwiki.apache.org/confluence/display/solr/Copying+Fields
https://cwiki.apache.org/confluence/display/solr/Copying+Fields
This is what worked for me 这对我有用
sqs = SearchQuerySet()
sqs1 = sqs.autocomplete(title_auto=query).order_by('-publish')
sqs2 = sqs.autocomplete(content_auto=query).order_by('-publish')
posts = sqs1 | sqs2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.