/usr/share/doc/python-whoosh-doc/html/quickstart.html is in python-whoosh-doc 2.5.7-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Quick start — Whoosh 2.5.7 documentation</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '2.5.7',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="Whoosh 2.5.7 documentation" href="index.html" />
<link rel="next" title="Introduction to Whoosh" href="intro.html" />
<link rel="prev" title="Whoosh 0.3 release notes" href="releases/0_3.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="intro.html" title="Introduction to Whoosh"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="releases/0_3.html" title="Whoosh 0.3 release notes"
accesskey="P">previous</a> |</li>
<li><a href="index.html">Whoosh 2.5.7 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="quick-start">
<h1>Quick start<a class="headerlink" href="#quick-start" title="Permalink to this headline">¶</a></h1>
<p>Whoosh is a library of classes and functions for indexing text and then searching the index.
It allows you to develop custom search engines for your content. For example, if you were
creating blogging software, you could use Whoosh to add a search function to allow users to
search blog entries.</p>
<div class="section" id="a-quick-introduction">
<h2>A quick introduction<a class="headerlink" href="#a-quick-introduction" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">whoosh.index</span> <span class="kn">import</span> <span class="n">create_in</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">whoosh.fields</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">>>> </span><span class="n">schema</span> <span class="o">=</span> <span class="n">Schema</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="n">TEXT</span><span class="p">(</span><span class="n">stored</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span> <span class="n">path</span><span class="o">=</span><span class="n">ID</span><span class="p">(</span><span class="n">stored</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span> <span class="n">content</span><span class="o">=</span><span class="n">TEXT</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">ix</span> <span class="o">=</span> <span class="n">create_in</span><span class="p">(</span><span class="s">"indexdir"</span><span class="p">,</span> <span class="n">schema</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">writer</span> <span class="o">=</span> <span class="n">ix</span><span class="o">.</span><span class="n">writer</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">writer</span><span class="o">.</span><span class="n">add_document</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">u"First document"</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s">u"/a"</span><span class="p">,</span>
<span class="gp">... </span> <span class="n">content</span><span class="o">=</span><span class="s">u"This is the first document we've added!"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">writer</span><span class="o">.</span><span class="n">add_document</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">u"Second document"</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s">u"/b"</span><span class="p">,</span>
<span class="gp">... </span> <span class="n">content</span><span class="o">=</span><span class="s">u"The second one is even more interesting!"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">writer</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">whoosh.qparser</span> <span class="kn">import</span> <span class="n">QueryParser</span>
<span class="gp">>>> </span><span class="k">with</span> <span class="n">ix</span><span class="o">.</span><span class="n">searcher</span><span class="p">()</span> <span class="k">as</span> <span class="n">searcher</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">query</span> <span class="o">=</span> <span class="n">QueryParser</span><span class="p">(</span><span class="s">"content"</span><span class="p">,</span> <span class="n">ix</span><span class="o">.</span><span class="n">schema</span><span class="p">)</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s">"first"</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">results</span> <span class="o">=</span> <span class="n">searcher</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">query</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">results</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="gp">...</span>
<span class="go">{"title": u"First document", "path": u"/a"}</span>
</pre></div>
</div>
</div>
<div class="section" id="the-index-and-schema-objects">
<h2>The <tt class="docutils literal"><span class="pre">Index</span></tt> and <tt class="docutils literal"><span class="pre">Schema</span></tt> objects<a class="headerlink" href="#the-index-and-schema-objects" title="Permalink to this headline">¶</a></h2>
<p>To begin using Whoosh, you need an <em>index object</em>. The first time you create
an index, you must define the index’s <em>schema</em>. The schema lists the <em>fields</em>
in the index. A field is a piece of information for each document in the index,
such as its title or text content. A field can be <em>indexed</em> (meaning it can
be searched) and/or <em>stored</em> (meaning the value that gets indexed is returned
with the results; this is useful for fields such as the title).</p>
<p>This schema has two fields, “title” and “content”:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">whoosh.fields</span> <span class="kn">import</span> <span class="n">Schema</span><span class="p">,</span> <span class="n">TEXT</span>
<span class="n">schema</span> <span class="o">=</span> <span class="n">Schema</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="n">TEXT</span><span class="p">,</span> <span class="n">content</span><span class="o">=</span><span class="n">TEXT</span><span class="p">)</span>
</pre></div>
</div>
<p>You only need to do create the schema once, when you create the index. The
schema is pickled and stored with the index.</p>
<p>When you create the <tt class="docutils literal"><span class="pre">Schema</span></tt> object, you use keyword arguments to map field names
to field types. The list of fields and their types defines what you are indexing
and what’s searchable. Whoosh comes with some very useful predefined field
types, and you can easily create your own.</p>
<dl class="docutils">
<dt><a class="reference internal" href="api/fields.html#whoosh.fields.ID" title="whoosh.fields.ID"><tt class="xref py py-class docutils literal"><span class="pre">whoosh.fields.ID</span></tt></a></dt>
<dd>This type simply indexes (and optionally stores) the entire value of the
field as a single unit (that is, it doesn’t break it up into individual
words). This is useful for fields such as a file path, URL, date, category,
etc.</dd>
<dt><a class="reference internal" href="api/fields.html#whoosh.fields.STORED" title="whoosh.fields.STORED"><tt class="xref py py-class docutils literal"><span class="pre">whoosh.fields.STORED</span></tt></a></dt>
<dd>This field is stored with the document, but not indexed. This field type is
not indexed and not searchable. This is useful for document information you
want to display to the user in the search results.</dd>
<dt><a class="reference internal" href="api/fields.html#whoosh.fields.KEYWORD" title="whoosh.fields.KEYWORD"><tt class="xref py py-class docutils literal"><span class="pre">whoosh.fields.KEYWORD</span></tt></a></dt>
<dd>This type is designed for space- or comma-separated keywords. This type is
indexed and searchable (and optionally stored). To save space, it does not
support phrase searching.</dd>
<dt><a class="reference internal" href="api/fields.html#whoosh.fields.TEXT" title="whoosh.fields.TEXT"><tt class="xref py py-class docutils literal"><span class="pre">whoosh.fields.TEXT</span></tt></a></dt>
<dd>This type is for body text. It indexes (and optionally stores) the text and
stores term positions to allow phrase searching.</dd>
<dt><a class="reference internal" href="api/fields.html#whoosh.fields.NUMERIC" title="whoosh.fields.NUMERIC"><tt class="xref py py-class docutils literal"><span class="pre">whoosh.fields.NUMERIC</span></tt></a></dt>
<dd>This type is for numbers. You can store integers or floating point numbers.</dd>
<dt><a class="reference internal" href="api/fields.html#whoosh.fields.BOOLEAN" title="whoosh.fields.BOOLEAN"><tt class="xref py py-class docutils literal"><span class="pre">whoosh.fields.BOOLEAN</span></tt></a></dt>
<dd>This type is for boolean (true/false) values.</dd>
<dt><a class="reference internal" href="api/fields.html#whoosh.fields.DATETIME" title="whoosh.fields.DATETIME"><tt class="xref py py-class docutils literal"><span class="pre">whoosh.fields.DATETIME</span></tt></a></dt>
<dd>This type is for <tt class="docutils literal"><span class="pre">datetime</span></tt> objects. See <a class="reference internal" href="dates.html"><em>Indexing and parsing dates/times</em></a> for more
information.</dd>
<dt><a class="reference internal" href="api/fields.html#whoosh.fields.NGRAM" title="whoosh.fields.NGRAM"><tt class="xref py py-class docutils literal"><span class="pre">whoosh.fields.NGRAM</span></tt></a> and <a class="reference internal" href="api/fields.html#whoosh.fields.NGRAMWORDS" title="whoosh.fields.NGRAMWORDS"><tt class="xref py py-class docutils literal"><span class="pre">whoosh.fields.NGRAMWORDS</span></tt></a></dt>
<dd>These types break the field text or individual terms into N-grams.
See <a class="reference internal" href="ngrams.html"><em>Indexing and searching N-grams</em></a> for more information.</dd>
</dl>
<p>(As a shortcut, if you don’t need to pass any arguments to the field type, you
can just give the class name and Whoosh will instantiate the object for you.)</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">whoosh.fields</span> <span class="kn">import</span> <span class="n">Schema</span><span class="p">,</span> <span class="n">STORED</span><span class="p">,</span> <span class="n">ID</span><span class="p">,</span> <span class="n">KEYWORD</span><span class="p">,</span> <span class="n">TEXT</span>
<span class="n">schema</span> <span class="o">=</span> <span class="n">Schema</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="n">TEXT</span><span class="p">(</span><span class="n">stored</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span> <span class="n">content</span><span class="o">=</span><span class="n">TEXT</span><span class="p">,</span>
<span class="n">path</span><span class="o">=</span><span class="n">ID</span><span class="p">(</span><span class="n">stored</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span> <span class="n">tags</span><span class="o">=</span><span class="n">KEYWORD</span><span class="p">,</span> <span class="n">icon</span><span class="o">=</span><span class="n">STORED</span><span class="p">)</span>
</pre></div>
</div>
<p>See <a class="reference internal" href="schema.html"><em>Designing a schema</em></a> for more information.</p>
<p>Once you have the schema, you can create an index using the <tt class="docutils literal"><span class="pre">create_in</span></tt>
function:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">os.path</span>
<span class="kn">from</span> <span class="nn">whoosh.index</span> <span class="kn">import</span> <span class="n">create_in</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s">"index"</span><span class="p">):</span>
<span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="s">"index"</span><span class="p">)</span>
<span class="n">ix</span> <span class="o">=</span> <span class="n">create_in</span><span class="p">(</span><span class="s">"index"</span><span class="p">,</span> <span class="n">schema</span><span class="p">)</span>
</pre></div>
</div>
<p>(At a low level, this creates a <em>Storage</em> object to contain the index. A
<tt class="docutils literal"><span class="pre">Storage</span></tt> object represents that medium in which the index will be stored.
Usually this will be <tt class="docutils literal"><span class="pre">FileStorage</span></tt>, which stores the index as a set of files
in a directory.)</p>
<p>After you’ve created an index, you can open it using the <tt class="docutils literal"><span class="pre">open_dir</span></tt>
convenience function:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">whoosh.index</span> <span class="kn">import</span> <span class="n">open_dir</span>
<span class="n">ix</span> <span class="o">=</span> <span class="n">open_dir</span><span class="p">(</span><span class="s">"index"</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="the-indexwriter-object">
<h2>The <tt class="docutils literal"><span class="pre">IndexWriter</span></tt> object<a class="headerlink" href="#the-indexwriter-object" title="Permalink to this headline">¶</a></h2>
<p>OK, so we’ve got an <tt class="docutils literal"><span class="pre">Index</span></tt> object, now we can start adding documents. The
<tt class="docutils literal"><span class="pre">writer()</span></tt> method of the <tt class="docutils literal"><span class="pre">Index</span></tt> object returns an <tt class="docutils literal"><span class="pre">IndexWriter</span></tt> object that lets
you add documents to the index. The IndexWriter’s <tt class="docutils literal"><span class="pre">add_document(**kwargs)</span></tt>
method accepts keyword arguments where the field name is mapped to a value:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">writer</span> <span class="o">=</span> <span class="n">ix</span><span class="o">.</span><span class="n">writer</span><span class="p">()</span>
<span class="n">writer</span><span class="o">.</span><span class="n">add_document</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">u"My document"</span><span class="p">,</span> <span class="n">content</span><span class="o">=</span><span class="s">u"This is my document!"</span><span class="p">,</span>
<span class="n">path</span><span class="o">=</span><span class="s">u"/a"</span><span class="p">,</span> <span class="n">tags</span><span class="o">=</span><span class="s">u"first short"</span><span class="p">,</span> <span class="n">icon</span><span class="o">=</span><span class="s">u"/icons/star.png"</span><span class="p">)</span>
<span class="n">writer</span><span class="o">.</span><span class="n">add_document</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">u"Second try"</span><span class="p">,</span> <span class="n">content</span><span class="o">=</span><span class="s">u"This is the second example."</span><span class="p">,</span>
<span class="n">path</span><span class="o">=</span><span class="s">u"/b"</span><span class="p">,</span> <span class="n">tags</span><span class="o">=</span><span class="s">u"second short"</span><span class="p">,</span> <span class="n">icon</span><span class="o">=</span><span class="s">u"/icons/sheep.png"</span><span class="p">)</span>
<span class="n">writer</span><span class="o">.</span><span class="n">add_document</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">u"Third time's the charm"</span><span class="p">,</span> <span class="n">content</span><span class="o">=</span><span class="s">u"Examples are many."</span><span class="p">,</span>
<span class="n">path</span><span class="o">=</span><span class="s">u"/c"</span><span class="p">,</span> <span class="n">tags</span><span class="o">=</span><span class="s">u"short"</span><span class="p">,</span> <span class="n">icon</span><span class="o">=</span><span class="s">u"/icons/book.png"</span><span class="p">)</span>
<span class="n">writer</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
</pre></div>
</div>
<p>Two important notes:</p>
<ul class="simple">
<li>You don’t have to fill in a value for every field. Whoosh doesn’t care if you
leave out a field from a document.</li>
<li>Indexed text fields must be passed a unicode value. Fields that are stored
but not indexed (<tt class="docutils literal"><span class="pre">STORED</span></tt> field type) can be passed any pickle-able object.</li>
</ul>
<p>If you have a text field that is both indexed and stored, you can index a
unicode value but store a different object if necessary (it’s usually not, but
sometimes this is really useful) using this trick:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">writer</span><span class="o">.</span><span class="n">add_document</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">u"Title to be indexed"</span><span class="p">,</span> <span class="n">_stored_title</span><span class="o">=</span><span class="s">u"Stored title"</span><span class="p">)</span>
</pre></div>
</div>
<p>Calling commit() on the <tt class="docutils literal"><span class="pre">IndexWriter</span></tt> saves the added documents to the index:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">writer</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
</pre></div>
</div>
<p>See <a class="reference internal" href="indexing.html"><em>How to index documents</em></a> for more information.</p>
<p>Once your documents are committed to the index, you can search for them.</p>
</div>
<div class="section" id="the-searcher-object">
<h2>The <tt class="docutils literal"><span class="pre">Searcher</span></tt> object<a class="headerlink" href="#the-searcher-object" title="Permalink to this headline">¶</a></h2>
<p>To begin searching the index, we’ll need a <tt class="docutils literal"><span class="pre">Searcher</span></tt> object:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">searcher</span> <span class="o">=</span> <span class="n">ix</span><span class="o">.</span><span class="n">searcher</span><span class="p">()</span>
</pre></div>
</div>
<p>You’ll usually want to open the searcher using a <tt class="docutils literal"><span class="pre">with</span></tt> statement so the
searcher is automatically closed when you’re done with it (searcher objects
represent a number of open files, so if you don’t explicitly close them and the
system is slow to collect them, you can run out of file handles):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">with</span> <span class="n">ix</span><span class="o">.</span><span class="n">searcher</span><span class="p">()</span> <span class="k">as</span> <span class="n">searcher</span><span class="p">:</span>
<span class="o">...</span>
</pre></div>
</div>
<p>This is of course equivalent to:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span>
<span class="n">searcher</span> <span class="o">=</span> <span class="n">ix</span><span class="o">.</span><span class="n">searcher</span><span class="p">()</span>
<span class="o">...</span>
<span class="k">finally</span><span class="p">:</span>
<span class="n">searcher</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>The Searcher’s <tt class="docutils literal"><span class="pre">search()</span></tt> method takes a <em>Query object</em>. You can construct
query objects directly or use a query parser to parse a query string.</p>
<p>For example, this query would match documents that contain both “apple” and
“bear” in the “content” field:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Construct query objects directly</span>
<span class="kn">from</span> <span class="nn">whoosh.query</span> <span class="kn">import</span> <span class="o">*</span>
<span class="n">myquery</span> <span class="o">=</span> <span class="n">And</span><span class="p">([</span><span class="n">Term</span><span class="p">(</span><span class="s">"content"</span><span class="p">,</span> <span class="s">u"apple"</span><span class="p">),</span> <span class="n">Term</span><span class="p">(</span><span class="s">"content"</span><span class="p">,</span> <span class="s">"bear"</span><span class="p">)])</span>
</pre></div>
</div>
<p>To parse a query string, you can use the default query parser in the <tt class="docutils literal"><span class="pre">qparser</span></tt>
module. The first argument to the <tt class="docutils literal"><span class="pre">QueryParser</span></tt> constructor is the default
field to search. This is usually the “body text” field. The second optional
argument is a schema to use to understand how to parse the fields:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Parse a query string</span>
<span class="kn">from</span> <span class="nn">whoosh.qparser</span> <span class="kn">import</span> <span class="n">QueryParser</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">QueryParser</span><span class="p">(</span><span class="s">"content"</span><span class="p">,</span> <span class="n">ix</span><span class="o">.</span><span class="n">schema</span><span class="p">)</span>
<span class="n">myquery</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">querystring</span><span class="p">)</span>
</pre></div>
</div>
<p>Once you have a <tt class="docutils literal"><span class="pre">Searcher</span></tt> and a query object, you can use the <tt class="docutils literal"><span class="pre">Searcher</span></tt>‘s
<tt class="docutils literal"><span class="pre">search()</span></tt> method to run the query and get a <tt class="docutils literal"><span class="pre">Results</span></tt> object:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">results</span> <span class="o">=</span> <span class="n">searcher</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">myquery</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">results</span><span class="p">))</span>
<span class="go">1</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">results</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="go">{"title": "Second try", "path": "/b", "icon": "/icons/sheep.png"}</span>
</pre></div>
</div>
<p>The default <tt class="docutils literal"><span class="pre">QueryParser</span></tt> implements a query language very similar to
Lucene’s. It lets you connect terms with <tt class="docutils literal"><span class="pre">AND</span></tt> or <tt class="docutils literal"><span class="pre">OR</span></tt>, eleminate terms with
<tt class="docutils literal"><span class="pre">NOT</span></tt>, group terms together into clauses with parentheses, do range, prefix,
and wilcard queries, and specify different fields to search. By default it joins
clauses together with <tt class="docutils literal"><span class="pre">AND</span></tt> (so by default, all terms you specify must be in
the document for the document to match):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s">u"render shade animate"</span><span class="p">))</span>
<span class="go">And([Term("content", "render"), Term("content", "shade"), Term("content", "animate")])</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s">u"render OR (title:shade keyword:animate)"</span><span class="p">))</span>
<span class="go">Or([Term("content", "render"), And([Term("title", "shade"), Term("keyword", "animate")])])</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s">u"rend*"</span><span class="p">))</span>
<span class="go">Prefix("content", "rend")</span>
</pre></div>
</div>
<p>Whoosh includes extra features for dealing with search results, such as</p>
<ul class="simple">
<li>Sorting results by the value of an indexed field, instead of by relelvance.</li>
<li>Highlighting the search terms in excerpts from the original documents.</li>
<li>Expanding the query terms based on the top few documents found.</li>
<li>Paginating the results (e.g. “Showing results 1-20, page 1 of 4”).</li>
</ul>
<p>See <a class="reference internal" href="searching.html"><em>How to search</em></a> for more information.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Quick start</a><ul>
<li><a class="reference internal" href="#a-quick-introduction">A quick introduction</a></li>
<li><a class="reference internal" href="#the-index-and-schema-objects">The <tt class="docutils literal"><span class="pre">Index</span></tt> and <tt class="docutils literal"><span class="pre">Schema</span></tt> objects</a></li>
<li><a class="reference internal" href="#the-indexwriter-object">The <tt class="docutils literal"><span class="pre">IndexWriter</span></tt> object</a></li>
<li><a class="reference internal" href="#the-searcher-object">The <tt class="docutils literal"><span class="pre">Searcher</span></tt> object</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="releases/0_3.html"
title="previous chapter">Whoosh 0.3 release notes</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="intro.html"
title="next chapter">Introduction to Whoosh</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/quickstart.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="intro.html" title="Introduction to Whoosh"
>next</a> |</li>
<li class="right" >
<a href="releases/0_3.html" title="Whoosh 0.3 release notes"
>previous</a> |</li>
<li><a href="index.html">Whoosh 2.5.7 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2007-2012 Matt Chaput.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.2.
</div>
</body>
</html>
|