Skip to content

Commit

Permalink
Merge pull request #3 from PSeitz/master
Browse files Browse the repository at this point in the history
update literate docs
  • Loading branch information
PSeitz authored May 13, 2024
2 parents 663a494 + fa66fb1 commit d36e34d
Show file tree
Hide file tree
Showing 27 changed files with 6,332 additions and 936 deletions.
572 changes: 572 additions & 0 deletions examples/aggregation.html

Large diffs are not rendered by default.

383 changes: 253 additions & 130 deletions examples/basic_search.html

Large diffs are not rendered by default.

374 changes: 247 additions & 127 deletions examples/custom_collector.html

Large diffs are not rendered by default.

251 changes: 180 additions & 71 deletions examples/custom_tokenizer.html

Large diffs are not rendered by default.

309 changes: 309 additions & 0 deletions examples/date_time_field.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,309 @@
<!DOCTYPE html>

<html>
<head>
<title>DateTime field example</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>

<ul id="jump_to">
<li>
<a class="large" href="javascript:void(0);">Jump To &hellip;</a>
<a class="small" href="javascript:void(0);">+</a>
<div id="jump_wrapper">
<div id="jump_page_wrapper">
<div id="jump_page">


<a class="source" href="aggregation.html">
aggregation.rs
</a>


<a class="source" href="basic_search.html">
basic_search.rs
</a>


<a class="source" href="custom_collector.html">
custom_collector.rs
</a>


<a class="source" href="custom_tokenizer.html">
custom_tokenizer.rs
</a>


<a class="source" href="date_time_field.html">
date_time_field.rs
</a>


<a class="source" href="deleting_updating_documents.html">
deleting_updating_documents.rs
</a>


<a class="source" href="faceted_search.html">
faceted_search.rs
</a>


<a class="source" href="faceted_search_with_tweaked_score.html">
faceted_search_with_tweaked_score.rs
</a>


<a class="source" href="fuzzy_search.html">
fuzzy_search.rs
</a>


<a class="source" href="index_from_multiple_threads.html">
index_from_multiple_threads.rs
</a>


<a class="source" href="index_with_json.html">
index_with_json.rs
</a>


<a class="source" href="integer_range_search.html">
integer_range_search.rs
</a>


<a class="source" href="ip_field.html">
ip_field.rs
</a>


<a class="source" href="iterating_docs_and_positions.html">
iterating_docs_and_positions.rs
</a>


<a class="source" href="json_field.html">
json_field.rs
</a>


<a class="source" href="phrase_prefix_search.html">
phrase_prefix_search.rs
</a>


<a class="source" href="pre_tokenized_text.html">
pre_tokenized_text.rs
</a>


<a class="source" href="snippet.html">
snippet.rs
</a>


<a class="source" href="stop_words.html">
stop_words.rs
</a>


<a class="source" href="warmer.html">
warmer.rs
</a>

</div>
</div>
</li>
</ul>

<ul class="sections">



<li id="section-1">
<div class="annotation">

<div class="sswrap ">
<a class="ss" href="#section-1">&#x00a7;</a>
</div>
<h1 id="datetime-field-example">DateTime field example</h1>
<p>This example shows how the DateTime field can be used</p>

</div>

<div class="content"><div class='highlight'><pre>
<span class="hljs-keyword">use</span> tantivy::collector::TopDocs;
<span class="hljs-keyword">use</span> tantivy::query::QueryParser;
<span class="hljs-keyword">use</span> tantivy::schema::{DateOptions, Document, OwnedValue, Schema, INDEXED, STORED, STRING};
<span class="hljs-keyword">use</span> tantivy::{Index, IndexWriter, TantivyDocument};

<span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() <span class="hljs-punctuation">-&gt;</span> tantivy::<span class="hljs-type">Result</span>&lt;()&gt; {</pre></div></div>

</li>


<li id="section-2">
<div class="annotation">

<div class="sswrap ">
<a class="ss" href="#section-2">&#x00a7;</a>
</div>
<h1 id="defining-the-schema">Defining the schema</h1>

</div>

<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">let</span> <span class="hljs-keyword">mut </span><span class="hljs-variable">schema_builder</span> = Schema::<span class="hljs-title function_ invoke__">builder</span>();
<span class="hljs-keyword">let</span> <span class="hljs-variable">opts</span> = DateOptions::<span class="hljs-title function_ invoke__">from</span>(INDEXED)
.<span class="hljs-title function_ invoke__">set_stored</span>()
.<span class="hljs-title function_ invoke__">set_fast</span>()
.<span class="hljs-title function_ invoke__">set_precision</span>(tantivy::schema::DateTimePrecision::Seconds);</pre></div></div>

</li>


<li id="section-3">
<div class="annotation">

<div class="sswrap ">
<a class="ss" href="#section-3">&#x00a7;</a>
</div>
<p>Add <code>occurred_at</code> date field type</p>

</div>

<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">let</span> <span class="hljs-variable">occurred_at</span> = schema_builder.<span class="hljs-title function_ invoke__">add_date_field</span>(<span class="hljs-string">&quot;occurred_at&quot;</span>, opts);
<span class="hljs-keyword">let</span> <span class="hljs-variable">event_type</span> = schema_builder.<span class="hljs-title function_ invoke__">add_text_field</span>(<span class="hljs-string">&quot;event&quot;</span>, STRING | STORED);
<span class="hljs-keyword">let</span> <span class="hljs-variable">schema</span> = schema_builder.<span class="hljs-title function_ invoke__">build</span>();</pre></div></div>

</li>


<li id="section-4">
<div class="annotation">

<div class="sswrap ">
<a class="ss" href="#section-4">&#x00a7;</a>
</div>
<h1 id="indexing-documents">Indexing documents</h1>

</div>

<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">let</span> <span class="hljs-variable">index</span> = Index::<span class="hljs-title function_ invoke__">create_in_ram</span>(schema.<span class="hljs-title function_ invoke__">clone</span>());

<span class="hljs-keyword">let</span> <span class="hljs-keyword">mut </span><span class="hljs-variable">index_writer</span>: IndexWriter = index.<span class="hljs-title function_ invoke__">writer</span>(<span class="hljs-number">50_000_000</span>)?;</pre></div></div>

</li>


<li id="section-5">
<div class="annotation">

<div class="sswrap ">
<a class="ss" href="#section-5">&#x00a7;</a>
</div>
<p>The dates are passed as string in the RFC3339 format</p>

</div>

<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">let</span> <span class="hljs-variable">doc</span> = TantivyDocument::<span class="hljs-title function_ invoke__">parse_json</span>(
&amp;schema,
<span class="hljs-string">r#&quot;{
&quot;occurred_at&quot;: &quot;2022-06-22T12:53:50.53Z&quot;,
&quot;event&quot;: &quot;pull-request&quot;
}&quot;#</span>,
)?;
index_writer.<span class="hljs-title function_ invoke__">add_document</span>(doc)?;
<span class="hljs-keyword">let</span> <span class="hljs-variable">doc</span> = TantivyDocument::<span class="hljs-title function_ invoke__">parse_json</span>(
&amp;schema,
<span class="hljs-string">r#&quot;{
&quot;occurred_at&quot;: &quot;2022-06-22T13:00:00.22Z&quot;,
&quot;event&quot;: &quot;comment&quot;
}&quot;#</span>,
)?;
index_writer.<span class="hljs-title function_ invoke__">add_document</span>(doc)?;
index_writer.<span class="hljs-title function_ invoke__">commit</span>()?;

