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.3KB

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
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/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](http://fulmicoton.com/tantivy-files/wiki-articles-1000.json).
  113. The `index` command will index your document.
  114. By default it will use as many threads as there are cores on your machine.
  115. You can change the number of threads by passing it the `-t` parameter.
  116. On my computer (8 core Xeon(R) CPU X3450 @ 2.67GHz), it will take around 6 minutes.
  117. ```
  118. cat wiki-articles.json | tantivy index -i ./wikipedia-index
  119. ```
  120. While it is indexing, you can peek at the index directory
  121. to check what is happening.
  122. ```bash
  123. ls ./wikipedia-index
  124. ```
  125. If you indexed the 5 million articles, you should see a lot of new files, all with the following format:
  126. The main file is `meta.json`.
  127. Our index is in fact divided in segments. Each segment acts as an individual smaller index.
  128. Its name is simply a uuid.
  129. # Serve the search index: `serve`
  130. Tantivy's cli also embeds a search server.
  131. You can run it with the following command.
  132. ```
  133. tantivy serve -i wikipedia-index
  134. ```
  135. By default, it will serve on port `3000`.
  136. You can search for the top 20 most relevant documents for the query `Barack Obama` by accessing
  137. the following [url](http://localhost:3000/api/?q=barack+obama&explain=true&nhits=20) in your browser
  138. http://localhost:3000/api/?q=barack+obama&explain=true&nhits=20
  139. # Optimizing the index: `merge`
  140. Each of tantivy's indexer threads is building its own independant segment.
  141. When its buffer is full, it closes its running segment, and starts working on a new one.
  142. You should currently have more than 50 segments in your directory.
  143. Having that many segments can hurt your query performance.
  144. Calling `tantivy merge` will merge your segments into one.
  145. ```
  146. tantivy merge -i ./wikipedia-index
  147. ```
  148. (The command takes less than 4 minutes on my computer)
  149. Note that your files are still there even after having run the command.
  150. However, `meta.json` only lists one of the segments.
  151. You will still need to remove the files manually.