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

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 simple way to add the `tantivy` CLI to your computer.
  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 `Linux 64bits`, 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 of 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 index. We want
  32. to also 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 go through
  36. the definition of the schema of our 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. When asked answer to the question, answer as follows:
  44. ```none
  45. Creating new index
  46. Let's define it's 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` has been written in `wikipedia-index/meta.json`.
  96. It is a fairly human readable JSON, so you may 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. More accurately, the file must contain one document per line, in a json format.
  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 millions+ English articles of wikipedia
  108. formatted in the right format here : [wiki-articles.json (2.34 GB)](https://www.dropbox.com/s/wwnfnu441w1ec9p/wiki-articles.json.bz2?dl=0).
  109. Make sure to uncompress the file
  110. ```bash
  111. bunzip2 wiki-articles.json.bz2
  112. ```
  113. 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).
  114. The `index` command will index your document.
  115. By default it will use as many threads as there are cores on your machine.
  116. You can change the number of threads by passing it the `-t` parameter.
  117. On my computer (8 core Xeon(R) CPU X3450 @ 2.67GHz), it will take around 6 minutes.
  118. ```
  119. cat wiki-articles.json | tantivy index -i ./wikipedia-index
  120. ```
  121. While it is indexing, you can peek at the index directory
  122. to check what is happening.
  123. ```bash
  124. ls ./wikipedia-index
  125. ```
  126. If you indexed the 5 millions articles, you should see a lot of new files, all with the following format
  127. The main file is `meta.json`.
  128. Our index is in fact divided in segments. Each segment acts as an individual smaller index.
  129. Its named is simply a uuid.
  130. # Serve the search index : `serve`
  131. Tantivy's cli also embeds a search server.
  132. You can run it with the following command.
  133. ```
  134. tantivy serve -i wikipedia-index
  135. ```
  136. By default, the server is serving on the port `3000`.
  137. You can search for the top 20 most relevant documents for the query `Barack Obama` by accessing
  138. the following [url](http://localhost:3000/api/?q=barack+obama&explain=true&nhits=20) in your browser
  139. http://localhost:3000/api/?q=barack+obama&explain=true&nhits=20
  140. # Optimizing the index : `merge`
  141. Each tantivy's indexer thread is closing a new segment every 100K documents (this is completely arbitrary at the moment).
  142. You should have more than 50 segments in your dictionary at the moment.
  143. Having that many queries is hurting your query performance (well, mostly the fast ones).
  144. Tantivy merge will merge your segment into one.
  145. ```
  146. tantivy merge -i ./wikipedia-index
  147. ```
  148. (The command takes around 7 minutes on my computer)
  149. Note that your files are still there even after having run the command.
  150. `meta.json` however only lists one of the segments.
  151. You will still need to remove the files manually.