<span class="hljs-keyword">let</span> <span class="hljs-variable">reader</span> = index.<span class="hljs-title function_ invoke__">reader</span>()?;
<span class="hljs-keyword">let</span> <span class="hljs-variable">searcher</span> = reader.<span class="hljs-title function_ invoke__">searcher</span>();</pre></div></div>

</li>


<li id="section-6">
<div class="annotation">

<div class="sswrap ">
<a class="ss" href="#section-6">&#x00a7;</a>
</div>
<h1 id="search">Search</h1>

</div>

<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">let</span> <span class="hljs-variable">query_parser</span> = QueryParser::<span class="hljs-title function_ invoke__">for_index</span>(&amp;index, <span class="hljs-built_in">vec!</span>[event_type]);
{</pre></div></div>

</li>


<li id="section-7">
<div class="annotation">

<div class="sswrap ">
<a class="ss" href="#section-7">&#x00a7;</a>
</div>
<p>Simple exact search on the date</p>

</div>

<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">let</span> <span class="hljs-variable">query</span> = query_parser.<span class="hljs-title function_ invoke__">parse_query</span>(<span class="hljs-string">&quot;occurred_at:\&quot;2022-06-22T12:53:50.53Z\&quot;&quot;</span>)?;
<span class="hljs-keyword">let</span> <span class="hljs-variable">count_docs</span> = searcher.<span class="hljs-title function_ invoke__">search</span>(&amp;*query, &amp;TopDocs::<span class="hljs-title function_ invoke__">with_limit</span>(<span class="hljs-number">5</span>))?;
<span class="hljs-built_in">assert_eq!</span>(count_docs.<span class="hljs-title function_ invoke__">len</span>(), <span class="hljs-number">1</span>);
}
{</pre></div></div>

</li>


<li id="section-8">
<div class="annotation">

<div class="sswrap ">
<a class="ss" href="#section-8">&#x00a7;</a>
</div>
<p>Range query on the date field</p>

</div>

<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">let</span> <span class="hljs-variable">query</span> = query_parser
.<span class="hljs-title function_ invoke__">parse_query</span>(<span class="hljs-string">r#&quot;occurred_at:[2022-06-22T12:58:00Z TO 2022-06-23T00:00:00Z}&quot;#</span>)?;
<span class="hljs-keyword">let</span> <span class="hljs-variable">count_docs</span> = searcher.<span class="hljs-title function_ invoke__">search</span>(&amp;*query, &amp;TopDocs::<span class="hljs-title function_ invoke__">with_limit</span>(<span class="hljs-number">4</span>))?;
<span class="hljs-built_in">assert_eq!</span>(count_docs.<span class="hljs-title function_ invoke__">len</span>(), <span class="hljs-number">1</span>);
<span class="hljs-title function_ invoke__">for</span> (_score, doc_address) <span class="hljs-keyword">in</span> count_docs {
<span class="hljs-keyword">let</span> <span class="hljs-variable">retrieved_doc</span> = searcher.doc::&lt;TantivyDocument&gt;(doc_address)?;
<span class="hljs-built_in">assert!</span>(matches!(
retrieved_doc.<span class="hljs-title function_ invoke__">get_first</span>(occurred_at),
<span class="hljs-title function_ invoke__">Some</span>(OwnedValue::<span class="hljs-title function_ invoke__">Date</span>(_))
));
<span class="hljs-built_in">assert_eq!</span>(
retrieved_doc.<span class="hljs-title function_ invoke__">to_json</span>(&amp;schema),
<span class="hljs-string">r#&quot;{&quot;event&quot;:[&quot;comment&quot;],&quot;occurred_at&quot;:[&quot;2022-06-22T13:00:00.22Z&quot;]}&quot;#</span>
);
}
}
<span class="hljs-title function_ invoke__">Ok</span>(())
}</pre></div></div>

</li>

</ul>
</div>
</body>
</html>
Loading

0 comments on commit d36e34d

Please # to comment.