- Apache Solr 教程
- Apache Solr - 首頁
- Apache Solr - 概述
- Apache Solr - 搜尋引擎基礎
- Apache Solr - Windows環境
- Apache Solr - 在Hadoop上
- Apache Solr - 架構
- Apache Solr - 術語
- Apache Solr - 基本命令
- Apache Solr - Core
- Apache Solr - 索引資料
- Apache Solr - 新增文件 (XML)
- Apache Solr - 更新資料
- Apache Solr - 刪除文件
- Apache Solr - 檢索資料
- Apache Solr - 查詢資料
- Apache Solr - 分面搜尋
- Apache Solr 有用資源
- Apache Solr - 快速指南
- Apache Solr - 有用資源
- Apache Solr - 討論
Apache Solr - 新增文件 (XML)
在上一章中,我們解釋瞭如何將JSON和.CSV檔案格式的資料新增到Solr中。在本章中,我們將演示如何使用XML文件格式將資料新增到Apache Solr索引中。
示例資料
假設我們需要使用XML檔案格式將以下資料新增到Solr索引中。
| 學生ID | 名字 | 姓氏 | 電話 | 城市 |
|---|---|---|---|---|
| 001 | Rajiv | Reddy | 9848022337 | 海德拉巴 |
| 002 | Siddharth | Bhattacharya | 9848022338 | 加爾各答 |
| 003 | Rajesh | Khanna | 9848022339 | 德里 |
| 004 | Preethi | Agarwal | 9848022330 | 浦那 |
| 005 | Trupthi | Mohanty | 9848022336 | 布巴內斯瓦爾 |
| 006 | Archana | Mishra | 9848022335 | 欽奈 |
使用XML新增文件
要將上述資料新增到Solr索引中,我們需要準備一個XML文件,如下所示。將此文件儲存到名為sample.xml的檔案中。
<add>
<doc>
<field name = "id">001</field>
<field name = "first name">Rajiv</field>
<field name = "last name">Reddy</field>
<field name = "phone">9848022337</field>
<field name = "city">Hyderabad</field>
</doc>
<doc>
<field name = "id">002</field>
<field name = "first name">Siddarth</field>
<field name = "last name">Battacharya</field>
<field name = "phone">9848022338</field>
<field name = "city">Kolkata</field>
</doc>
<doc>
<field name = "id">003</field>
<field name = "first name">Rajesh</field>
<field name = "last name">Khanna</field>
<field name = "phone">9848022339</field>
<field name = "city">Delhi</field>
</doc>
<doc>
<field name = "id">004</field>
<field name = "first name">Preethi</field>
<field name = "last name">Agarwal</field>
<field name = "phone">9848022330</field>
<field name = "city">Pune</field>
</doc>
<doc>
<field name = "id">005</field>
<field name = "first name">Trupthi</field>
<field name = "last name">Mohanthy</field>
<field name = "phone">9848022336</field>
<field name = "city">Bhuwaeshwar</field>
</doc>
<doc>
<field name = "id">006</field>
<field name = "first name">Archana</field>
<field name = "last name">Mishra</field>
<field name = "phone">9848022335</field>
<field name = "city">Chennai</field>
</doc>
</add>
您可以看到,編寫的用於將資料新增到索引的XML檔案包含三個重要的標籤,即<add></add>,<doc></doc>和<field></field>。
add − 這是將文件新增到索引的根標籤。它包含一個或多個要新增的文件。
doc − 我們新增的文件應該用<doc></doc>標籤括起來。此文件包含欄位形式的資料。
field − field標籤包含文件欄位的名稱和值。
準備好文件後,您可以使用上一章中討論的任何方法將此文件新增到索引中。
假設XML檔案存在於Solr的bin目錄中,並且它要索引到名為my_core的核心,那麼您可以使用post工具將其新增到Solr索引中,如下所示:
[Hadoop@localhost bin]$ ./post -c my_core sample.xml
執行上述命令後,您將獲得以下輸出。
/home/Hadoop/java/bin/java -classpath /home/Hadoop/Solr/dist/Solr- core6.2.0.jar -Dauto = yes -Dc = my_core -Ddata = files org.apache.Solr.util.SimplePostTool sample.xml SimplePostTool version 5.0.0 Posting files to [base] url https://:8983/Solr/my_core/update... Entering auto mode. File endings considered are xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx, xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,htm,html,txt,log POSTing file sample.xml (application/xml) to [base] 1 files indexed. COMMITting Solr index changes to https://:8983/Solr/my_core/update... Time spent: 0:00:00.201
驗證
訪問Apache Solr Web介面的主頁,然後選擇核心my_core。嘗試透過在文字區域q中傳遞查詢“:”來檢索所有文件並執行查詢。執行後,您可以觀察到所需資料已新增到Solr索引中。
廣告