Skip to content

Instantly share code, notes, and snippets.

@dscape
Created March 24, 2010 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dscape/342965 to your computer and use it in GitHub Desktop.
Save dscape/342965 to your computer and use it in GitHub Desktop.
Introduction to Alerting
let $doc :=
<root>
<a> It starts with hello and ends with goodbye. </a>
</root>
return
cts:highlight( $doc, cts:and-query(("hello", "goodbye")),
if (cts:word-query-text($cts:queries) eq "hello")
then (<font color="blue">{$cts:text}</font>)
else (<font color="red">{$cts:text}</font>))
let $query := <query> {cts:word-query("hello")} </query>
let $p := <p>hello stranger!</p>
return cts:contains( $query, cts:reverse-query($p) )
(: now you can store your queries in the database and use them on ingestion :)
let $car := <car>
<maker country="jp">Honda</maker>
<model>Civic</model>
<year>2005</year>
<type>Compact</type>
<query> { cts:or-query(
( cts:word-query("civic","case-insensitive"),
cts:word-query("honda","case-insensitive"),
cts:word-query("compact","case-insensitive"),
cts:word-query("japan","case-insensitive")))} </query>
<desc>The Honda Civic is a line of compact cars developed and manufactured by Honda. In North America, the Civic is the second-longest continuously-running nameplate from a Japanese manufacturer; only the Toyota Corolla, introduced in 1968, has been in production longer. The Civic, along with the Accord and Prelude, comprised Honda's vehicles sold in North America until the 1990s, when the model lineup was expanded. Having gone through several generational changes, the Civic has become larger and more upmarket, and it currently slots between the Fit and Accord.</desc>
</car>
let $x := <p>I like cars from japan</p>
return cts:contains( $car/query, cts:reverse-query($x) )
declare function local:uri-for-car($maker, $model, $year) {
fn:concat('/cars/', $maker, '-', $model, '.xml') } ;
let $civic := <car>
<maker country="jp">Honda</maker>
<model>Civic</model>
<year>2005</year>
<type>Compact</type>
<query> { cts:or-query(
( cts:word-query("civic","case-insensitive"),
cts:word-query("honda","case-insensitive"),
cts:word-query("compact","case-insensitive"),
cts:word-query("japan","case-insensitive")))} </query>
<desc>The Honda Civic is a line of compact cars developed and manufactured by Honda. In North America, the Civic is the second-longest continuously-running nameplate from a Japanese manufacturer; only the Toyota Corolla, introduced in 1968, has been in production longer. The Civic, along with the Accord and Prelude, comprised Honda's vehicles sold in North America until the 1990s, when the model lineup was expanded. Having gone through several generational changes, the Civic has become larger and more upmarket, and it currently slots between the Fit and Accord.</desc>
</car>
let $accord := <car>
<maker country="jp">Toyota</maker>
<model>Accord</model>
<year>2008</year>
<type>Sedan</type>
<query> { cts:or-query(
( cts:word-query("accord","case-insensitive"),
cts:word-query("compact","case-insensitive"),
cts:word-query("japan","case-insensitive")))} </query>
<desc>The Toyota Corolla is a line of subcompact/compact cars manufactured by the Japanese automaker Toyota, which has become very popular throughout the world since the nameplate was first introduced in 1966. In 1997, the Corolla became the best selling nameplate in the world, with over 35 million sold as of 2007. Over the past 40 years, one Corolla car has been sold on average every 40 seconds. The series has undergone several major redesigns.</desc>
</car>
let $charger := <car>
<maker country="jp">Dodge</maker>
<model>Charger</model>
<year>2006</year>
<type>Sedan</type>
<query> { cts:or-query(
( cts:word-query("dodge","case-insensitive"),
cts:word-query("sedan","case-insensitive"),
cts:word-query("charger","case-insensitive"),
cts:word-query("america","case-insensitive")))} </query>
<desc>The Dodge Charger LX is a rear-wheel drive four-door automobile introduced in February 2005. Built by Chrysler for its North American Dodge brand, the car was created to continue the Dodge Charger line, and replaced the Dodge Intrepid as Dodge's full-size sedan. It shares the LX platform with the Chrysler 300, the newer third generation Dodge Challenger, and the now-discontinued Dodge Magnum.</desc>
</car>
for $car in ($civic, $accord, $charger)
return
xdmp:document-insert(
local:uri-for-car($car/maker, $car/model, $car/year),
$car
)
declare function local:uri-for-car($maker, $model, $year) {
fn:concat('/cars/', $maker, '-', $model, '.xml') } ;
declare function local:alert($query,$cars){
for $car in $cars/car[cts:contains(.,$query)]
let $maker := $car/maker
let $model := $car/model
let $year := $car/year
return
xdmp:log(
fn:concat("Match found for ", $maker , " " , $model, " of ", $year)
) };
let $car := <car>
<maker country="jp">Honda</maker>
<model>Odyssey</model>
<year>2006</year>
<!-- Changing to Sedan gives a lot more results -->
<type>Minivan</type>
<query> { cts:or-query(
( cts:word-query("minivan","case-insensitive"),
cts:word-query("honda","case-insensitive"),
cts:word-query("odyssey","case-insensitive"),
cts:word-query("japan","case-insensitive")))} </query>
<desc>The Honda Odyssey is a minivan manufactured by Japanese automaker Honda since 1994, marketed worldwide, and now in its third (North America) and fourth generations (Japan).</desc>
</car>
let $cars := cts:search( fn:doc(), cts:query($car//query/*) )
let $query := cts:or-query( ( cts:query( $cars//query/*) ) )
let $_ :=
cts:highlight( $car, $query,
local:alert($cts:queries,$cars))
return xdmp:document-insert(
local:uri-for-car($car/maker, $car/model, $car/year), $car )
(: showing reverse queries :)
declare function local:uri-for-car($maker, $model, $year) {
fn:concat('/cars/', $maker, '-', $model, '.xml') } ;
declare function local:tweet($username, $password, $message) {
let $options := <options xmlns="xdmp:http">
<authentication method="basic">
<username>{$username}</username>
<password>{$password}</password>
</authentication>
</options>
return xdmp:http-post(
fn:concat("http://twitter.com/statuses/update.xml?status=",
xdmp:url-encode($message) ), $options) };
declare function local:alert($query,$cars){
for $car in $cars/car[cts:contains(.,$query)]
let $maker := $car/maker
let $model := $car/model
let $year := $car/year
return
local:tweet( "test_kelly", "test_kelly2",
fn:concat("Match found for ", $maker , " " , $model, " of ", $year)
) };
let $car := <car>
<maker country="jp">Honda</maker>
<model>Odyssey</model>
<year>2006</year>
<!-- Changing to Sedan gives a lot more results -->
<type>Minivan</type>
<query> { cts:or-query(
( cts:word-query("minivan","case-insensitive"),
cts:word-query("honda","case-insensitive"),
cts:word-query("odyssey","case-insensitive"),
cts:word-query("japan","case-insensitive")))} </query>
<desc>The Honda Odyssey is a minivan manufactured by Japanese automaker Honda since 1994, marketed worldwide, and now in its third (North America) and fourth generations (Japan).</desc>
</car>
let $cars := cts:search( fn:doc(), cts:query($car//query/*) )
let $query := cts:or-query( ( cts:query( $cars//query/*) ) )
let $_ :=
cts:highlight( $car, $query,
local:alert($cts:queries,$cars))
return xdmp:document-insert(
local:uri-for-car($car/maker, $car/model, $car/year), $car )
(: go to twitter.com/test_kelly and see it yourself :)
(: introduce alerting application :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment