跳轉到內容

XQuery/索引配置檔案自動生成

來自華夏公益教科書,開放的書籍,開放的世界

您希望根據例項或 XML 模式資料自動生成索引配置檔案。

對於新使用者來說,建立索引配置檔案很困難。為了幫助新使用者入門,通常會根據使用者提供的示例例項資料或 XML 模式,為這些使用者生成一個示例 collection.xconf 檔案。

索引型別

[編輯 | 編輯原始碼]

您可能希望建立幾種型別的索引。範圍索引在您有識別符號或希望根據元素內容對結果進行排序時非常有用。全文索引最常用於包含帶標點的完整句子的語言文字。

全文索引

[編輯 | 編輯原始碼]

以下是有關如何執行此操作的一些示例程式碼。

當 Lucene 全文索引索引完整的句子時,它們最有用。一種方法是掃描例項文件以查詢完整的句子,尋找帶標點的較長字串。雖然完整的實現將涉及包含“自然語言處理”庫(如 Apache UIMA),但我們可以從一些非常簡單的規則開始。

以下是一些針對非混合文字內容的流程示例步驟。混合文字也可以完成,但步驟更復雜。

  1. 獲取示例索引檔案中所有元素的列表
  2. 根據元素是否具有簡單或複雜內容對它們進行分類
  3. 如果它們具有簡單內容,請查詢句子(空格和標點符號)
  4. 對於每個具有全文的元素,建立一個 Lucene 索引

名稱空間生成的示例程式碼

[編輯 | 編輯原始碼]

這會在 <foo> 上建立一個索引,並使用集合中使用的每個名稱空間。

let $defaultNamespaces :=
 for $value in distinct-values(
     for $doc in collection($dataLocation)
       let $ns := namespace-uri($doc/*)
       return
         if ($ns)
         then $ns
         else ()
     )
   return element ns { $value }

let $index1 :=
 "<collection xmlns='http://exist-db.org/collection-config/1.0'><index"
let $index2 :=
 for $ns in $defaultNamespaces
   return concat(' xmlns:ns',index-of($defaultNamespaces,$ns),$eq,$qt,$ns,$qt)
let $index3 :=
 "><fulltext default='none' attribute='no'/><lucene><analyzer
class='org.apache.lucene.analysis.standard.StandardAnalyzer'/><analyzer
id='ws' class='org.apache.lucene.analysis.WhitespaceAnalyzer'/><text
qname='foo'/>"
let $index4 :=
 for $ns in $defaultNamespaces
   let $prefix := concat('ns',index-of($defaultNamespaces,$ns))
   return concat('<text qname=',$qt,$prefix,':foo',$qt,'/>')
let $index5 :=
 "</lucene></index></collection>"

let $index := util:parse(string-join(($index1,$index2,$index3,$index4,$index5),""))
let $status := xmldb:store($indexLocation,"collection.xconf",$index)
let $result :=xmldb:reindex($dataLocation)
華夏公益教科書