Loading TOC...

search:search

search:search(
   $qtext as xs:string+,
   [$options as element(search:options)?],
   [$start as xs:unsignedLong?],
   [$page-length as xs:unsignedLong?]
) as element(search:response)

Summary

This function parses and invokes a query according to specified options, returning up to $page-length result nodes starting from $start.

Parameters
qtext The query text to parse. This may be a sequence, to accommodate more complex search UI. Multiple query texts are combined with an AND operator.
options Options to define the search grammar and control the search.

The following is a list of the options that apply to document searches. The options node can contain other options, such as a values specification, but such options are not used in this context. For details, see Appendix: Query Options Reference in the Search Developer's Guide.

  • additional-query - Additional query(s) to apply to the search. For details, see additional-query in the Search Developer's Guide
  • concurrency-level - The maximum number of threads to use when resolving facets. For details, see concurrency-level in the Search Developer's Guide
  • constraint - Limit the scope of a search and define facets. For details, see constraint in the Search Developer's Guide
  • debug - Enable or disable inclusion of debugging data in the search results. For details, see debug in the Search Developer's Guide
  • default-suggestion-source - Define a source for generating suggestions for naked terms. For details, see default-suggestion-source in the Search Developer's Guide
  • extract-document-data - Select elements, attributes, and JSON properties to include in the search results. For details, see extract-document-data in the Search Developer's Guide
  • extract-metadata: This option is deprecated. Use extract-document-data instead. For details, see extract-document-data in the Search Developer's Guide.
  • forest - Limit a search or lexicon query to specific forests. For details, see forest in the Search Developer's Guide
  • fragment-scope - Control the global fragment scope (properties or documents) over which to search. For details, see fragment-scope in the Search Developer's Guide
  • grammar - Define a custom search grammar. For details, see grammar in the Search Developer's Guide
  • operator - For details, see operator in the Search Developer's Guide
  • page-length - The number of results to return in each page of results. For details, see page-length in the Search Developer's Guide
  • quality-weight - A document quality weight to use when computing scores. For details, see quality-weight in the Search Developer's Guide
  • result-decorator - Specify a function with which to decorate each search results with additional information. For details, see result-decorator in the Search Developer's Guide
  • return-constraints - Whether or not to include the input constraint definitions in the search response. For details, see return-constraints in the Search Developer's Guide
  • return-facets - Whether or not to include facets in the search response. For details, see return-facets in the Search Developer's Guide
  • return-metrics - Whether or not to include query performance data in the search response. For details, see return-metrics in the Search Developer's Guide
  • return-plan - Whether or not to include a query plan in the search response. For details, see return-plan in the Search Developer's Guide
  • return-qtext - Whether or not to include the input query text in the search response. For details, see return-qtext in the Search Developer's Guide
  • return-query - Whether or not to include the final representation of the input query in the search response. For details, see return-query in the Search Developer's Guide
  • return-results - Whether or not to include search result details in the search response. For details, see return-results in the Search Developer's Guide
  • return-similar - Whether or not to include a list of similar document for each search result in the response. For details, see return-similar in the Search Developer's Guide
  • search-option - Specify an advanced option to pass to the underlying cts query layer. For details, see search-option in the Search Developer's Guide
  • searchable-expression - An XPath expression that selects the documents to include in a search. For details, see searchable-expression in the Search Developer's Guide. Due to security and performance considerations, beginning in MarkLogic 9.0-10, the searchable-expression property/element in query options is deprecated. In addition, in 10.0-2 and moving forward, the searchable-expression requires the eval-search-string privilege.
  • sort-order - Define elements, attributes, fields, or JSON properties on which to order results. For details, see sort-order in the Search Developer's Guide
  • suggestion-source - Define a suggestion completion source for constraint-qualified search terms. For details, see suggestion-source in the Search Developer's Guide
  • term - Specify the handling of empty searches and search terms not associated with a constraint. For details, see term in the Search Developer's Guide
  • transform-results - Specify a function to use for generating snippets. For details, see transform-results in the Search Developer's Guide

start The index of the first hit to return. If 0, treated as 1. If greater than the number of results, no results will be returned. The default is 1.
page-length The maximum number of hits to return. The default is 10. If the value is 0, no results are returned.

Usage Notes

The output of search:search returns a <response> element, which in turn contains a total attribute. The value of the total attribute is an estimate, based on the index resolution of the query, and it is not filtered for accuracy. The accuracy of the index resolution depends on the index configuration of the database, on the query, and on the data being searched.

Example

xquery version "1.0-ml";
import module namespace search = "http://marklogic.com/appservices/search"
    at "/MarkLogic/appservices/search/search.xqy";

search:search("Vannevar Bush",
    <options xmlns="http://marklogic.com/appservices/search">
      <return-results>false</return-results>
      <return-facets>true</return-facets>
    </options>)

=>

<search:response total="1234" start="1" page-length="10" xmlns=""
        xmlns:search="http://marklogic.com/appservices/search">
  <search:facet name="date">
	  <search:facet-value value="today" count="1000">
		  Today</search:facet-value>
	  <search:facet-value value="yesterday" count="234">
		  Yesterday</search:facet-value>
	  <search:facet-value value="thismonth" count="1234">
		  This Month</search:facet-value>
  <search:/facet>
    ...
</search:response>

    

Example

(: properties constraint example :)
xquery version "1.0-ml";
(: create a document with some properties to test with :)
xdmp:document-insert("/foo.xml", <foo>hello</foo>);
xdmp:document-set-properties("/foo.xml", <blah>boo</blah>);

(: do a properties constraint search :)
import module namespace search = "http://marklogic.com/appservices/search"
     at "/MarkLogic/appservices/search/search.xqy";

search:search("hello sample-property-constraint:boo",
<options xmlns="http://marklogic.com/appservices/search">
  <constraint name="sample-property-constraint">
    <properties />
  </constraint>
  <debug>true</debug>
</options>)

=>

<search:response total="1" start="1" page-length="10" xmlns=""
	xmlns:search="http://marklogic.com/appservices/search">
  <search:result index="1" uri="/foo.xml" 
		path="fn:doc(&quot;/foo.xml&quot;)" score="328" 
		confidence="0.807121" fitness="0.901397">
    <search:snippet>
	    <search:match path="fn:doc(&quot;/foo.xml&quot;)/foo">
		    <search:highlight>hello</search:highlight></search:match>
    </search:snippet>
  </search:result>
  <search:qtext>hello sample-property-constraint:boo</search:qtext>
  <search:report id="SEARCH-FLWOR">(cts:search(fn:collection(), 
	  cts:and-query((cts:word-query("hello", ("lang=en"), 1), 
	  cts:properties-query(cts:word-query("boo", ("lang=en"), 1))), 
	  ()), ("score-logtfidf"), 1))[1 to 10]
  </search:report>
  <search:metrics>
    <search:query-resolution-time>PT0.647S</search:query-resolution-time>
    <search:facet-resolution-time>PT0S</search:facet-resolution-time>
    <search:snippet-resolution-time>PT0.002S</search:snippet-resolution-time>
    <search:total-time>PT0.651S</search:total-time>
  </search:metrics>
</search:response>


    

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.