简体   繁体   English

django icontains搜索postgres数据库

[英]django icontains to search postgres database

I currently have a Django application and postgres database. 我目前有一个Django应用程序和postgres数据库。 I want to have a search bar that allows users to enter in a value and it will search some of the fields of the model to search for matching values. 我想要一个允许用户输入值的搜索栏,它将搜索模型的某些字段以搜索匹配的值。 I want this to work even for values of "". 我希望即使对于“”值也可以使用。 I currently have: 我目前有:

MyModel.objects.filter(myfield__icontains=search_query).order_by(...)

How would I make this so that it can search multiple fields of the model at the same time. 我将如何做到这一点,以便它可以同时搜索模型的多个字段。 What is the most efficient way to do so? 这样做最有效的方法是什么? Is "icontains" okay for this? 这样“包含”可以吗?

Any help would be greatly appreciated! 任何帮助将不胜感激!

You can use Q to search multiple fields, for example: 您可以使用Q搜索多个字段,例如:

fields that you want to search: 您要搜索的字段:

field0
field1
field2

Django search code: Django搜索代码:

from django.db.models import Q

search_result = MyModel.objects.filter(
    Q(field0_icontains=search_query) |
    Q(field1_icontains=search_query) |
    Q(field2_icontains=search_query)
).order_by(...)

Doing this through regular filter queries and icontains is not advisable as it becomes inefficient pretty quickly - you certainly don't want to be doing that on multiple large text fields. 不建议通过常规的过滤器查询和icontains来执行此操作,因为它很快会变得效率低下-您当然不希望在多个大型文本字段上执行此操作。

However, PostgreSQL comes with a full text search engine which is designed for exactly this purpose. 但是,PostgreSQL附带了一个全文搜索引擎 ,正是出于这个目的而设计的。 Django provides support for this . Django 为此提供支持

You can define a SearchVector to perform full text search on multiple fields at once, eg, : 您可以定义SearchVector以便一次对多个字段执行全文搜索,例如:

from django.contrib.postgres.search import SearchVector

MyModel.objects.annotate(
    search=SearchVector('field_1') + SearchVector('field_2'),
).filter(search='search_query')

The documentation I've linked to provides a lot of additional information on how to perform ranking etc. on search results. 我链接到的文档提供了许多有关如何对搜索结果进行排名等其他信息。

Another alternative is to use a search engine like Elasticsearch - whether this is necessary depends on how many objects you have and what kind of filtering and ranking you need to do on results. 另一种选择是使用像Elasticsearch这样的搜索引擎-是否必要取决于您有多少个对象以及需要对结果进行什么样的筛选和排名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Postgres 搜索 Django 的现代实用选项? 需要多个条件,但 icontains 效率太低 - Modern practical option for Django search with Postgres? Need multiple criteria, but icontains is too inefficient 使用django在数据库中搜索字符串。 icontains和iexact查找无法满足我的要求 - Search string in database with django. icontains and iexact lookups not fulfill my requirements 简单的Django queryset __icontains搜索将首先找到大多数特定元素 - Simple Django queryset __icontains search that will find most specific elements first 带有 __in 查找的 django icontains - django icontains with __in lookup 使用 icontains 的 Django SearchVector - Django SearchVector using icontains icontains和getlist django python - icontains and getlist django python 如何更改Django的默认Postgres数据库搜索目录 - How to change Django's default Postgres database search directory 如何使用Django icontains查询搜索包含非ASCII字符的文本? - How to search text containing non-ASCII characters with Django icontains query? 使用 django 外键进行多次搜索(相关字段查找无效:​​icontains) - Multiple search with django foreign key (Related Field got invalid lookup: icontains) Django:icontains大小写对Unicode敏感 - Django: icontains case is sensitive for unicode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM