天道酬勤,学无止境

searchqueryset

Django Haystack 字段的不同值(Django Haystack Distinct Value for Field)

问题 我正在使用 Django Haystack + Elasticsearch + Django REST Framework 构建一个小型搜索引擎,并且我试图找出重现 Django QuerySet的distinct方法的行为。 我的索引看起来像这样: class ItemIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) item_id = indexes.IntegerField(faceted=True) def prepare_item_id(self, obj): return obj.item_id 我希望能够做的是以下内容: sqs = SearchQuerySet().filter(content=my_search_query).distinct('item_id') 但是,Haystack 的SearchQuerySet没有distinct方法,所以我有点迷茫。 我尝试对字段进行分面,然后使用返回的item_id列表查询 Django,但这会失去 Elasticsearch 的性能,并且也无法使用 Elasticsearch 的排序功能。 有什么想法吗? 编辑: 示例数据: 示例数据: Item

2021-12-10 09:09:52    分类:技术分享    python   django   elasticsearch   django-haystack   searchqueryset

Django Haystack Distinct Value for Field

I am building a small search engine using Django Haystack + Elasticsearch + Django REST Framework, and I'm trying to figure out reproduce the behavior of a Django QuerySet's distinct method. My index looks something like this: class ItemIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) item_id = indexes.IntegerField(faceted=True) def prepare_item_id(self, obj): return obj.item_id What I'd like to be able to do is the following: sqs = SearchQuerySet().filter(content=my_search_query).distinct('item_id') However, Haystack's SearchQuerySet

2021-11-26 01:48:35    分类:问答    python   django   elasticsearch   django-haystack   searchqueryset

如何在不更改类型的情况下从 SearchQuerySet 中获取 n 个搜索对象?(How to get n search objects from a SearchQuerySet without changing the type?)

问题 我正在尝试获取 10 个对象,例如: q_auth = SearchQuerySet().filter(content=validate_query) q_auth = q_auth[:10] print type(q_auth) 我想要的输出是: <class 'haystack.query.SearchQuerySet'>但我得到的是<type 'list'> 。 有人可以帮我吗? 回答1 我尝试了类似你的代码的东西,但得到了这样的输出: <class 'django.db.models.query.QuerySet'> 根据您所拥有的,我认为您可以尝试以下操作: print type(q_auth[0]) 回答2 查看源代码,您将看到q_auth[:10]返回结果列表。 SearchQuerySet是惰性的,并且在您使用切片检索它们之前可能没有所有结果,即q_auth[:10] 。 做就是了: first_results = q_auth[:10] 并通过以下方式访问结果: first_results[0] 我建议不要这样做: q_auth = q_auth[:10] 因为您的SearchQuerySet实例q_auth将无法用于稍后检索更多结果。

2021-09-29 23:20:35    分类:技术分享    python   django   elasticsearch   django-haystack   searchqueryset

How to get n search objects from a SearchQuerySet without changing the type?

I am trying to get the to 10 objects like : q_auth = SearchQuerySet().filter(content=validate_query) q_auth = q_auth[:10] print type(q_auth) The output I want is: <class 'haystack.query.SearchQuerySet'> but I am getting is <type 'list'>. Can some one please help me out?

2021-06-28 19:19:25    分类:问答    python   django   elasticsearch   django-haystack   searchqueryset

SearchQuerySet 的哪个属性和 prefetch_related 的功能一样?(Which attribute of SearchQuerySet has the same function as prefetch_related?)

问题 def get_books_by_query_params(context, query, query_parameters): binding_query = query_parameters['binding_query'] query_parameters['validate']=1 default_query = None if query: default_queries = [ Q(title__icontains=query), Q(isbn_10__contains=query), Q(isbn_13__contains=query), Q(publishers=Publisher.objects.filter(name=query)), Q(institutes=Institute.objects.filter(name=query)), Q(authors=Author.objects.filter(name=query)), Q(sellers=Seller.objects.filter(name=query)) ] default_query = reduce(operator.or_, default_queries) default_query = default_query & binding_query if binding_query is

2021-06-23 05:54:58    分类:技术分享    python   django   django-haystack   searchqueryset

Which attribute of SearchQuerySet has the same function as prefetch_related?

def get_books_by_query_params(context, query, query_parameters): binding_query = query_parameters['binding_query'] query_parameters['validate']=1 default_query = None if query: default_queries = [ Q(title__icontains=query), Q(isbn_10__contains=query), Q(isbn_13__contains=query), Q(publishers=Publisher.objects.filter(name=query)), Q(institutes=Institute.objects.filter(name=query)), Q(authors=Author.objects.filter(name=query)), Q(sellers=Seller.objects.filter(name=query)) ] default_query = reduce(operator.or_, default_queries) default_query = default_query & binding_query if binding_query is not

2021-05-14 03:38:53    分类:问答    python   django   django-haystack   searchqueryset