[MarkLogic Dev General] how to restrict a search:search query to only search within certain elements

Jakob Fix jakob.fix at gmail.com
Sat Mar 17 07:33:48 PDT 2012


I'd like to know whether the (working) solution below is the way to go
or whether there are better ways.

Here it goes: I'd like to restrain a simple word search to a number of
elements, like so:

cts:search(/*,
cts:and-query((
    cts:element-word-query(
      (xs:QName("abstract"),
      xs:QName("title"),
      xs:QName("subTitle")
      ),
      "query"
    )
  ))
)

but I'd like to also use the search:search API in order to profit from
the snippeting, paging and reporting features it provides.

I've looked at the <additional-query> option and indeed it works when
including the above cts:and-query, but I'm not sure whether that is
the optimal way of restraining search:search to a number of elements,
in particular the fact that I have to repeat the search term ($term)
seems kind of suboptimal:

let $term := "cambodia"

let $additional-query := cts:and-query((
    cts:element-word-query(
      (fn:QName("http://purl.org/dc/terms/", "abstract"),
      fn:QName("http://purl.org/dc/terms/", "title"),
      fn:QName("http://purl.org/dc/terms/", "subtitle")
      ),
      $term
    )
  ))

let $options :=
  <options xmlns="http://marklogic.com/appservices/search">
    <additional-query>{$additional-query}</additional-query>
    <return-facets>false</return-facets>
     <debug>true</debug>
  </options>

return search:search($term, $options)


I had looked also at using <element-query> constraints to constrain
the search to these elements only, but the <constraint> options seem
to provide a facility to further constrain the search via their name,
but I'm looking to only ever search in the three elements.

    <constraint name="title">
      <element-query ns="http://purl.org/dc/terms/" name="title"/>
    </constraint>
    <constraint name="abstract">
      <element-query ns="http://purl.org/dc/terms/" name="abstract"/>
    </constraint>
    <constraint name="subtitle">
      <element-query ns="http://purl.org/dc/terms/" name="subtitle"/>
    </constraint>

Defining these constraints would allow me to do
search:search("title:cambodia subtitle:cambodia abstract:cambodia",
$options) but my overall goal is to only search a number of predefined
elements for any search. And repeating the constraints and the terms
in the search seems clumsy.

I didn't have more luck with the <word> constraint option, for the
same reason as the <element-query> option.

  <constraint name="title">
    <word>
      <element ns="http://purl.org/dc/terms/" name="title"/>
    </word>
  </constraint>
  <constraint name="abstract">
    <word>
      <element ns="http://purl.org/dc/terms/" name="abstract"/>
    </word>
  </constraint>

So is the <additional-query> approach the way to go?

Thanks,
Jakob.


More information about the General mailing list