Hi Jonna,

Justin gave you the solution. This a little something extra we did one
occasion in a MarkLogic Essentials class to respond to this question:
- http://gist.github.com/343528 (see listing 8)

Dont know if you attended the class but case you did you can try this out.
Also if anyone is interested in MarkLogic training please refer to
http://marklogic.com/services/training.html

Hope this helps,
Nuno

On Aug 24, 2010 4:49 PM, "Justin Makeig" <[email protected]>
wrote:
Jonna,
cts:search gives you a list of results based on a cts:query. You can also
use a cts:query to limit the scope of a facet, but you don’t use cts:search
to generate the values and counts. You’ll need a lexicon for that. The first
thing you need to do is to create a range index on the element (or
element-attribute combination) that contains your data, for example “color”
below. A range index keeps track of the values of a particular element (or
element-attribute) in memory, so it’s very fast for look-ups, counts, and
aggregates. To access the lexicon, you can use cts:element-values to
enumerate the values and cts:frequency to get the counts. For example,

xquery version "1.0-ml";

let $query := cts:and-query(("dog", "cat"))
let $options := (
  'frequency-order',
  'collation=http://marklogic.com/collation/codepoint'
)

for $v in cts:element-values(QName("", "color"), "", $options, $query)[1 to
10]
return
 <facet frequency={cts:frequency($v)}>{$v}</facet>


The above will get the top 10 most frequently occurring values for the
“color” element and create output of the form:

<facet frequency="16828">black</facet>
<facet frequency="16752">orange</facet>
<facet frequency="16659">green</facet>
<facet frequency="16629">blue</facet>
<facet frequency="16613">voilet</facet>
<facet frequency="16519">red</facet>

This assumes you have a string range index on the color element that uses
the Unicode Codepoint collation. For more about range indexes, please see
section 21 of the “Administrator’s Guide” <
http://developer.marklogic.com/pubs/4.1/books/admin.pdf>.

Here’s how you’d do roughly the same facet in the Search API

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

let $options := <options xmlns="http://marklogic.com/appservices/search";>
  <constraint name="color">
    <range type="xs:string" collation="
http://marklogic.com/collation/codepoint"; facet="true">
      <facet-option>limit=10</facet-option>
      <element name="color"/>
      <facet-option>frequency-order</facet-option>
      <facet-option>descending</facet-option>
    </range>
  </constraint>
  <return-results>false</return-results>
  <return-facets>true</return-facets>
</options>

return search:search("dog cat", $options)

which will output something like

<search:response total="100000" start="1" page-length="10" xmlns:search="
http://marklogic.com/appservices/search";>
  <search:facet name="color">
    <search:facet-value name="black"
count="16828">black</search:facet-value>
    <search:facet-value name="orange"
count="16752">orange</search:facet-value>
    <search:facet-value name="green"
count="16659">green</search:facet-value>
    <search:facet-value name="blue" count="16629">blue</search:facet-value>
    <search:facet-value name="voilet"
count="16613">voilet</search:facet-value>
    <search:facet-value name="red" count="16519">red</search:facet-value>
  </search:facet>
  <search:qtext/>
  <search:metrics>
    <search:facet-resolution-time>PT0.005495S</search:facet-resolution-time>
    <search:snippet-resolution-time>PT0S</search:snippet-resolution-time>
    <search:total-time>PT0.035695S</search:total-time>
  </search:metrics>
</search:response>

For this simple example, there is a little more typing with the Search API,
but once you get into things like query parsing, auto-suggest, and bucketed
constraints  most users appreciate its level of abstraction.

Again, feedback and questions are much appreciated.

Justin


If you’re playing along at home, here’s the script that I used to generate
my dummy data set:

xquery version "1.0-ml”;

let $colors := ('red', 'green', 'blue', 'orange', 'voilet', 'black')
let $len := count($colors)
for $i in (1 to 100000)
let $rand := xdmp:random($len -1) + 1
let $d := <doc>
  <description>This references a dog and a cat.</description>
  <color>{$colors[$rand]}</color>
</doc>
return  xdmp:document-insert(string(xdmp:random()),$d)




Justin Makeig
Senior Product Manager
MarkLogic Corporation

email  [email protected]
we...

On Aug 24, 2010, at 1:25 PM, Jonna Marry wrote:

> Hi Justin,
>
> Thanks for your reply.
>
> Can you please give me a sample to get facet in cts:sear...

> On Aug 24, 2010, at 12:18 PM, Jonna Marry wrote:
>
>> Hi,
>>
>> I am new to Mark logic. And I am a...



_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to