Reputation: 1681
I'm doing a search using facets with the new api search:search but I have the next problem:
My source: File #1
<root>
<location>
<university>
<name>Yale</name>
<country>USA</country>
</university>
</location>
<location>
<university>
<name>MIT</name>
<country>USA</country>
</university>
</location>
<location>
<university>
<name>Santander</name>
<country>Spain</country>
</university>
</location>
</root>
File #2
<root>
<location>
<university>
<name>MIT</name>
<country>USA</country>
</university>
</location>
</root>
I need to know the number of universities by each country, but the facets return me the number of files that include one country or the number of locations in all files repeat universities, so in the last example of data it returns me this with the 2 options.
First Option (using frequency-order)
USA - 2 (Number of Files with at least one location with USA) SPAIN - 1
Second Option (Using item-frequency)
USA - 3 SPAIN - 1
When the result should be this:
USA - 2 (because in the two files there are only two universities) SPAIN - 1
How can I do this???
Upvotes: 2
Views: 208
Reputation: 20414
I think you need the item-frequency option, instead of the default fragment-frequency option. You add it to a constraint as a so-called facet-option. More details, and examples can be found on CMC: http://community.marklogic.com/pubs/5.0/apidocs/SearchAPI.html#search:search
-- edit --
I think I didn't read your question thoroughly enough. The search library focusses on search results, and the facet counts on fragments. Easiest way to improve the counts is by defining the location
element as a fragment root. However, I don't think that really returns the numbers you are looking for. The country facet really only counts the country occurrences, and not the universities within countries. You can't achieve that with the search library. It isn't difficult to do it yourself though:
for $country in cts:element-values(xs:QName('country'))
let $universities := cts:element-values(xs:QName('university'), (), cts:element-value-query(xs:QName('country'), $country))
return fn:concat($country, ' - ', fn:count($universities))
Note: Untested code, but it at least shows the essential steps. It also require countries to not occur within same fragments. You need to add location
as fragment root in the ML admin interface.
HTH!
Upvotes: 2