[英]MarkLogic: search for word in any element apart from specified one(s)
我如何編寫cts:query來有效地搜索包含某個單詞的文檔,除非該單詞僅出現在某個元素中。
例如,我想返回包含單詞“ dog”的文檔,但前提是該單詞位於<title>之外的任何元素中。
因此,鑒於這些文件:
<document id="doc-1">
<heading>foo</heading>
<paragraph>foo foo foo</paragraph>
</document>
<document id="doc-2">
<heading>bar dog</heading>
<paragraph>bar bar bar</paragraph>
</document>
<document id="doc-3">
<heading>foo dog</heading>
<paragraph>dog bar bar</paragraph>
</document>
我希望返回文檔3。
這有效:
for $i in $doc-set
where( doc($i)//*/text()[contains(normalize-space(lower-case(.)), "dog")]
[not(parent::title)] )
return $i ;
但是很慢
如果您始終要排除title元素,則在Admin UI上轉到數據庫配置頁面,單擊Word Query(在左側),單擊Excludes選項卡,然后添加該元素。 然后,該元素將從索引中排除,並且cts:word-query()在此處找不到術語。
對於更靈活的解決方案,請使用cts:not-in-query()函數,也稱為“溫和不”。
cts:search(
fn:doc(),
cts:not-in-query(
cts:word-query("dog"),
cts:element-word-query(xs:QName("title"), "dog")
)
)
請注意(如cts:not-in-query()頁中所述),您需要打開正確的位置索引。 我想為此,您需要打開元素詞位置,但要運行一些測試。
使用cts:search
:
cts:search(//document,
cts:element-query((xs:QName('heading'), xs:QName('paragraph')),
cts:word-query('dog', 'case-insensitive')))
另外,您可以創建一個字段索引並使用XPath表達式定義要搜索的內容。
使用cts:search說“父母”是您的根元素
cts:search(fn:doc()/ Parent [name()!='Title'],cts:word-query(“ dog”))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.