I have a model like this:
我有这样的模型:
class MyModel(models.Model):
desc1 = models.TextField(blank=True, default="")
desc2 = models.TextField(blank=True, default="")
I want to search string on fields of this model. Assume these instances of MyModel:
我想在这个模型的字段上搜索字符串。假设MyModel的这些实例:
1: desc1="ABc?", desc2="asdasd you"
2: desc1="Hello, How are you?", desc2="Thank you!"
3: desc1="ABc?", desc2="ajdf"
when I search "you", it should show me, first and second instances. finally I need to show the results which have "you" in desc1 higher than others. for example, in this sample, second one should be higher than first one.
当我搜索“你”时,它应该显示我,第一和第二个实例。最后我需要显示desc1中“你”比其他人更高的结果。例如,在此示例中,第二个应高于第一个。
I have used, haystack for search and created a template for this. but I couldn't solve the priority problem.
我用过,haystack进行搜索并为此创建了一个模板。但我无法解决优先问题。
2 个解决方案
#1
2
When you say 'priority' you really mean 'sort', in the lingo of searching.
当你说'优先'时,你的意思是'排序',在搜索的语言中。
Django Haystack can sort by field matches, but it can sort by 'score', where it uses an algorithm to determine the sort order. You can influence the weighting of the score with 'Boost'ing it -- see http://django-haystack.readthedocs.org/en/latest/boost.html
Django Haystack可以按字段匹配排序,但它可以按“得分”排序,它使用算法来确定排序顺序。你可以用'Boost'来影响得分的加权 - 见http://django-haystack.readthedocs.org/en/latest/boost.html
Also, you should consider adding extra fields in your search_indexes.py that will be just for weighting. You don't need to have a one-to-one mapping between the Django model fields and the index. Something like
此外,您应该考虑在search_indexes.py中添加额外字段,这些字段仅用于加权。您不需要在Django模型字段和索引之间建立一对一的映射。就像是
class MyModelIndex(QueuedSearchIndex):
desc1 = indexes.CharField()
desc2 = indexes.CharField()
otherWeightedField = indexes.CharField()
def prepare_otherWeightedField(self,obj)
# fill the extra field with extra stuff to help you sort, based on processing values from other fields
#2
1
I use this approach.
我用这种方法。
from types import ListType
from haystack import indexes
class DocumentField(indexes.SearchField):
"""An index field that combines and weights other fields because Haystack
does not provide for weighted searching. This field makes use of other
existing field classes for DRY."""
def __init__(self, *args, **kwargs):
self.fields = kwargs.pop("fields", None)
super(DocumentField, self).__init__(*args, **kwargs)
self.document = True
self.use_template = False
def prepare(self, obj):
values = []
for field_instance in self.fields.values():
v = field_instance.prepare(obj)
if not v:
continue
if not isinstance(v, ListType):
v = [v]
# Apply boost
v = v * int(field_instance.boost * 10)
values.extend(v)
return "\n".join(values)
class MyModelIndex(indexes.SearchIndex, indexes.Indexable):
text = DocumentField(fields=dict(
desc1 = indexes.CharField(model_attr="desc1", boost=1.0),
desc2 = indexes.CharField(model_attr="desc2", boost=0.7)
))
def get_model(self):
return MyModel
#1
2
When you say 'priority' you really mean 'sort', in the lingo of searching.
当你说'优先'时,你的意思是'排序',在搜索的语言中。
Django Haystack can sort by field matches, but it can sort by 'score', where it uses an algorithm to determine the sort order. You can influence the weighting of the score with 'Boost'ing it -- see http://django-haystack.readthedocs.org/en/latest/boost.html
Django Haystack可以按字段匹配排序,但它可以按“得分”排序,它使用算法来确定排序顺序。你可以用'Boost'来影响得分的加权 - 见http://django-haystack.readthedocs.org/en/latest/boost.html
Also, you should consider adding extra fields in your search_indexes.py that will be just for weighting. You don't need to have a one-to-one mapping between the Django model fields and the index. Something like
此外,您应该考虑在search_indexes.py中添加额外字段,这些字段仅用于加权。您不需要在Django模型字段和索引之间建立一对一的映射。就像是
class MyModelIndex(QueuedSearchIndex):
desc1 = indexes.CharField()
desc2 = indexes.CharField()
otherWeightedField = indexes.CharField()
def prepare_otherWeightedField(self,obj)
# fill the extra field with extra stuff to help you sort, based on processing values from other fields
#2
1
I use this approach.
我用这种方法。
from types import ListType
from haystack import indexes
class DocumentField(indexes.SearchField):
"""An index field that combines and weights other fields because Haystack
does not provide for weighted searching. This field makes use of other
existing field classes for DRY."""
def __init__(self, *args, **kwargs):
self.fields = kwargs.pop("fields", None)
super(DocumentField, self).__init__(*args, **kwargs)
self.document = True
self.use_template = False
def prepare(self, obj):
values = []
for field_instance in self.fields.values():
v = field_instance.prepare(obj)
if not v:
continue
if not isinstance(v, ListType):
v = [v]
# Apply boost
v = v * int(field_instance.boost * 10)
values.extend(v)
return "\n".join(values)
class MyModelIndex(indexes.SearchIndex, indexes.Indexable):
text = DocumentField(fields=dict(
desc1 = indexes.CharField(model_attr="desc1", boost=1.0),
desc2 = indexes.CharField(model_attr="desc2", boost=0.7)
))
def get_model(self):
return MyModel