Solr:後臺管理介面的使用(管理索引庫、匯入資料庫資料、查詢)
阿新 • • 發佈:2020-12-23
1、管理索引庫
(1)新增
初始查詢結果:
{ "responseHeader": { "status": 0, "QTime": 1, "params": { "q": "*:*", "indent": "true", "wt": "json", "_": "1608643748789" } }, "response": { "numFound": 2, "start": 0, "docs": [ { "id": "1", "title": [ "zhai" ], "_version_": 1686785156649582600 }, { "id": "change.me", "title": [ "change.me" ], "_version_": 1686785221848989700 } ] } }
提交資料:
{"id":"2","title":"test"}
再次查詢:
{ "responseHeader": { "status": 0, "QTime": 1, "params": { "q": "*:*", "indent": "true", "wt": "json", "_": "1608643854340" } }, "response": { "numFound": 3, "start": 0, "docs": [ { "id": "1", "title": [ "zhai" ], "_version_": 1686785156649582600}, { "id": "change.me", "title": [ "change.me" ], "_version_": 1686785221848989700 }, { "id": "2", "title": [ "test" ], "_version_": 1686785333280112600 } ] } }
(2)更新
當ID相同的時候進行的是更新操作:
提交一個已經存在的ID
{"id":"2","title":"hello"}
查詢:
{ "responseHeader": { "status": 0, "QTime": 1, "params": { "q": "*:*", "indent": "true", "wt": "json", "_": "1608643946423" } }, "response": { "numFound": 3, "start": 0, "docs": [ { "id": "1", "title": [ "zhai" ], "_version_": 1686785156649582600 }, { "id": "change.me", "title": [ "change.me" ], "_version_": 1686785221848989700 }, { "id": "2", "title": [ "hello" ], "_version_": 1686785397815771100 } ] } }
執行過程是先刪除再新增
(3)刪除
刪除ID為1的索引:
<delete>
<id>1</id>
</delete>
<commit/>
查詢結果中ID為1的索引全部消失了
按照條件刪除ID為2的索引:
<delete>
<query>id:2</query>
</delete>
<commit/>
刪除所有:
<delete> <query>*:*</query> </delete> <commit/>
2、配置資料匯入
(1)jar包
將以上兩個jar包複製到核的lib目錄下,然後將mysql的驅動包也拷貝到此目錄下:
(2)配置solrconfig.xml檔案,新增一個requestHandler
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst> </requestHandler>
(3)建立一個data-config.xml,儲存到collection1\conf\目錄下(與solrconfig.xml在同一級目錄下)
<?xml version="1.0" encoding="UTF-8" ?> <dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/lucene" user="root" password="root"/> <document> <entity name="product" query="SELECT pid,name,catalog_name,price,description,picture FROM products "> <field column="pid" name="id"/> <field column="name" name="product_name"/> <field column="catalog_name" name="product_catalog_name"/> <field column="price" name="product_price"/> <field column="description" name="product_description"/> <field column="picture" name="product_picture"/> </entity> </document> </dataConfig>
(4)向資料庫匯入資料
(5)設定業務系統域(schema.xml)
<field name="product_name" type="text_ik" indexed="true" stored="true"/> <field name="product_price" type="float" indexed="true" stored="true"/> <field name="product_description" type="text_ik" indexed="true" stored="false" /> <field name="product_picture" type="string" indexed="false" stored="true" /> <field name="product_catalog_name" type="string" indexed="true" stored="true" />
在第三步中,只有ID能夠域系統中已經存在的ID進行對映,因此,需要向系統中新增域來滿足業務需求
<field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/> <copyField source="product_name" dest="product_keywords"/> <copyField source="product_description" dest="product_keywords"/>
上面是配置根據關鍵字搜尋的拷貝域,就是將商品的名稱和描述拷貝到一個域中,搜尋的時候既不是根據名稱或描述搜尋,而是將其拷貝到一個域中將兩個欄位組合後進行搜尋
(6)重啟Tomcat
(7) 注意點
如果是Mysql8需要更新jar包到8版本,同時,獲取資料庫的連線的方法也要注意更改
<?xml version="1.0" encoding="UTF-8" ?> <dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.cj.jdbc.Driver" url="jdbc:mysql://localhost:3306/lucene?serverTimezone=CST" user="root" password="root"/> <document> <entity name="product" query="SELECT pid,name,catalog_name,price,description,picture FROM products "> <field column="pid" name="id"/> <field column="name" name="product_name"/> <field column="catalog_name" name="product_catalog_name"/> <field column="price" name="product_price"/> <field column="description" name="product_description"/> <field column="picture" name="product_picture"/> </entity> </document> </dataConfig>
(8)測試
查詢:
3、查詢
(1)關鍵字查詢
(2)多條件查詢
(3)過濾
(4)排序
(5)只要某一個欄位
用逗號分隔就可以查詢多個欄位
(6)設定查詢的關鍵字高亮
執行結果: