Skip to content

Instantly share code, notes, and snippets.

@dscape
Created March 25, 2010 13:06
Show Gist options
  • Save dscape/343528 to your computer and use it in GitHub Desktop.
Save dscape/343528 to your computer and use it in GitHub Desktop.
Search, Updates and Range Indexes
declare namespace ts="http://marklogic.com/MLU/top-songs";
declare function local:build-query($text as xs:string) {
cts:or-query((
cts:word-query($text, (), 1),
cts:element-word-query(
xs:QName("ts:title"), $text, (), 2 ))) } ;
declare function local:search-rating($song) {
cts:score($song)
} ;
for $song in cts:search(fn:doc(), local:build-query("night")) [1 to 10]
return <div>
{ xs:string($song//ts:title) }
[ { local:search-rating($song) } ]
</div>
import module namespace s="http://marklogic.com/appservices/search" at "/MarkLogic/appservices/search/search.xqy";
declare namespace ts="http://marklogic.com/MLU/top-songs";
let $options :=
<s:options>
<s:transform-results apply="raw"/>
</s:options>
return
for $song in s:search("night", $options)/s:result
return
<div class="songname">
{ fn:string($song//ts:title) }
</div>
(: Showing the difference between contains and search - relevance vs doc order :)
declare namespace ts="http://marklogic.com/MLU/top-songs";
for $song in /ts:top-song
[ fn:contains(., "night") ] [1 to 10]
return <div>
{ xs:string($song/ts:title) }
</div>
(: Using URI to get the title from a document :)
import module namespace s="http://marklogic.com/appservices/search" at "/MarkLogic/appservices/search/search.xqy";
declare namespace ts="http://marklogic.com/MLU/top-songs";
for $result in s:search("night")/s:result
let $title := fn:string( fn:doc($result/@uri)//ts:title )
return
<div class="songname">
{ $title }
</div>
declare namespace ts="http://marklogic.com/MLU/top-songs";
(:::::::::::::::::: update ::::::::::::::)
(: xdmp:node-insert-child(
: (//ts:p[cts:contains(., "yeah")])[6],
: element stars { "3" } ) :)
(::::::::::: selection ::::::::::::::::::)
(//ts:p[cts:contains(., "yeah")])[6]
xdmp:document-properties("/images/Michael-Jackson+The-Way-You-Make-Me-Feel.jpg")
declare namespace ts="http://marklogic.com/MLU/top-songs";
for $weeks in //ts:weeks
let $week := $weeks/ts:week
let $count := fn:count ( $week )
let $last := fn:max( xs:date( $week ) )
return (
xdmp:node-insert-child($weeks, attribute count { $count } ),
xdmp:node-insert-child($weeks, attribute last { $last } ) )
declare namespace ts="http://marklogic.com/MLU/top-songs";
for $week in //ts:weeks/ts:week
return if ($week castable as xs:date)
then ()
else ( xdmp:node-delete($week), "Deleted one node" )
(: set up range indexes first :)
declare namespace ts="http://marklogic.com/MLU/top-songs";
for $i in cts:element-attribute-values(
xs:QName("ts:weeks"), xs:QName("count"), (), "frequency-order" )
return
<count i="{$i}">
{ cts:frequency($i) }
</count>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment