You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 6.8KB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
  2. `tantivy-cli` is the project hosting the command line interface for [tantivy](https://github.com/fulmicoton/tantivy), a search engine project.
  3. # Tutorial: Indexing Wikipedia with Tantivy CLI
  4. ## Introduction
  5. In this tutorial, we will create a brand new index with the articles of English wikipedia in it.
  6. ## Installing the tantivy CLI.
  7. There are a couple ways to install `tantivy-cli`.
  8. If you are a Rust programmer, you probably have `cargo` installed and you can just
  9. run `cargo install tantivy-cli`.
  10. Alternatively, if you are on 64-bit Linux, you can directly download a
  11. static binary: [binaries/linux_x86_64/](http://fulmicoton.com/tantivy-files/binaries/0.2.0/linux_x86_64/tantivy),
  12. and save it in a directory on your system's `PATH`.
  13. ## Creating the index: `new`
  14. Let's create a directory in which your index will be stored.
  15. ```bash
  16. # create the directory
  17. mkdir wikipedia-index
  18. ```
  19. We will now initialize the index and create its schema.
  20. The [schema](http://fulmicoton.com/tantivy/tantivy/schema/index.html) defines
  21. the list of your fields, and for each field:
  22. - its name
  23. - its type, currently `u32` or `str`
  24. - how it should be indexed.
  25. You can find more information about the latter on
  26. [tantivy's schema documentation page](http://fulmicoton.com/tantivy/tantivy/schema/index.html
  27. In our case, our documents will contain
  28. * a title
  29. * a body
  30. * a url
  31. We want the title and the body to be tokenized and indexed. We also want
  32. to add the term frequency and term positions to our index.
  33. (To be honest, phrase queries are not yet implemented in tantivy,
  34. so the positions won't be really useful in this tutorial.)
  35. Running `tantivy new` will start a wizard that will help you
  36. define the schema of the new index.
  37. Like all the other commands of `tantivy`, you will have to
  38. pass it your index directory via the `-i` or `--index`
  39. parameter as follows:
  40. ```bash
  41. tantivy new -i wikipedia-index
  42. ```
  43. Answer the questions as follows:
  44. ```none
  45. Creating new index
  46. Let's define its schema!
  47. New field name ? title
  48. Text or unsigned 32-bit integer (T/I) ? T
  49. Should the field be stored (Y/N) ? Y
  50. Should the field be indexed (Y/N) ? Y
  51. Should the field be tokenized (Y/N) ? Y
  52. Should the term frequencies (per doc) be in the index (Y/N) ? Y
  53. Should the term positions (per doc) be in the index (Y/N) ? Y
  54. Add another field (Y/N) ? Y
  55. New field name ? body
  56. Text or unsigned 32-bit integer (T/I) ? T
  57. Should the field be stored (Y/N) ? Y
  58. Should the field be indexed (Y/N) ? Y
  59. Should the field be tokenized (Y/N) ? Y
  60. Should the term frequencies (per doc) be in the index (Y/N) ? Y
  61. Should the term positions (per doc) be in the index (Y/N) ? Y
  62. Add another field (Y/N) ? Y
  63. New field name ? url
  64. Text or unsigned 32-bit integer (T/I) ? T
  65. Should the field be stored (Y/N) ? Y
  66. Should the field be indexed (Y/N) ? N
  67. Add another field (Y/N) ? N
  68. [
  69. {
  70. "name": "title",
  71. "type": "text",
  72. "options": {
  73. "indexing": "position",
  74. "stored": true
  75. }
  76. },
  77. {
  78. "name": "body",
  79. "type": "text",
  80. "options": {
  81. "indexing": "position",
  82. "stored": true
  83. }
  84. },
  85. {
  86. "name": "url",
  87. "type": "text",
  88. "options": {
  89. "indexing": "unindexed",
  90. "stored": true
  91. }
  92. }
  93. ]
  94. ```
  95. After the wizard has finished, a `meta.json` should exist in `wikipedia-index/meta.json`.
  96. It is a fairly human readable JSON, so you can check its content.
  97. It contains two sections:
  98. - segments (currently empty, but we will change that soon)
  99. - schema
  100. # Indexing the document: `index`
  101. Tantivy's `index` command offers a way to index a json file.
  102. The file must contain one JSON object per line.
  103. The structure of this JSON object must match that of our schema definition.
  104. ```json
  105. {"body": "some text", "title": "some title", "url": "http://somedomain.com"}
  106. ```
  107. For this tutorial, you can download a corpus with the 5 million+ English Wikipedia articles in the right format here: [wiki-articles.json (2.34 GB)](https://www.dropbox.com/s/wwnfnu441w1ec9p/wiki-articles.json.bz2?dl=0).
  108. Make sure to decompress the file
  109. ```bash
  110. bunzip2 wiki-articles.json.bz2
  111. ```
  112. If you are in a rush you can [download 100 articles in the right format here (11 MB)](http://fulmicoton.com/tantivy-files/wiki-articles-1000.json).
  113. The `index` command will index your document.
  114. By default it will use as 3 thread, each with a buffer size of 1GB split a
  115. accross these threads.
  116. ```
  117. cat wiki-articles.json | tantivy index -i ./wikipedia-index
  118. ```
  119. You can change the number of threads by passing it the `-t` parameter, and the total
  120. buffer size used by the threads heap by using the `-m`. Note that tantivy's memory usage
  121. is greater than just this buffer size parameter.
  122. On my computer (8 core Xeon(R) CPU X3450 @ 2.67GHz), on 8 threads, indexing wikipedia takes around 9 minutes.
  123. While tantivy is indexing, you can peek at the index directory to check what is happening.
  124. ```bash
  125. ls ./wikipedia-index
  126. ```
  127. The main file is `meta.json`.
  128. You should also see a lot of files with a UUID as filename, and different extensions.
  129. Our index is in fact divided in segments. Each segment acts as an individual smaller index.
  130. Its name is simply a uuid.
  131. If you decided to index the complete wikipedia, you may also see some of these files disappear.
  132. Having too many segments can hurt search performance, so tantivy actually automatically starts
  133. merging segments.
  134. # Serve the search index: `serve`
  135. Tantivy's cli also embeds a search server.
  136. You can run it with the following command.
  137. ```
  138. tantivy serve -i wikipedia-index
  139. ```
  140. By default, it will serve on port `3000`.
  141. You can search for the top 20 most relevant documents for the query `Barack Obama` by accessing
  142. the following [url](http://localhost:3000/api/?q=barack+obama&nhits=20) in your browser
  143. http://localhost:3000/api/?q=barack+obama&nhits=20
  144. By default this query is treated as `barack OR obama`.
  145. You can also search for documents that contains both term, by adding a `+` sign before the terms in your query.
  146. http://localhost:3000/api/?q=%2Bbarack%20%2Bobama%0A&nhits=20
  147. Also, `-` makes it possible to remove documents the documents containing a specific term.
  148. http://localhost:3000/api/?q=-barack%20%2Bobama%0A&nhits=20
  149. Finally tantivy handle phrase queries.
  150. http://localhost:3000/api/?q=%22barack%20obama%22&nhits=20
  151. # Search the index via the command line
  152. You may also use the `search` command to stream all documents matching a specific query.
  153. The documents are returned in an unspecified order.
  154. ```
  155. tantivy search -i wikipedia-index -q "barack obama"
  156. ```