2012-09-27 44 views
0

我有以下XML結構:Marklogic:搜索:建議在短語

<root> 
<text>Hi i am a test user and doing testing here. Copied text Let’s suppose we have a text field where the user needs to enter the number of a person id. If the user types 1, all ids starting with 1 will show up. If the user types 12, all ids starting with 12 will show up.</text> 
</root> 

現在,我已經創建領域「文本」元素,它也能字段字詞庫上。執行以下查詢:

xquery version "1.0-ml"; 
import module namespace search ="http://marklogic.com/appservices/search" at "/MarkLogic/appservices/search/search.xqy"; 
let $options := 
<search:options xmlns="http://marklogic.com/appservices/search"> 
<default-suggestion-source> 
    <word collation="http://marklogic.com/collation//S2"> 
     <field name="text"/> 
    </word> 
</default-suggestion-source> 
</search:options> 
return 
search:suggest("tes", $options, 100) 

結果我得到了「測試」和「tseting」的建議,絕對是不錯,但我太想一些文字像在上述情況下,我期待「測試用戶,做.. 。「和」在這裏測試......「。請幫助我。

回答

1

詞彙詞庫存儲單詞標記,所以這就是爲什麼你得到個別單詞返回,而不是短語。對於短語中的匹配,您可以使用<text>上的範圍索引,並且對於每個搜索建議條目concat('*',$term,'*'),以便您的API調用看起來像這樣search:suggest("*tes*", $options, 100)

但是,由於領先的通配符模式,我認爲這將大大減慢您的查詢速度,並且它還會返回元素的整個值,而不是從搜索詞的位置開始,即:Hi i am a test user and doing testing here. Copied text ...而不是test user and doing ... 。當然,你可以用編程的方式解析這個。

爲了獲得更好的性能,請考慮使用分塊元素範圍索引策略。它需要預處理和大量的數據,具體取決於塊源的大小,但它會達到您想要的結果並且速度非常快並且可擴展。有一個excellent blog post over at Avalon consulting,描述如何詳細做這件事。

1

要搜索部分詞組,請使用開頭雙引號(語法值),而不使用結尾引號。 例如:搜索:建議(「「和日」,$選項) ‘和’ ‘而這種’ 結束的雙引號信號分析器,這句話是完整的,併產生 因此沒有擴大的建議 。也有約束使用。

search:suggest('constraint:"and th', $options)</search:quotation> 

===== 從http://docs.marklogic.com/search:suggest