Skip to content

djangodocs

Django 3.1
  • 6.1 current
  • 6.0
  • 5.2 LTS
  • 5.1 unsupported
  • 5.0 unsupported
  • 4.2 unsupported
  • 4.1 unsupported
  • 4.0 unsupported
  • 3.2 unsupported
  • 3.1 unsupported
  • 3.0 unsupported
  • 2.2 unsupported
  • 2.1 unsupported
  • 2.0 unsupported
  • 1.11 unsupported
한국어
  • English
  • 简体中文
  • Français
  • 日本語
  • Bahasa Indonesia
  • Português (Brasil)
  • 한국어
  • Español
  • Ελληνικά
  • Polski
Documentation contents
  • Django 문서
  • 시작하기
  • Django 사용하기
    • Django 설치하는 법
    • 모델과 데이터베이스
      • 모델
      • 쿼리 만들기
      • Aggregation
      • 검색
      • Managers
      • Performing raw SQL queries
      • 데이터베이스 트랜잭션
      • Multiple databases
      • 테이블스페이스
      • 데이터베이스 접근 최적화
      • Database instrumentation
      • Examples of model relationship API usage
    • Handling HTTP requests
    • 폼을 사용하여 작업하기
    • 템플릿
    • Class-based views
    • 마이그레이션
    • 파일 관리하기
    • Testing in Django
    • Django에서의 사용자 증명
    • Django의 캐시 프레임워크
    • Conditional View Processing
    • Cryptographic signing
    • 이메일 보내기
    • 국제화와 현지화
    • 로깅
    • 페이지네이션
    • Security in Django
    • 성능과 최적화
    • Serializing Django objects
    • Django settings
    • Signals
    • 시스템 점검 프레임워크
    • 외부 패키지
    • 비동기 지원
  • “How-to” 가이드
  • 장고 자주묻는 질문
  • API Reference
  • 메타 문서 및 기타
  • 용어
  • Release notes
  • Django internals
Django 3.1 is no longer supported. It receives no security fixes. Use it for reference only. Latest release
Korean translation. Untranslated passages appear in English. 29.5% Help translate
  1. Django 3.1
  2. Django 사용하기
  3. 모델과 데이터베이스

검색Link to this heading#

A common task for web applications is to search some data in the database with user input. In a simple case, this could be filtering a list of objects by a category. A more complex use case might require searching with weighting, categorization, highlighting, multiple languages, and so on. This document explains some of the possible use cases and the tools you can use.

We’ll refer to the same models used in 쿼리 만들기.

Use CasesLink to this heading#

표준적인 텍스트 질의Link to this heading#

Text-based fields have a selection of matching operations. For example, you may wish to allow lookup up an author like so:

Code
>>> Author.objects.filter(name__contains='Terry')
[<Author: Terry Gilliam>, <Author: Terry Jones>]

This is a very fragile solution as it requires the user to know an exact substring of the author’s name. A better approach could be a case-insensitive match (icontains), but this is only marginally better.

데이터베이스의 고급 비교 함수Link to this heading#

If you’re using PostgreSQL, Django provides a selection of database specific tools to allow you to leverage more complex querying options. Other databases have different selections of tools, possibly via plugins or user-defined functions. Django doesn’t include any support for them at this time. We’ll use some examples from PostgreSQL to demonstrate the kind of functionality databases may have.

다른 데이터베이스에서 검색하기

All of the searching tools provided by django.contrib.postgres are constructed entirely on public APIs such as custom lookups and database functions. Depending on your database, you should be able to construct queries to allow similar APIs. If there are specific things which cannot be achieved this way, please open a ticket.

In the above example, we determined that a case insensitive lookup would be more useful. When dealing with non-English names, a further improvement is to use unaccented comparison:

Code
>>> Author.objects.filter(name__unaccent__icontains='Helen')
[<Author: Helen Mirren>, <Author: Helena Bonham Carter>, <Author: Hélène Joy>]

This shows another issue, where we are matching against a different spelling of the name. In this case we have an asymmetry though - a search for Helen will pick up Helena or Hélène, but not the reverse. Another option would be to use a trigram_similar comparison, which compares sequences of letters.

예시:

Code
>>> Author.objects.filter(name__unaccent__lower__trigram_similar='Hélène')
[<Author: Helen Mirren>, <Author: Hélène Joy>]

Now we have a different problem - the longer name of “Helena Bonham Carter” doesn’t show up as it is much longer. Trigram searches consider all combinations of three letters, and compares how many appear in both search and source strings. For the longer name, there are more combinations that don’t appear in the source string, so it is no longer considered a close match.

The correct choice of comparison functions here depends on your particular data set, for example the language(s) used and the type of text being searched. All of the examples we’ve seen are on short strings where the user is likely to enter something close (by varying definitions) to the source data.

문서 기반 검색Link to this heading#

Standard database operations stop being a useful approach when you start considering large blocks of text. Whereas the examples above can be thought of as operations on a string of characters, full text search looks at the actual words. Depending on the system used, it’s likely to use some of the following ideas:

  • Ignoring “stop words” such as “a”, “the”, “and”.

  • Stemming words, so that “pony” and “ponies” are considered similar.

  • Weighting words based on different criteria such as how frequently they appear in the text, or the importance of the fields, such as the title or keywords, that they appear in.

There are many alternatives for using searching software, some of the most prominent are Elastic and Solr. These are full document-based search solutions. To use them with data from Django models, you’ll need a layer which translates your data into a textual document, including back-references to the database ids. When a search using the engine returns a certain document, you can then look it up in the database. There are a variety of third-party libraries which are designed to help with this process.

PostgreSQL 지원Link to this heading#

PostgreSQL has its own full text search implementation built-in. While not as powerful as some other search engines, it has the advantage of being inside your database and so can easily be combined with other relational queries such as categorization.

The django.contrib.postgres module provides some helpers to make these queries. For example, a query might select all the blog entries which mention “cheese”:

Code
>>> Entry.objects.filter(body_text__search='cheese')
[<Entry: Cheese on Toast recipes>, <Entry: Pizza recipes>]

You can also filter on a combination of fields and on related models:

Code
>>> Entry.objects.annotate(
...     search=SearchVector('blog__tagline', 'body_text'),
... ).filter(search='cheese')
[
    <Entry: Cheese on Toast recipes>,
    <Entry: Pizza Recipes>,
    <Entry: Dairy farming in Argentina>,
]

See the contrib.postgres Full text search document for complete details.

View as Markdown Edit this page on GitHub Official version
PreviousAggregation NextManagers

On this page

  • Use Cases
    • 표준적인 텍스트 질의
    • 데이터베이스의 고급 비교 함수
    • 문서 기반 검색
      • PostgreSQL 지원

An unofficial rendering of the Django documentation.

Not affiliated with or endorsed by the Django Software Foundation. The documentation is copyright © Django Software Foundation and individual contributors, and is used under the BSD 3-Clause licence. “Django” is a trademark of the Django Software Foundation.

This translation is the work of the Django i18n community, not of this site. Read the official documentation at docs.djangoproject.com.

👋 Jason Cartwright