additional_functionality.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. Additional functionality
  2. ------------------------
  3. .. _build_excerpts:
  4. BuildExcerpts
  5. ~~~~~~~~~~~~~
  6. **Prototype:** function BuildExcerpts ( $docs, $index, $words,
  7. $opts=array() )
  8. Excerpts (snippets) builder function. Connects to ``searchd``, asks it
  9. to generate excerpts (snippets) from given documents, and returns the
  10. results.
  11. ``$docs`` is a plain array of strings that carry the documents'
  12. contents. ``$index`` is an index name string. Different settings (such
  13. as charset, morphology, wordforms) from given index will be used.
  14. ``$words`` is a string that contains the keywords to highlight. They
  15. will be processed with respect to index settings. For instance, if
  16. English stemming is enabled in the index, ``shoes`` will be highlighted
  17. even if keyword is ``shoe``. Keywords can contain wildcards, that work
  18. similarly to star-syntax available in queries. ``$opts`` is a hash which
  19. contains additional optional highlighting parameters:
  20. - ``before_match``:
  21. A string to insert before a keyword match. A ``%PASSAGE_ID%`` macro can
  22. be used in this string. The first match of the macro is replaced with
  23. an incrementing passage number within a current snippet. Numbering
  24. starts at 1 by default but can be overridden with
  25. ``start_passage_id`` option. In a multi-document call, %PASSAGE_ID%
  26. would restart at every given document. Default is ``<b>``.
  27. - ``after_match``:
  28. A string to insert after a keyword match. Starting with version
  29. 1.10-beta, a %PASSAGE_ID% macro can be used in this string. Default
  30. is ``</b>``.
  31. - ``chunk_separator``:
  32. A string to insert between snippet chunks (passages). Default is ``…``.
  33. - ``field_separator``:
  34. A string to insert between fields. Default is ``|``.
  35. - ``limit``:
  36. Maximum snippet size, in symbols (codepoints). Integer, default is
  37. 256.
  38. - ``around``:
  39. How much words to pick around each matching keywords block. Integer,
  40. default is 5.
  41. - ``exact_phrase``:
  42. Whether to highlight exact query phrase matches only instead of
  43. individual keywords. Boolean, default is false.
  44. - ``use_boundaries``:
  45. Whether to additionally break passages by phrase boundary characters,
  46. as configured in index settings with
  47. :ref:`phrase_boundary <phrase_boundary>`
  48. directive. Boolean, default is false.
  49. - ``weight_order``:
  50. Whether to sort the extracted passages in order of relevance
  51. (decreasing weight), or in order of appearance in the document
  52. (increasing position). Boolean, default is false.
  53. - ``query_mode``:
  54. Whether to handle $words as a query in :ref:`extended
  55. syntax <extended_query_syntax>`, or as a bag of words
  56. (default behavior). For instance, in query mode (``one two`` \| ``three
  57. four``) will only highlight and include those occurrences ``one two`` or
  58. ``three four`` when the two words from each pair are adjacent to each
  59. other. In default mode, any single occurrence of ``one``, ``two``,
  60. ``three``, or ``four`` would be highlighted. Boolean, default is false.
  61. - ``force_all_words``:
  62. Ignores the snippet length limit until it includes all the keywords.
  63. Boolean, default is false.
  64. - ``limit_passages``:
  65. Limits the maximum number of passages that can be included into the
  66. snippet. Integer, default is 0 (no limit).
  67. - ``limit_words``:
  68. Limits the maximum number of words that can be included into the
  69. snippet. Note the limit applies to any words, and not just the
  70. matched keywords to highlight. For example, if we are highlighting
  71. ``Mary`` and a passage ``Mary had a little lamb`` is selected, then it
  72. contributes 5 words to this limit, not just 1. Integer, default is 0
  73. (no limit).
  74. - ``start_passage_id``:
  75. Specifies the starting value of ``%PASSAGE_ID%`` macro (that gets
  76. detected and expanded in ``before_match``, ``after_match`` strings).
  77. Integer, default is 1.
  78. - ``load_files``:
  79. Whether to handle $docs as data to extract snippets from (default
  80. behavior), or to treat it as file names, and load data from specified
  81. files on the server side. Up to
  82. :ref:`dist_threads <dist_threads>`
  83. worker threads per request will be created to parallelize the work
  84. when this flag is enabled. Boolean, default is false. Building of the
  85. snippets could be parallelized between remote agents. Just set the
  86. :ref:`‘dist_threads’ <dist_threads>`
  87. param in the config to the value greater than 1, and then invoke the
  88. snippets generation over the distributed index, which contain only
  89. one(!) :ref:`local <local>` agent
  90. and several remotes. The
  91. :ref:`snippets_file_prefix <snippets_file_prefix>`
  92. option is also in the game and the final filename is calculated by
  93. concatenation of the prefix with given name. Otherwords, when
  94. snippets_file_prefix is ‘/var/data’ and filename is ‘text.txt’ the
  95. sphinx will try to generate the snippets from the file
  96. ‘/var/datatext.txt’, which is exactly ‘/var/data’ + ‘text.txt’.
  97. - ``load_files_scattered``:
  98. It works only with distributed snippets generation with remote
  99. agents. The source files for snippets could be distributed among
  100. different agents, and the main daemon will merge together all
  101. non-erroneous results. So, if one agent of the distributed index has
  102. ‘file1.txt’, another has ‘file2.txt’ and you call for the snippets
  103. with both these files, the sphinx will merge results from the agents
  104. together, so you will get the snippets from both ‘file1.txt’ and
  105. ‘file2.txt’. Boolean, default is false.
  106. If the ``load_files`` is also set, the request will return the error
  107. in case if any of the files is not available anywhere. Otherwise (if
  108. ``load_files`` is not set) it will just return the empty strings for
  109. all absent files. The master instance reset this flag when
  110. distributes the snippets among agents. So, for agents the absence of
  111. a file is not critical error, but for the master it might be so. If
  112. you want to be sure that all snippets are actually created, set both
  113. ``load_files_scattered`` and ``load_files``. If the absence of some
  114. snippets caused by some agents is not critical for you - set just
  115. ``load_files_scattered``, leaving ``load_files`` not set.
  116. - ``html_strip_mode``:
  117. HTML stripping mode setting. Defaults to ``index``, which means that
  118. index settings will be used. The other values are ``none`` and ``strip``,
  119. that forcibly skip or apply stripping irregardless of index settings;
  120. and ``retain``, that retains HTML markup and protects it from
  121. highlighting. The ``retain`` mode can only be used when highlighting
  122. full documents and thus requires that no snippet size limits are set.
  123. String, allowed values are ``none``, ``strip``, ``index``, and ``retain``.
  124. - ``allow_empty``:
  125. Allows empty string to be returned as highlighting result when a
  126. snippet could not be generated (no keywords match, or no passages fit
  127. the limit). By default, the beginning of original text would be
  128. returned instead of an empty string. Boolean, default is false.
  129. - ``passage_boundary``:
  130. Ensures that passages do not cross a sentence, paragraph, or zone
  131. boundary (when used with an index that has the respective indexing
  132. settings enabled). String, allowed values are ``sentence``,
  133. ``paragraph``, and ``zone``.
  134. - ``emit_zones``:
  135. Emits an HTML tag with an enclosing zone name before each passage.
  136. Boolean, default is false.
  137. - ``force_passages``:
  138. Whether to generate passages for snippet even if limits allow to highlight
  139. whole text. Boolean, default is false.
  140. Snippets extraction algorithm currently favors better passages (with
  141. closer phrase matches), and then passages with keywords not yet in
  142. snippet. Generally, it will try to highlight the best match with the
  143. query, and it will also to highlight all the query keywords, as made
  144. possible by the limits. In case the document does not match the query,
  145. beginning of the document trimmed down according to the limits will be
  146. return by default. You can also return an empty snippet instead case by
  147. setting ``allow_empty`` option to true.
  148. Returns false on failure. Returns a plain array of strings with excerpts
  149. (snippets) on success.
  150. .. _build_keywords:
  151. BuildKeywords
  152. ~~~~~~~~~~~~~
  153. **Prototype:** function BuildKeywords ( $query, $index, $hits )
  154. Extracts keywords from query using tokenizer settings for given index,
  155. optionally with per-keyword occurrence statistics. Returns an array of
  156. hashes with per-keyword information.
  157. ``$query`` is a query to extract keywords from. ``$index`` is a name of
  158. the index to get tokenizing settings and keyword occurrence statistics
  159. from. ``$hits`` is a boolean flag that indicates whether keyword
  160. occurrence statistics are required.
  161. Usage example:
  162. .. code-block:: php
  163. $keywords = $cl->BuildKeywords ( "this.is.my query", "test1", false );
  164. .. _escape_string:
  165. EscapeString
  166. ~~~~~~~~~~~~
  167. **Prototype:** function EscapeString ( $string )
  168. Escapes characters that are treated as special operators by the query
  169. language parser. Returns an escaped string.
  170. ``$string`` is a string to escape.
  171. This function might seem redundant because it's trivial to implement in
  172. any calling application. However, as the set of special characters might
  173. change over time, it makes sense to have an API call that is guaranteed
  174. to escape all such characters at all times.
  175. Usage example:
  176. .. code-block:: php
  177. $escaped = $cl->EscapeString ( "escaping-sample@query/string" );
  178. .. _flush_attributes:
  179. FlushAttributes
  180. ~~~~~~~~~~~~~~~
  181. **Prototype:** function FlushAttributes ()
  182. Forces ``searchd`` to flush pending attribute updates to disk, and
  183. blocks until completion. Returns a non-negative internal ``flush tag`` on
  184. success. Returns -1 and sets an error message on error.
  185. Attribute values updated using
  186. :ref:`UpdateAttributes() <update_attributes>`
  187. API call are kept in a memory mapped file. Which means the OS
  188. decides when the updates are actually written to disk.
  189. FlushAttributes() call lets you enforce a flush, which writes all the
  190. changes to disk. The call will block
  191. until ``searchd`` finishes writing the data to disk, which might take
  192. seconds or even minutes depending on the total data size (.spa file
  193. size). All the currently updated indexes will be flushed.
  194. Flush tag should be treated as an ever growing magic number that does
  195. not mean anything. It's guaranteed to be non-negative. It is guaranteed
  196. to grow over time, though not necessarily in a sequential fashion; for
  197. instance, two calls that return 10 and then 1000 respectively are a
  198. valid situation. If two calls to FlushAttrs() return the same tag, it
  199. means that there were no actual attribute updates in between them, and
  200. therefore current flushed state remained the same (for all indexes).
  201. Usage example:
  202. .. code-block:: php
  203. $status = $cl->FlushAttributes ();
  204. if ( $status<0 )
  205. print "ERROR: " . $cl->GetLastError();
  206. .. _Status:
  207. Status
  208. ~~~~~~
  209. **Prototype:** function Status ()
  210. Queries searchd status, and returns an array of status variable name and
  211. value pairs.
  212. Usage example:
  213. .. code-block:: php
  214. $status = $cl->Status ();
  215. foreach ( $status as $row )
  216. print join ( ": ", $row ) . "\n";
  217. .. _update_attributes:
  218. UpdateAttributes
  219. ~~~~~~~~~~~~~~~~
  220. **Prototype:** function UpdateAttributes ( $index, $attrs, $values,
  221. $type=SPH_UPDATE_INT, $ignorenonexistent=false )
  222. Instantly updates given attribute values in given documents. Returns
  223. number of actually updated documents (0 or more) on success, or -1 on
  224. failure.
  225. ``$index`` is a name of the index (or indexes) to be updated. ``$attrs``
  226. is a plain array with string attribute names, listing attributes that
  227. are updated.
  228. .. warning::
  229. Note that document ``id`` attribute cannot be updated.
  230. ``$values`` is a hash with documents IDs as keys and new attribute values,
  231. see below.
  232. Optional ``$type`` parameter can have the following values:
  233. 1. ``SPH_UPDATE_INT``. This is the default value. ``$values`` hash holds
  234. documents IDs as keys and a plain arrays of new attribute values.
  235. 2. ``SPH_UPDATE_MVA``. Points that MVA attributes are being updated. In this
  236. case the ``$values`` must be a hash with document IDs as keys and array of
  237. arrays of int values (new MVA attribute values).
  238. 3. ``SPH_UPDATE_STRING``. Points that string attributes are being updated.
  239. ``$values`` must be a hash with document IDs as keys and array of strings
  240. as values.
  241. 4. ``SPH_UPDATE_JSON``. Works the same as ``SPH_UPDATE_STRING``, but for
  242. JSON attribute updates.
  243. Optional boolean parameter ``$ignorenonexistent``
  244. points that the update will silently ignore any warnings about trying to
  245. update a column which is not exists in current index schema.
  246. ``$index`` can be either a single index name or a list, like in
  247. ``Query()``. Unlike ``Query()``, wildcard is not allowed and all the
  248. indexes to update must be specified explicitly. The list of indexes can
  249. include distributed index names. Updates on distributed indexes will be
  250. pushed to all agents.
  251. Usage example:
  252. .. code-block:: php
  253. $cl->UpdateAttributes ( "test1", array("group_id"), array(1=>array(456)) );
  254. $cl->UpdateAttributes ( "products", array ( "price", "amount_in_stock" ),
  255. array ( 1001=>array(123,5), 1002=>array(37,11), 1003=>(25,129) ) );
  256. The first sample statement will update document 1 in index ``test1``,
  257. setting ``group_id`` to 456. The second one will update documents 1001,
  258. 1002 and 1003 in index ``products``. For document 1001, the new price will
  259. be set to 123 and the new amount in stock to 5; for document 1002, the
  260. new price will be 37 and the new amount will be 11; etc.