{"title":"검색","version":"3.2","locale":"ko","docname":"topics/db/search","url":"/ko/3.2/topics/db/search/","canonical":"https://djangodocs.dev/ko/3.2/topics/db/search/","summary":"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…","html":"<h1>검색<a class=\"heading-anchor\" href=\"#search\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>A common task for web applications is to search some data in the database with\nuser input. In a simple case, this could be filtering a list of objects by a\ncategory. A more complex use case might require searching with weighting,\ncategorization, highlighting, multiple languages, and so on. This document\nexplains some of the possible use cases and the tools you can use.</p>\n<p>We’ll refer to the same models used in <a class=\"reference internal\" href=\"/ko/3.2/topics/db/queries/\"><span class=\"doc\">쿼리 만들기</span></a>.</p>\n<section id=\"use-cases\">\n<h2>Use Cases<a class=\"heading-anchor\" href=\"#use-cases\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"standard-textual-queries\">\n<h3>표준적인 텍스트 질의<a class=\"heading-anchor\" href=\"#standard-textual-queries\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Text-based fields have a selection of matching operations. For example, you may\nwish to allow lookup up an author like so:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">Author</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">name__contains</span><span class=\"o\">=</span><span class=\"s1\">&#39;Terry&#39;</span><span class=\"p\">)</span>\n<span class=\"go\">[&lt;Author: Terry Gilliam&gt;, &lt;Author: Terry Jones&gt;]</span>\n</code></pre></div>\n<p>This is a very fragile solution as it requires the user to know an exact\nsubstring of the author’s name. A better approach could be a case-insensitive\nmatch (<a class=\"reference internal\" href=\"/ko/3.2/ref/models/querysets/#std-fieldlookup-icontains\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">icontains</span></code></a>), but this is only marginally better.</p>\n</section>\n<section id=\"a-database-s-more-advanced-comparison-functions\">\n<h3>데이터베이스의 고급 비교 함수<a class=\"heading-anchor\" href=\"#a-database-s-more-advanced-comparison-functions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you’re using PostgreSQL, Django provides <a class=\"reference internal\" href=\"/ko/3.2/ref/contrib/postgres/search/\"><span class=\"doc\">a selection of database\nspecific tools</span></a> to allow you to leverage more\ncomplex querying options. Other databases have different selections of tools,\npossibly via plugins or user-defined functions. Django doesn’t include any\nsupport for them at this time. We’ll use some examples from PostgreSQL to\ndemonstrate the kind of functionality databases may have.</p>\n<aside class=\"admonition-searching-in-other-databases admonition\">\n<p class=\"admonition-title\">다른 데이터베이스에서 검색하기</p>\n<p>All of the searching tools provided by <a class=\"reference internal\" href=\"/ko/3.2/ref/contrib/postgres/#module-django.contrib.postgres\" title=\"django.contrib.postgres: PostgreSQL-specific fields and features\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.postgres</span></code></a> are\nconstructed entirely on public APIs such as <a class=\"reference internal\" href=\"/ko/3.2/ref/models/lookups/\"><span class=\"doc\">custom lookups</span></a> and <a class=\"reference internal\" href=\"/ko/3.2/ref/models/database-functions/\"><span class=\"doc\">database functions</span></a>. Depending on your database, you should\nbe able to construct queries to allow similar APIs. If there are specific\nthings which cannot be achieved this way, please open a ticket.</p>\n</aside>\n<p>In the above example, we determined that a case insensitive lookup would be\nmore useful. When dealing with non-English names, a further improvement is to\nuse <a class=\"reference internal\" href=\"/ko/3.2/ref/contrib/postgres/lookups/#std-fieldlookup-unaccent\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">unaccented</span> <span class=\"pre\">comparison</span></code></a>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">Author</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">name__unaccent__icontains</span><span class=\"o\">=</span><span class=\"s1\">&#39;Helen&#39;</span><span class=\"p\">)</span>\n<span class=\"go\">[&lt;Author: Helen Mirren&gt;, &lt;Author: Helena Bonham Carter&gt;, &lt;Author: Hélène Joy&gt;]</span>\n</code></pre></div>\n<p>This shows another issue, where we are matching against a different spelling of\nthe name. In this case we have an asymmetry though - a search for <code class=\"docutils literal notranslate\"><span class=\"pre\">Helen</span></code>\nwill pick up <code class=\"docutils literal notranslate\"><span class=\"pre\">Helena</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">Hélène</span></code>, but not the reverse. Another option\nwould be to use a <a class=\"reference internal\" href=\"/ko/3.2/ref/contrib/postgres/lookups/#std-fieldlookup-trigram_similar\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">trigram_similar</span></code></a> comparison, which compares\nsequences of letters.</p>\n<p>예시:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">Author</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">name__unaccent__lower__trigram_similar</span><span class=\"o\">=</span><span class=\"s1\">&#39;Hélène&#39;</span><span class=\"p\">)</span>\n<span class=\"go\">[&lt;Author: Helen Mirren&gt;, &lt;Author: Hélène Joy&gt;]</span>\n</code></pre></div>\n<p>Now we have a different problem - the longer name of “Helena Bonham Carter”\ndoesn’t show up as it is much longer. Trigram searches consider all\ncombinations of three letters, and compares how many appear in both search and\nsource strings. For the longer name, there are more combinations that don’t\nappear in the source string, so it is no longer considered a close match.</p>\n<p>The correct choice of comparison functions here depends on your particular data\nset, for example the language(s) used and the type of text being searched. All\nof the examples we’ve seen are on short strings where the user is likely to\nenter something close (by varying definitions) to the source data.</p>\n</section>\n<section id=\"document-based-search\">\n<h3>문서 기반 검색<a class=\"heading-anchor\" href=\"#document-based-search\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Standard database operations stop being a useful approach when you start\nconsidering large blocks of text. Whereas the examples above can be thought of\nas operations on a string of characters, full text search looks at the actual\nwords. Depending on the system used, it’s likely to use some of the following\nideas:</p>\n<ul class=\"simple\">\n<li><p>Ignoring “stop words” such as “a”, “the”, “and”.</p></li>\n<li><p>Stemming words, so that “pony” and “ponies” are considered similar.</p></li>\n<li><p>Weighting words based on different criteria such as how frequently they\nappear in the text, or the importance of the fields, such as the title or\nkeywords, that they appear in.</p></li>\n</ul>\n<p>There are many alternatives for using searching software, some of the most\nprominent are <a class=\"reference external\" href=\"https://www.elastic.co/\">Elastic</a> and <a class=\"reference external\" href=\"https://solr.apache.org/\">Solr</a>. These are full document-based search\nsolutions. To use them with data from Django models, you’ll need a layer which\ntranslates your data into a textual document, including back-references to the\ndatabase ids. When a search using the engine returns a certain document, you\ncan then look it up in the database. There are a variety of third-party\nlibraries which are designed to help with this process.</p>\n<section id=\"postgresql-support\">\n<h4>PostgreSQL 지원<a class=\"heading-anchor\" href=\"#postgresql-support\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>PostgreSQL has its own full text search implementation built-in. While not as\npowerful as some other search engines, it has the advantage of being inside\nyour database and so can easily be combined with other relational queries such\nas categorization.</p>\n<p>The <a class=\"reference internal\" href=\"/ko/3.2/ref/contrib/postgres/#module-django.contrib.postgres\" title=\"django.contrib.postgres: PostgreSQL-specific fields and features\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.postgres</span></code></a> module provides some helpers to make these\nqueries. For example, a query might select all the blog entries which mention\n“cheese”:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">Entry</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">body_text__search</span><span class=\"o\">=</span><span class=\"s1\">&#39;cheese&#39;</span><span class=\"p\">)</span>\n<span class=\"go\">[&lt;Entry: Cheese on Toast recipes&gt;, &lt;Entry: Pizza recipes&gt;]</span>\n</code></pre></div>\n<p>You can also filter on a combination of fields and on related models:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">Entry</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">annotate</span><span class=\"p\">(</span>\n<span class=\"gp\">... </span>    <span class=\"n\">search</span><span class=\"o\">=</span><span class=\"n\">SearchVector</span><span class=\"p\">(</span><span class=\"s1\">&#39;blog__tagline&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;body_text&#39;</span><span class=\"p\">),</span>\n<span class=\"gp\">... </span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">search</span><span class=\"o\">=</span><span class=\"s1\">&#39;cheese&#39;</span><span class=\"p\">)</span>\n<span class=\"go\">[</span>\n<span class=\"go\">    &lt;Entry: Cheese on Toast recipes&gt;,</span>\n<span class=\"go\">    &lt;Entry: Pizza Recipes&gt;,</span>\n<span class=\"go\">    &lt;Entry: Dairy farming in Argentina&gt;,</span>\n<span class=\"go\">]</span>\n</code></pre></div>\n<p>See the <code class=\"docutils literal notranslate\"><span class=\"pre\">contrib.postgres</span></code> <a class=\"reference internal\" href=\"/ko/3.2/ref/contrib/postgres/search/\"><span class=\"doc\">Full text search</span></a> document for\ncomplete details.</p>\n</section>\n</section>\n</section>","rootId":"search","toc":[{"title":"Use Cases","anchor":"use-cases","children":[{"title":"표준적인 텍스트 질의","anchor":"standard-textual-queries","children":[]},{"title":"데이터베이스의 고급 비교 함수","anchor":"a-database-s-more-advanced-comparison-functions","children":[]},{"title":"문서 기반 검색","anchor":"document-based-search","children":[{"title":"PostgreSQL 지원","anchor":"postgresql-support","children":[]}]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Django 사용하기","url":"/ko/3.2/topics/"},{"docname":"topics/db/index","title":"모델과 데이터베이스","url":"/ko/3.2/topics/db/"}],"prev":{"docname":"topics/db/aggregation","title":"Aggregation","url":"/ko/3.2/topics/db/aggregation/"},"next":{"docname":"topics/db/managers","title":"Managers","url":"/ko/3.2/topics/db/managers/"},"formats":{"html":"/ko/3.2/topics/db/search/","markdown":"/ko/3.2/topics/db/search.md","json":"/ko/3.2/topics/db/search.json"},"source":"https://github.com/django/django/blob/stable/3.2.x/docs/topics/db/search.txt","official":"https://docs.djangoproject.com/ko/3.2/topics/db/search/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}