问题
Calling search:search
doesn't return expected facet result counts. Below are two example files and the code I used.
Page1.xml
<pages xmlns="http://marklogic.com/docs">
<page>
<elementNode>data1</elementNode>
<textNode>text1</textNode>
</page>
<page>
<elementNode>data2</elementNode>
<textNode>text2</textNode>
</page>
<page>
<elementNode>data3</elementNode>
<textNode>text3</textNode>
</page>
<page>
<elementNode>data4</elementNode>
<textNode>text4</textNode>
</page>
</pages>
Page2.xml
<pages xmlns="http://marklogic.com/docs">
<page>
<elementNode>data5</elementNode>
<textNode>text5</textNode>
</page>
<page>
<elementNode>data6</elementNode>
<textNode>text6</textNode>
</page>
<page>
<elementNode>data7</elementNode>
<textNode>text7</textNode>
</page>
<page>
<elementNode>data8</elementNode>
<textNode>text8</textNode>
</page>
</pages>
Also I have created an element range index on <elementNode>
. Now I executed following XQuery with searchText "text1"
xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";
declare namespace ts= "http://marklogic.com/docs";
import module namespace search ="http://marklogic.com/appservices/search" at "/MarkLogic/appservices/search/search.xqy";
declare variable $options :=
<options xmlns="http://marklogic.com/appservices/search">
<searchable-expression xmlns:ex="http://marklogic.com/docs">//ex:page</searchable-expression>
<grammar>
<starter strength="30" apply="grouping" delimiter=")">(</starter>
<starter strength="40" apply="prefix" element="cts:not-query">NOT</starter>
<joiner strength="10" apply="infix" element="cts:or-query" tokenize="word">OR</joiner>
<joiner strength="20" apply="infix" element="cts:and-query" tokenize="word">AND</joiner>
<joiner strength="50" apply="constraint">:</joiner>
</grammar>
<constraint name="elementNode">
<range collation="http://marklogic.com/collation/" type="xs:string">
<facet-option>limit=1000</facet-option>
<element ns="http://marklogic.com/docs" name="elementNode"/>
<searchable-expression xmlns:ex="http://marklogic.com/clover/docs-xml">//ex:elementNode</searchable-expression>
</range>
</constraint>
</options>;
let $searchResult := search:search("text1", $options)
return $searchResult
Which produces this result:
<search:response xmlns="" xmlns:search="http://marklogic.com/appservices/search">
<search:result index="1" uri="Page1.xml" path="fn:doc("Page1.xml")/*:pages/*:page[1]">
<search:snippet>
<search:match path="fn:doc("Page1.xml")/*:pages/*:page[1]/*:textNode">
<search:highlight>text1</search:highlight>
</search:match>
</search:snippet>
</search:result>
<search:facet name="elementNode">
<search:facet-value name="data1" count="1">data1</search:facet-value>
<search:facet-value name="data2" count="1">data2</search:facet-value>
<search:facet-value name="data3" count="1">data3</search:facet-value>
<search:facet-value name="data4" count="1">data4</search:facet-value>
</search:facet>
</search:response>
Now I'm seeing a problem in facet-result
. It should return only one facet value i.e. data1 from page1 since only page1 has the desired search expression "text1". Instead facet-result
giving me result of all textNode values from the whole document. Please help me -- How we can restrict this? Also, count is giving me for whole document but what I want is page count match.
回答1:
Indexes point to documents or fragments, not elements. This is similar to an RDBMS, in which indexes point to rows. Your sample XML and code suggests that you are thinking of each document as something like a relational table. Instead think of documents as rows.
If the example code is similar to the real application, I might refactor the documents so that each 'page' element becomes its own document.
Also, it's bad practice to create your own content using a namespace like http://marklogic.com/docs
. MarkLogic might decide to use that namespace in some future release, which could cause problems for you. Select a namespace that you have a reasonable expectation of controlling.
回答2:
You use a searchable-expression, which causes search results to be portioned to those elements. Unfortunately that is not the case for facet counts, as mblakele explains. His suggestion of putting the page elements in their own document is one of two possible solutions here. The other one is defining the page element as fragment root. You can do so in the Admin interface of MarkLogic. You can find it among the database properties.
HTH!
来源:https://stackoverflow.com/questions/12435620/marklogic-wrong-count-and-facet-result-xquery