我正在尝试使用django-haystack. 我正在尝试实现用户搜索。我的用户模型是这样的:
class MyUser(AbstractBaseUser):
username = models.CharField(max_length=255, unique=True)
name = models.CharField(max_length=63, blank=True)
email = models.EmailField(blank=True, unique=True)
status = models.CharField(max_length=255, blank=True, null=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
joined = models.DateTimeField(auto_now_add=True, null=True)
Run Code Online (Sandbox Code Playgroud)
现在,我想搜索name和username字段。我创建了以下内容search_indexes.py:
class UserIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.EdgeNgramField(document=True, model_attr='name')
username = indexes.CharField(model_attr='username')
def get_model(self):
return MyUser
def get_updated_field(self):
return "joined"
Run Code Online (Sandbox Code Playgroud)
但是,当我执行搜索时,我只得到与该name字段匹配的结果。我在这里做错了什么?还有其他方法可以做到这一点吗?
提前致谢。
我有一个Haystack/xapian搜索索引django.contrib.auth.models.User.模板很简单
{{object.get_full_name}}
Run Code Online (Sandbox Code Playgroud)
因为我打算让用户输入名称并能够搜索它.
我的问题是:如果我搜索(比如说)Sri(我的完整名字),我会得到一个与我的名字有关的用户对象的结果.但是,如果我搜索Sri Ragh- 也就是我的全名,以及我姓氏的一部分,我就没有结果.
如何设置Haystack以便我可以获得部分查询的相应结果?(我基本上希望它能够搜索*Sri Ragh*,但我不知道通配符是否会真正发挥作用,或者如何实现它们).
这是我的搜索查询:
results = SearchQuerySet().filter(content='Sri Ragh')
Run Code Online (Sandbox Code Playgroud) 我正在阅读Haystack的"入门"指南,并尝试使用Haystack为我的网站实现Whoosh后端.我成功设置了整个项目,我还可以在search.html模板上看到搜索框.我虽然无法建立索引.
当我输入./manage.py rebuild_index时,我收到错误:
未知命令:rebuild_index
我知道这是一个微不足道的问题.我无法理解manage.py文件如何理解原本不属于它的命令.
我有搜索结果显示使用facet选项来深入挖掘数据.选择构面后,它会更改构面结果.因此,如果我最初显示"places(10)","images(5)","people(3)"和"All(18)"作为分面选项,我点击图像我仍然希望看到所有的其他人,即使搜索结果发生了变化.有没有办法做到这一点?
我目前正在寻求基于索引中的许多字段实现更多类似于此功能的功能.
我目前的配置如下:Haystack | PySolr | Solr的
对于这篇文章我正在使用PySolr并将参数传递给more_like_this函数.响应找到文档但不找到任何相关结果.这是为什么?
这是我点击的网址:
Run Code Online (Sandbox Code Playgroud)http://localhost:8080/solr/mlt?q=django_id:12123412&mlt.fl=industry_ids,loc_state,amount,sector_id&mlt.interestingTerms=details
以下是我对Solr的回复:
<response>
<object type="{XXXXXX-0F1D-4F28-AAA2-XXXXXXXXXXX}" cotype="cs" id="cosymantecbfw" style="width: 0px; height: 0px; display: block;"/>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">24</int>
</lst>
<result name="match" numFound="1" start="0">
<doc>...</doc>
</result>
<result name="response" numFound="0" start="0"/>
<lst name="interestingTerms"/>
</response>
Run Code Online (Sandbox Code Playgroud)
solrconfig.xml中
<!-- More Like This -->
<requestHandler name="/mlt" class="solr.MoreLikeThisHandler">
</requestHandler>
Run Code Online (Sandbox Code Playgroud)
schema.xml中
<field name="award_amount" type="sfloat" indexed="true" stored="true" multiValued="false" termVectors="true" />
<field name="estatus" type="slong" indexed="true" stored="true" multiValued="false" termVectors="true"/>
<field name="loc_state" type="string" indexed="true" stored="true" multiValued="false" termVectors="true"/>
<field name="orgtype_id" type="string" indexed="true" stored="true" multiValued="false" …Run Code Online (Sandbox Code Playgroud) 我使用haystack搜索我的django网站,它完美地做到了这一点.但是在我的结果页面上,链接不起作用.在我的模板中我使用的代码:
<a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a>
Run Code Online (Sandbox Code Playgroud)
在我的其他/ models.py中,我包括:
def get_absolute_url(self):
return urlresolvers.reverse('post', args=[self.pk])
Run Code Online (Sandbox Code Playgroud)
我的urls.py看起来像:
from django.conf.urls.defaults import *
from dbe.other.models import *
urlpatterns = patterns('dbe.other.views',
(r"^(\d+)/$", "post"),
(r"^add_comment/(\d+)/$", "add_comment"),
(r"^delete_comment/(\d+)/$", "delete_comment"),
(r"^delete_comment/(\d+)/(\d+)/$", "delete_comment"),
(r"^month/(\d+)/(\d+)/$", "month"),
(r"", "main"),
)
Run Code Online (Sandbox Code Playgroud)
它应该链接到的URL是:
http://127.0.0.1:8000/other/10/
Run Code Online (Sandbox Code Playgroud)
但它仍然链接到:
http://127.0.0.1:8000/search/?q=searchterm
Run Code Online (Sandbox Code Playgroud)
在shell中会发生这种情况:
>>> from other.models import Post
>>> inst = Post.objects.get(pk=1)
>>> inst.get_absolute_url()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/export/mailgrp4_a/sc10jbr/lib/python/django/utils/functional.py", line 55, in _curried
return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
File "/export/mailgrp4_a/sc10jbr/lib/python/django/db/models/base.py", line 887, …Run Code Online (Sandbox Code Playgroud) 我正在使用django-haystack来搜索我的应用程序,我有布尔字段我想过滤我的模型.但是,当我尝试这样做时,我的搜索查询失败.
我正在使用的搜索后端是elasticsearch
我有一个大约40k行的数据集,每行有4个字段.现在我想对文本框中的这4个字段使用自动完成机制(必须在显示建议之前将这4个字段中的值连接成一个字符串).哪种查询会扩展并且表现更好?EdgeNGrams上的自动完成或简单文本索引上的通配符搜索.
我正在尝试使用whoosh作为引擎来设置django-haystack。如本文所述,在建立第一个索引时发生了我的问题。
鉴于我的staff / models.py:
class Person(models.Model):
first_name = models.CharField(_('first name'), max_length=30, blank=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True)
def __unicode__(self):
return '%s %s' % ( self.first_name, self.last_name)
Run Code Online (Sandbox Code Playgroud)
我已经写了职员/indexes.py:
from haystack import indexes
from staff.models import Person
class PersonIndex(indexes.SearchIndex, indexes.Indexable):
first_name = indexes.CharField(model_attr='first_name')
last_name = indexes.CharField(document=True, model_attr='last_name')
def get_model(self):
return Person
Run Code Online (Sandbox Code Playgroud)
然后,将以下配置添加到mycms / settings.py中,以将whoosh用作引擎:
import os
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
},
}
Run Code Online (Sandbox Code Playgroud)
后者是从教程中逐字逐句地获取的。然后,我为索引添加了一个简单的文本模板,即mycms / templates / search / indexes …
我正在使用Haystack连接并与elasticsearch的安装进行交互.Elasticsearch安装在主Web服务器的不同框中.
我使用nginx在elasticsearch框上设置了HTTP身份验证.这是为了阻止对elasticsearch的未授权访问.
Haystack配置看起来像这样:
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': 'http://USERNAME:PASSWORD@DOMAIN:PORT/',
'INDEX_NAME': 'haystack',
},
}
Run Code Online (Sandbox Code Playgroud)
有了这个设置,我得到一个连接错误:
elasticsearch.exceptions.ConnectionError: ConnectionError(('Connection aborted.',
gaierror(-2, 'Name or service not known'))) caused by: ProtocolError(('Connection
aborted.', gaierror(-2, 'Name or service not known')))
Run Code Online (Sandbox Code Playgroud)
如果我关闭HTTP身份验证并相应地更新URL http://DOMAIN:PORT/连接没有问题.
可能是Haystack(或elasticsearch-py(http://www.elasticsearch.org/guide/en/elasticsearch/client/python-api/current/)不允许在URL中使用HTTP身份验证吗?I请注意这是Solr - Solr身份验证的问题(使用Django Haystack)
django http-authentication django-haystack elasticsearch pyelasticsearch
django-haystack ×10
django ×7
python ×3
whoosh ×3
solr ×2
facet ×1
morelikethis ×1
n-gram ×1
pysolr ×1
search ×1