Solr Reference Guide 5.3: Indexing and Basic Data Operations

Introduction to Solr Indexing

要餵進 Solr index 的資料有個共同的基本資料結構:一個 document 包含多個 fields,每個 field 有個名稱以及內容,內容可能是空的。

通常會指定一個 ID field (類似 database 裡的 primary key),雖然 Solr 並沒有嚴格要求一定要有 ID field。

The curl Utility for Transferring Files

在範例或測試中使用 curl 或其他 command line 工具來上傳資料是沒有問題,

但在正式 production 環境中這不是建議的最佳效能方式。

Post Tool

必須指定 collection 或 core 名稱:

bin/post -c gettingstarted example/films/films.json

Examples

Indexing XML

bin/post -c gettingstarted *.xml

傳送 XML 參數,從 gettingstarted 刪除一個 document:

bin/post -c gettingstarted -d '<delete><id>42</id></delete>'

Uploading Data with Index Handlers

XML Formatted Index Updates

Adding Documents

The <add> element introduces one more documents to be added. The <doc> element introduces the fields making up a document. The <field> element presents the content for a specific field.

  • <add> 元素包含一個或多個要新增的 documents
  • <doc> 元素包含組成這個 document 的 fields
  • <field> 元素是某個特定 field 的內容

如果這個 document schema 有指定 unique key,那麼預設會覆蓋取代一樣的 unique key。

XML Update Commands

Commit and Optimize Operations

<commit> 這個操作會將上次 commit 之後載入的 documents 寫入磁碟。

在 commit 被發佈之前,新近索引的內容不會被搜尋到。

Commits 可以用 <commit/> 訊息明確發佈,也可以由 solrconfig.xml 裡的 <autocommit> 參數觸發。

<optimize> 這個操作會要求 Solr 合併內部資料結構,以此來提升搜尋的效能。

對於大規模的索引來說,optimization 會需要一些時間,但是經由合併許多小檔成為一個大檔,搜尋效能會得到提升。

Delete Operations

有兩個方式可以刪除索引中的 documents:

  • Delete by ID
  • Delete by Query

一個 delete message 裡面可以有多個 delete 動作:

<delete>
  <id>0002166313</id>
  <id>0031745983</id>
  <query>subject:sport</query>
  <query>publisher:penguin</query>
</delete>

Rollback Operations

rollback 這個命令會還原上次 commit 之後對於索引所做的新增、刪除。

它的語法就是簡單的 <rollback/>.

Uploading Structured Data Store Data with the Data Import Handler

EntityProcessors

LineEntityProcessor

有些使用案例,需要從檔案讀進來的每一行都建一個 Solr document。

Updating Parts of Documents

只更新 document 的一部份。

De-Duplication

避免重複的 document