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 5.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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
  2. Tantivy-cli is command line interface for [tantivy search engine](https://github.com/fulmicoton/tantivy).
  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. ## Install
  7. There are two ways to get `tantivy`.
  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 try and download a
  11. static binary: [binaries/linux_x86_64/](http://fulmicoton.com/tantivy-files/binaries/linux_x86_64/tantivy)
  12. ## Creating the index: `new`
  13. Let's create a directory in which your index will be stored.
  14. ```bash
  15. # create the directory
  16. mkdir wikipedia-index
  17. ```
  18. We will now initialize the index and create its schema.
  19. The [schema](http://fulmicoton.com/tantivy/tantivy/schema/index.html) defines
  20. the list of your fields, and for each field :
  21. - its name
  22. - its type, currently `u32` or `str`
  23. - how it should be indexed.
  24. You can find more information about the latter on
  25. [tantivy's schema documentation page](http://fulmicoton.com/tantivy/tantivy/schema/index.html
  26. In our case, our documents will contain
  27. * a title
  28. * a body
  29. * a url
  30. We want the title and the body to be tokenized and index. We want
  31. to also add the term frequency and term positions to our index.
  32. (To be honest, phrase queries are not yet implemented in tantivy,
  33. so the positions won't be really useful in this tutorial.)
  34. Running `tantivy new` will start a wizard that will help you go through
  35. the definition of the schema of our new index.
  36. Like all the other commands of `tantivy`, you will have to
  37. pass it your index directory via the `-i` or `--index`
  38. parameter as follows.
  39. ```bash
  40. tantivy new -i wikipedia-index
  41. ```
  42. When asked answer to the question, answer as follows:
  43. ```none
  44. Creating new index
  45. Let's define it's schema!
  46. New field name ? title
  47. Text or unsigned 32-bit Integer (T/I) ? T
  48. Should the field be stored (Y/N) ? Y
  49. Should the field be indexed (Y/N) ? Y
  50. Should the field be tokenized (Y/N) ? Y
  51. Should the term frequencies (per doc) be in the index (Y/N) ? Y
  52. Should the term positions (per doc) be in the index (Y/N) ? Y
  53. Add another field (Y/N) ? Y
  54. New field name ? body
  55. Text or unsigned 32-bit Integer (T/I) ? T
  56. Should the field be stored (Y/N) ? Y
  57. Should the field be indexed (Y/N) ? Y
  58. Should the field be tokenized (Y/N) ? Y
  59. Should the term frequencies (per doc) be in the index (Y/N) ? Y
  60. Should the term positions (per doc) be in the index (Y/N) ? Y
  61. Add another field (Y/N) ? Y
  62. New field name ? url
  63. Text or unsigned 32-bit Integer (T/I) ? T
  64. Should the field be stored (Y/N) ? Y
  65. Should the field be indexed (Y/N) ? N
  66. Add another field (Y/N) ? N
  67. [
  68. {
  69. "name": "title",
  70. "type": "text",
  71. "options": {
  72. "indexing": "position",
  73. "stored": true
  74. }
  75. },
  76. {
  77. "name": "body",
  78. "type": "text",
  79. "options": {
  80. "indexing": "position",
  81. "stored": true
  82. }
  83. },
  84. {
  85. "name": "url",
  86. "type": "text",
  87. "options": {
  88. "indexing": "unindexed",
  89. "stored": true
  90. }
  91. }
  92. ]
  93. ```
  94. After the wizard has finished, a `meta.json` has been written in `wikipedia-index/meta.json`.
  95. It is a fairly human readable JSON, so you may check its content.
  96. It contains two sections :
  97. - segments (currently empty, but we will change that soon)
  98. - schema
  99. # Indexing the document : `index`
  100. Tantivy's `index` command offers a way to index a json file.
  101. More accurately, the file must contain one document per line, in a json format.
  102. The structure of this JSON object must match that of our schema definition.
  103. ```json
  104. {"body": "some text", "title": "some title", "url": "http://somedomain.com"}
  105. ```
  106. For this tutorial, you can download a corpus with the 5 millions+ English articles of wikipedia
  107. formatted 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 uncompress 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 millions 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 named is simply a uuid.
  129. # Serve the search index
  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, the server is serving on the port `3000`.