querying.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. .. _querying:
  2. Querying
  3. --------
  4. .. _add_query:
  5. AddQuery
  6. ~~~~~~~~
  7. **Prototype:** function AddQuery ( $query, $index=“\*“, $comment=”" )
  8. Adds additional query with current settings to multi-query batch.
  9. ``$query`` is a query string. ``$index`` is an index name (or names)
  10. string. Additionally if provided, the contents of ``$comment`` are sent
  11. to the query log, marked in square brackets, just before the search
  12. terms, which can be very useful for debugging. Currently, this is
  13. limited to 128 characters. Returns index to results array returned from
  14. :ref:`run_queries`.
  15. Batch queries (or multi-queries) enable ``searchd`` to perform internal
  16. optimizations if possible. They also reduce network connection overheads
  17. and search process creation overheads in all cases. They do not result
  18. in any additional overheads compared to simple queries. Thus, if you run
  19. several different queries from your web page, you should always consider
  20. using multi-queries.
  21. For instance, running the same full-text query but with different
  22. sorting or group-by settings will enable ``searchd`` to perform
  23. expensive full-text search and ranking operation only once, but compute
  24. multiple group-by results from its output.
  25. This can be a big saver when you need to display not just plain search
  26. results but also some per-category counts, such as the amount of
  27. products grouped by vendor. Without multi-query, you would have to run
  28. several queries which perform essentially the same search and retrieve
  29. the same matches, but create result sets differently. With multi-query,
  30. you simply pass all these queries in a single batch and Manticore optimizes
  31. the redundant full-text search internally.
  32. ``AddQuery()`` internally saves full current settings state along with
  33. the query, and you can safely change them afterwards for subsequent
  34. ``AddQuery()`` calls. Already added queries will not be affected;
  35. there's actually no way to change them at all. Here's an example:
  36. .. code-block:: php
  37. $cl->SetSortMode ( SPH_SORT_RELEVANCE );
  38. $cl->AddQuery ( "hello world", "documents" );
  39. $cl->SetSortMode ( SPH_SORT_ATTR_DESC, "price" );
  40. $cl->AddQuery ( "ipod", "products" );
  41. $cl->AddQuery ( "harry potter", "books" );
  42. $results = $cl->RunQueries ();
  43. With the code above, 1st query will search for “hello world” in
  44. “documents” index and sort results by relevance, 2nd query will search
  45. for “ipod” in “products” index and sort results by price, and 3rd query
  46. will search for “harry potter” in “books” index while still sorting by
  47. price. Note that 2nd ``SetSortMode()`` call does not affect the first
  48. query (because it's already added) but affects both other subsequent
  49. queries.
  50. Additionally, any filters set up before an ``AddQuery()`` will fall
  51. through to subsequent queries. So, if ``SetFilter()`` is called before
  52. the first query, the same filter will be in place for the second (and
  53. subsequent) queries batched through ``AddQuery()`` unless you call
  54. ``ResetFilters()`` first. Alternatively, you can add additional filters
  55. as well.
  56. This would also be true for grouping options and sorting options; no
  57. current sorting, filtering, and grouping settings are affected by this
  58. call; so subsequent queries will reuse current query settings.
  59. ``AddQuery()`` returns an index into an array of results that will be
  60. returned from ``RunQueries()`` call. It is simply a sequentially
  61. increasing 0-based integer, ie. first call will return 0, second will
  62. return 1, and so on. Just a small helper so you won't have to track the
  63. indexes manually if you need then.
  64. .. _query:
  65. Query
  66. ~~~~~
  67. **Prototype:** function Query ( $query, $index=“\*“, $comment=”" )
  68. Connects to ``searchd`` server, runs given search query with current
  69. settings, obtains and returns the result set.
  70. ``$query`` is a query string. ``$index`` is an index name (or names)
  71. string. Returns false and sets ``GetLastError()`` message on general
  72. error. Returns search result set on success. Additionally, the contents
  73. of ``$comment`` are sent to the query log, marked in square brackets,
  74. just before the search terms, which can be very useful for debugging.
  75. Currently, the comment is limited to 128 characters.
  76. Default value for ``$index`` is ``"*"`` that means to query
  77. all local indexes. Characters allowed in index names include Latin
  78. letters (a-z), numbers (0-9) and underscore (_); everything else is
  79. considered a separator. Note that index name should not start with
  80. underscore character. Therefore, all of the following samples calls are
  81. valid and will search the same two indexes:
  82. .. code-block:: php
  83. $cl->Query ( "test query", "main delta" );
  84. $cl->Query ( "test query", "main;delta" );
  85. $cl->Query ( "test query", "main, delta" );
  86. On success, ``Query()`` returns a result set that contains some of the
  87. found matches (as requested by
  88. :ref:`SetLimits() <set_limits>`) and
  89. additional general per-query statistics. The result set is a hash (PHP
  90. specific; other languages might utilize other structures instead of
  91. hash) with the following keys and values:
  92. - "matches": Hash which maps found document IDs to another small hash containing
  93. document weight and attribute values (or an array of the similar
  94. small hashes if
  95. :ref:`SetArrayResult() <set_array_result>`
  96. was enabled).
  97. - "total": Total amount of matches retrieved *on server* (ie. to the server side
  98. result set) by this query. You can retrieve up to this amount of
  99. matches from server for this query text with current query settings.
  100. - "total_found": Total amount of matching documents in index (that were found and
  101. processed on server).
  102. - "words": Hash which maps query keywords (case-folded, stemmed, and otherwise
  103. processed) to a small hash with per-keyword statistics (“docs”,
  104. “hits”).
  105. - "error": Query error message reported by ``searchd`` (string, human readable).
  106. Empty if there were no errors.
  107. - "warning": Query warning message reported by ``searchd`` (string, human
  108. readable). Empty if there were no warnings.
  109. It should be noted that ``Query()`` carries out the same actions as
  110. ``AddQuery()`` and ``RunQueries()`` without the intermediate steps; it
  111. is analogous to a single ``AddQuery()`` call, followed by a
  112. corresponding ``RunQueries()``, then returning the first array element
  113. of matches (from the first, and only, query.)
  114. .. _run_queries:
  115. RunQueries
  116. ~~~~~~~~~~
  117. **Prototype:** function RunQueries ()
  118. Connect to searchd, runs a batch of all queries added using
  119. ``AddQuery()``, obtains and returns the result sets. Returns false and
  120. sets ``GetLastError()`` message on general error (such as network I/O
  121. failure). Returns a plain array of result sets on success.
  122. Each result set in the returned array is exactly the same as the result
  123. set returned from :ref:`query`.
  124. Note that the batch query request itself almost always succeeds - unless
  125. there's a network error, blocking index rotation in progress, or another
  126. general failure which prevents the whole request from being processed.
  127. However individual queries within the batch might very well fail. In
  128. this case their respective result sets will contain non-empty
  129. ``&quot;error&quot;`` message, but no matches or query statistics. In
  130. the extreme case all queries within the batch could fail. There still
  131. will be no general error reported, because API was able to successfully
  132. connect to ``searchd``, submit the batch, and receive the results - but
  133. every result set will have a specific error message.
  134. .. _reset_filters:
  135. ResetFilters
  136. ~~~~~~~~~~~~
  137. **Prototype:** function ResetFilters ()
  138. Clears all currently set filters.
  139. This call is only normally required when using multi-queries. You might
  140. want to set different filters for different queries in the batch. To do
  141. that, you should call ``ResetFilters()`` and add new filters using the
  142. respective calls.
  143. .. _reset_group_by:
  144. ResetGroupBy
  145. ~~~~~~~~~~~~
  146. **Prototype:** function ResetGroupBy ()
  147. Clears all currently group-by settings, and disables group-by.
  148. This call is only normally required when using multi-queries. You can
  149. change individual group-by settings using ``SetGroupBy()`` and
  150. ``SetGroupDistinct()`` calls, but you can not disable group-by using
  151. those calls. ``ResetGroupBy()`` fully resets previous group-by settings
  152. and disables group-by mode in the current state, so that subsequent
  153. ``AddQuery()`` calls can perform non-grouping searches.