Haystack more_like_this returns all

2019-06-03 11:12发布

问题:

I am using Django, haystack, solr, to do searching. Ive am able to search and now I would like to find similar items using more_like_this. When I try to use the more_like_this functionality I get back all of the objects that are of that model type instead of just the ones that closely match it. Here is some code to show you how I am using it:

def resource_view(request, slug):
    resource = Resource.objects.get(slug=slug)
    versions = Version.objects.get_for_object(resource)
    related = SearchQuerySet().more_like_this(resource)
    add_comment_form = AddCommentForm()
    return render_to_response('resources/resource.html',
                              {'resource': resource,
                               'versions': versions,
                               'related': related,
                               'add_comment_form': add_comment_form},
                              context_instance=RequestContext(request))

Apparently I need to enable mlt in the solrconfig.xml file. Anyone know how to do this, or an article/tutorial that is helpful?

回答1:

stale question, but here's the answer anyway:

As John already pointed out, you need to add the more like this handler (MLT) to your solr config. This should do, put it somewhere in your solrconfig.xml, and reload SOLR (Tomcat):

<requestHandler name="/mlt" class="solr.MoreLikeThisHandler">
<lst name="defaults">
  <str name="mlt.mintf">1</str>
  <str name="mlt.mindf">1</str>
  <str name="mlt.minwl">3</str>
  <str name="mlt.maxwl">15</str>
  <str name="mlt.maxqt">20</str>
  <str name="mlt.match.include">false</str>
</lst>
</requestHandler>