From 37e71f7c634939fd521153f4b6854bc1cde3c105 Mon Sep 17 00:00:00 2001
From: Claus Matzinger
Date: Sun, 12 Mar 2017 22:59:38 -0400
Subject: [PATCH 01/18] fixes #100 and improves #99
---
examples/simple_search.rs | 120 ++++++++++++++++++++------------------
1 file changed, 62 insertions(+), 58 deletions(-)
diff --git a/examples/simple_search.rs b/examples/simple_search.rs
index 2f26ba1fb..d422b461b 100644
--- a/examples/simple_search.rs
+++ b/examples/simple_search.rs
@@ -10,105 +10,107 @@ use tantivy::collector::TopCollector;
use tantivy::query::QueryParser;
fn main() {
- // Let's create a temporary directory for the
+ // Let's create a temporary directory for the
// sake of this example
if let Ok(dir) = TempDir::new("tantivy_example_dir") {
run_example(dir.path()).unwrap();
dir.close().unwrap();
- }
+ }
}
fn run_example(index_path: &Path) -> tantivy::Result<()> {
-
-
+
+
// # Defining the schema
//
// The Tantivy index requires a very strict schema.
// The schema declares which fields are in the index,
- // and for each field, its type and "the way it should
+ // and for each field, its type and "the way it should
// be indexed".
-
-
+
+
// first we need to define a schema ...
let mut schema_builder = SchemaBuilder::default();
-
+
// Our first field is title.
// We want full-text search for it, and we want to be able
// to retrieve the document after the search.
//
// TEXT | STORED is some syntactic sugar to describe
- // that.
- //
+ // that.
+ //
// `TEXT` means the field should be tokenized and indexed,
// along with its term frequency and term positions.
//
// `STORED` means that the field will also be saved
// in a compressed, row-oriented key-value store.
- // This store is useful to reconstruct the
+ // This store is useful to reconstruct the
// documents that were selected during the search phase.
schema_builder.add_text_field("title", TEXT | STORED);
-
+
// Our first field is body.
// We want full-text search for it, and we want to be able
// to retrieve the body after the search.
schema_builder.add_text_field("body", TEXT);
-
- let schema = schema_builder.build();
+
+ let schema = schema_builder.build();
// # Indexing documents
//
// Let's create a brand new index.
- //
+ //
// This will actually just save a meta.json
// with our schema in the directory.
let index = try!(Index::create(index_path, schema.clone()));
-
-
+
+
// To insert document we need an index writer.
// There must be only one writer at a time.
// This single `IndexWriter` is already
// multithreaded.
//
- // Here we use a buffer of 1 GB. Using a bigger
+ // Here we use a buffer of 50MB. Using a bigger
// heap for the indexer can increase its throughput.
// This buffer will be split between the indexing
// threads.
- let mut index_writer = try!(index.writer(1_000_000_000));
+ let mut index_writer = try!(index.writer(50_000_000));
// Let's index our documents!
// We first need a handle on the title and the body field.
-
-
+
+
// ### Create a document "manually".
//
// We can create a document manually, by setting the fields
// one by one in a Document object.
let title = schema.get_field("title").unwrap();
let body = schema.get_field("body").unwrap();
-
+
let mut old_man_doc = Document::default();
old_man_doc.add_text(title, "The Old Man and the Sea");
- old_man_doc.add_text(body, "He was an old man who fished alone in a skiff in the Gulf Stream and he had gone eighty-four days now without taking a fish.");
-
+ old_man_doc.add_text(body,
+ "He was an old man who fished alone in a skiff in the Gulf Stream and \
+ he had gone eighty-four days now without taking a fish.");
+
// ... and add it to the `IndexWriter`.
index_writer.add_document(old_man_doc);
-
+
// ### Create a document directly from json.
//
// Alternatively, we can use our schema to parse
// a document object directly from json.
-
+
let mice_and_men_doc = try!(schema.parse_document(r#"{
"title": "Of Mice and Men",
"body": "few miles south of Soledad, the Salinas River drops in close to the hillside bank and runs deep and green. The water is warm too, for it has slipped twinkling over the yellow sands in the sunlight before reaching the narrow pool. On one side of the river the golden foothill slopes curve up to the strong and rocky Gabilan Mountains, but on the valley side the water is lined with trees—willows fresh and green with every spring, carrying in their lower leaf junctures the debris of the winter’s flooding; and sycamores with mottled, white,recumbent limbs and branches that arch over the pool"
}"#));
-
+
index_writer.add_document(mice_and_men_doc);
-
+
// Multi-valued field are allowed, they are
// expressed in JSON by an array.
// The following document has two titles.
@@ -117,19 +119,19 @@ fn run_example(index_path: &Path) -> tantivy::Result<()> {
"body": "You will rejoice to hear that no disaster has accompanied the commencement of an enterprise which you have regarded with such evil forebodings. I arrived here yesterday, and my first task is to assure my dear sister of my welfare and increasing confidence in the success of my undertaking."
}"#));
index_writer.add_document(frankenstein_doc);
-
+
// This is an example, so we will only index 3 documents
// here. You can check out tantivy's tutorial to index
- // the English wikipedia. Tantivy's indexing is rather fast.
+ // the English wikipedia. Tantivy's indexing is rather fast.
// Indexing 5 million articles of the English wikipedia takes
// around 4 minutes on my computer!
-
-
+
+
// ### Committing
- //
+ //
// At this point our documents are not searchable.
//
- //
+ //
// We need to call .commit() explicitly to force the
// index_writer to finish processing the documents in the queue,
// flush the current index to the disk, and advertise
@@ -137,22 +139,25 @@ fn run_example(index_path: &Path) -> tantivy::Result<()> {
//
// This call is blocking.
try!(index_writer.commit());
-
+
// If `.commit()` returns correctly, then all of the
// documents that have been added are guaranteed to be
// persistently indexed.
- //
+ //
// In the scenario of a crash or a power failure,
// tantivy behaves as if has rolled back to its last
// commit.
-
-
+
+
// # Searching
//
- // Let's search our index. We start
- // by creating a searcher. There can be more
- // than one searcher at a time.
- //
+ // Let's search our index. Start by reloading
+ // searchers in the index. This should be done
+ // after every commit().
+ try!(index.load_searchers());
+
+ // Afterwards create one (or more) searchers.
+ //
// You should create a searcher
// every time you start a "search query".
let searcher = index.searcher();
@@ -161,46 +166,45 @@ fn run_example(index_path: &Path) -> tantivy::Result<()> {
// Here, if the user does not specify which
// field they want to search, tantivy will search
// in both title and body.
- let query_parser = QueryParser::new(index.schema(), vec!(title, body));
-
+ let query_parser = QueryParser::new(index.schema(), vec![title, body]);
+
// QueryParser may fail if the query is not in the right
// format. For user facing applications, this can be a problem.
// A ticket has been opened regarding this problem.
let query = try!(query_parser.parse_query("sea whale"));
-
-
+
+
// A query defines a set of documents, as
// well as the way they should be scored.
- //
+ //
// A query created by the query parser is scored according
// to a metric called Tf-Idf, and will consider
// any document matching at least one of our terms.
-
- // ### Collectors
+
+ // ### Collectors
//
- // We are not interested in all of the documents but
+ // We are not interested in all of the documents but
// only in the top 10. Keeping track of our top 10 best documents
// is the role of the TopCollector.
-
let mut top_collector = TopCollector::with_limit(10);
-
+
// We can now perform our query.
try!(searcher.search(&*query, &mut top_collector));
- // Our top collector now contains the 10
+ // Our top collector now contains the 10
// most relevant doc ids...
let doc_addresses = top_collector.docs();
- // The actual documents still need to be
+ // The actual documents still need to be
// retrieved from Tantivy's store.
- //
+ //
// Since the body field was not configured as stored,
// the document returned will only contain
// a title.
-
+
for doc_address in doc_addresses {
- let retrieved_doc = try!(searcher.doc(&doc_address));
- println!("{}", schema.to_json(&retrieved_doc));
+ let retrieved_doc = try!(searcher.doc(&doc_address));
+ println!("{}", schema.to_json(&retrieved_doc));
}
Ok(())
From 292dd6dcb65d993fc59704290ae0a2ead5fc2a03 Mon Sep 17 00:00:00 2001
From: Claus Matzinger
Date: Mon, 13 Mar 2017 00:24:54 -0400
Subject: [PATCH 02/18] fixup
---
examples/simple_search.rs | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/examples/simple_search.rs b/examples/simple_search.rs
index d422b461b..430d7abf0 100644
--- a/examples/simple_search.rs
+++ b/examples/simple_search.rs
@@ -73,10 +73,8 @@ fn run_example(index_path: &Path) -> tantivy::Result<()> {
// This single `IndexWriter` is already
// multithreaded.
//
- // Here we use a buffer of 50MB. Using a bigger
+ // Here we use a buffer of 50MB per thread. Using a bigger
// heap for the indexer can increase its throughput.
- // This buffer will be split between the indexing
- // threads.
let mut index_writer = try!(index.writer(50_000_000));
// Let's index our documents!
From 50659147d12da30ff805740374744e5eaa8d24c6 Mon Sep 17 00:00:00 2001
From: Paul Masurel
Date: Tue, 14 Mar 2017 12:04:21 +0900
Subject: [PATCH 03/18] NOBUG updated simple_search.html
---
examples/html/simple_search.html | 134 +++++++++++++++++--------------
1 file changed, 74 insertions(+), 60 deletions(-)
diff --git a/examples/html/simple_search.html b/examples/html/simple_search.html
index 1cfc7ac6e..1aa6b63ab 100644
--- a/examples/html/simple_search.html
+++ b/examples/html/simple_search.html
@@ -52,7 +52,7 @@
Let’s create a temporary directory for the
sake of this example
@@ -60,7 +60,7 @@ sake of this example
ifletOk(dir) = TempDir::new("tantivy_example_dir") {
run_example(dir.path()).unwrap();
dir.close().unwrap();
- }
+ }
}
@@ -78,7 +78,7 @@ sake of this example
Defining the schema
The Tantivy index requires a very strict schema.
The schema declares which fields are in the index,
-and for each field, its type and “the way it should
+and for each field, its type and “the way it should
be indexed”.
@@ -111,12 +111,12 @@ be indexed”.
We want full-text search for it, and we want to be able
to retrieve the document after the search.
TEXT | STORED is some syntactic sugar to describe
-that.
+that.
TEXT means the field should be tokenized and indexed,
along with its term frequency and term positions.
STORED means that the field will also be saved
in a compressed, row-oriented key-value store.
-This store is useful to reconstruct the
+This store is useful to reconstruct the
documents that were selected during the search phase.
@@ -139,7 +139,7 @@ to retrieve the body after the search.
schema_builder.add_text_field("body", TEXT);
-
+
let schema = schema_builder.build();
@@ -173,14 +173,12 @@ with our schema in the directory.
There must be only one writer at a time.
This single IndexWriter is already
multithreaded.
-
Here we use a buffer of 1 GB. Using a bigger
-heap for the indexer can increase its throughput.
-This buffer will be split between the indexing
-threads.
+
Here we use a buffer of 50MB per thread. Using a bigger
+heap for the indexer can increase its throughput.
@@ -213,10 +211,12 @@ one by one in a Document object.
let title = schema.get_field("title").unwrap();
let body = schema.get_field("body").unwrap();
-
+
letmut old_man_doc = Document::default();
old_man_doc.add_text(title, "The Old Man and the Sea");
- old_man_doc.add_text(body, "He was an old man who fished alone in a skiff in the Gulf Stream and he had gone eighty-four days now without taking a fish.");
+ old_man_doc.add_text(body,
+ "He was an old man who fished alone in a skiff in the Gulf Stream and \
+ he had gone eighty-four days now without taking a fish.");
@@ -231,7 +231,7 @@ one by one in a Document object.
-
try!(index_writer.add_document(old_man_doc));
+
index_writer.add_document(old_man_doc);
@@ -248,13 +248,13 @@ a document object directly from json.
-
+
let mice_and_men_doc = try!(schema.parse_document(r#"{
"title": "Of Mice and Men",
"body": "few miles south of Soledad, the Salinas River drops in close to the hillside bank and runs deep and green. The water is warm too, for it has slipped twinkling over the yellow sands in the sunlight before reaching the narrow pool. On one side of the river the golden foothill slopes curve up to the strong and rocky Gabilan Mountains, but on the valley side the water is lined with trees—willows fresh and green with every spring, carrying in their lower leaf junctures the debris of the winter’s flooding; and sycamores with mottled, white,recumbent limbs and branches that arch over the pool"
}"#));
-
- try!(index_writer.add_document(mice_and_men_doc));
+
+ index_writer.add_document(mice_and_men_doc);
@@ -275,7 +275,7 @@ The following document has two titles.
"title": ["Frankenstein", "The Modern Promotheus"],
"body": "You will rejoice to hear that no disaster has accompanied the commencement of an enterprise which you have regarded with such evil forebodings. I arrived here yesterday, and my first task is to assure my dear sister of my welfare and increasing confidence in the success of my undertaking."
}"#));
- try!(index_writer.add_document(frankenstein_doc));
+ index_writer.add_document(frankenstein_doc);
@@ -288,7 +288,7 @@ The following document has two titles.
This is an example, so we will only index 3 documents
here. You can check out tantivy’s tutorial to index
-the English wikipedia. Tantivy’s indexing is rather fast.
+the English wikipedia. Tantivy’s indexing is rather fast.
Indexing 5 million articles of the English wikipedia takes
around 4 minutes on my computer!
The query parser can interpret human queries.
-Here, if the user does not specify which
-field they want to search, tantivy will search
-in both title and body.
+
Afterwards create one (or more) searchers.
+
You should create a searcher
+every time you start a “search query”.
-
let query_parser = QueryParser::new(index.schema(), vec!(title, body));
The query parser can interpret human queries.
+Here, if the user does not specify which
+field they want to search, tantivy will search
+in both title and body.
+
+
+
+
let query_parser = QueryParser::new(index.schema(), vec![title, body]);
QueryParser may fail if the query is not in the right
format. For user facing applications, this can be a problem.
A ticket has been opened regarding this problem.
@@ -391,11 +406,11 @@ A ticket has been opened regarding this problem.
-