Solrj实现增删改查
生活随笔
收集整理的這篇文章主要介紹了
Solrj实现增删改查
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、準(zhǔn)備
在 solrhome/collection1/conf/schema.xml 配置文件中定義相關(guān)的字段。
<!--name:該字段的名稱。type:fieldType 字段對(duì)應(yīng)的名稱。"text_ik" 是我自定義的一個(gè)字段類型,并配置了中文分詞器。 --> <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_catalog_name" type="string" indexed="true" stored="true" /><!--當(dāng)從 "product_keywords" 字段搜索時(shí),會(huì)搜索 "product_name" 和 "product_description"字段。 --> <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"/>這里通過新建 Maven 項(xiàng)目完成測(cè)試,在pom.xml中添加對(duì)應(yīng)的依賴:
<!-- 這里使用 7.2.1 的版本 --><dependency><groupId>org.apache.solr</groupId><artifactId>solr-solrj</artifactId><version>7.2.1</version></dependency><!-- 添加 Junit 測(cè)試的依賴 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency>二、測(cè)試增刪改
增刪改相對(duì)來說比較簡(jiǎn)單,這里放在一起進(jìn)行測(cè)試,Solr 中沒有提供專門的修改方法,會(huì)根據(jù) id 在文檔中查找,如果沒有找到就是添加,找到了就會(huì)覆蓋原來的數(shù)據(jù),即修改。
2.1新增與修改
編寫 Java 代碼:
@Testpublic void indexCreateTest() throws Exception {// 創(chuàng)建和Solr 服務(wù)端的連接,并指定 Solr 實(shí)例是 "collection1"SolrClient client = new HttpSolrClient.Builder("http://192.168.248.136:8080/solr/collection1").build();// 創(chuàng)建 Solr 文檔對(duì)象SolrInputDocument document = new SolrInputDocument();// 向文檔對(duì)象中添加指定字段對(duì)應(yīng)的字段值,字段必須先定義后使用,且必須要有 id 字段// 如果是修改,只需要固定 id 字段的值,修改其他的字段即可document.addField("id", "a1");document.addField("product_catalog_name", "幽默雜貨");document.addField("product_price", "20");document.addField("product_name", "小王子");// 將文檔添加到 client 對(duì)象中client.add(document);// 提交client.commit();}在瀏覽器端進(jìn)行驗(yàn)證:
2.2刪除
編寫 Java 代碼:
@Testpublic void indexDelTest() throws Exception {SolrClient client = new HttpSolrClient.Builder("http://192.168.248.136:8080/solr/collection1").build();// 根據(jù) id 刪除// client.deleteById("a1");// 根據(jù)查詢刪除, *:* 表示刪除所有client.deleteByQuery("product_name:小王子");client.commit();}這里就不在瀏覽器驗(yàn)證了。
三、測(cè)試查詢
相對(duì)于增刪改操作,往往需要我們做的是查詢操作。查詢操作也相對(duì)比較復(fù)雜,有關(guān)查詢的設(shè)置,可以在瀏覽器端查看,只要在代碼中設(shè)置即可。
查詢高亮的結(jié)果比較麻煩,這里將瀏覽器端的查詢結(jié)果貼出來,希望能幫助大家更好理解高亮數(shù)據(jù)的獲取方式。需要高亮顯示的字段是:product_name,查詢關(guān)鍵字是:小王子。
編寫 Java 代碼:
@Testpublic void indexSearchTest() throws Exception {SolrClient client = new HttpSolrClient.Builder("http://192.168.248.136:8080/solr/collection1").build();// 創(chuàng)建查詢對(duì)象SolrQuery query = new SolrQuery();// 設(shè)置默認(rèn)搜索字段,如果不指定搜索的字段,則從默認(rèn)字段中搜索query.set("df", "product_keywords");// 指定搜索域與搜索的關(guān)鍵字query.setQuery("product_name:小王子");// 如果不定搜索域,則從默認(rèn)搜索域中搜索,如下//query.setQuery("手機(jī)");// 設(shè)置搜索的過濾器,只搜索"product_price" 在 10-20 之間的query.addFilterQuery("product_price:[15 TO 30]");// 設(shè)置起始的條數(shù),默認(rèn)是 0query.setStart(0);// 設(shè)置查詢的條數(shù),默認(rèn)是 10 query.setRows(5);// 設(shè)置高亮query.setHighlight(true);// 設(shè)置顯示高亮的字段query.addHighlightField("product_name");// 設(shè)置高亮字段值的前綴query.setHighlightSimplePre("<span style=\"color:red\">");// 設(shè)置高亮字段值的后綴query.setHighlightSimplePost("</span>");// 獲得查詢結(jié)果的響應(yīng)對(duì)象QueryResponse response = client.query(query);// 從響應(yīng)對(duì)象中獲得結(jié)果集對(duì)象SolrDocumentList list = response.getResults();System.out.println("查詢到的總記錄數(shù):" + list.getNumFound());// 遍歷結(jié)果集for (SolrDocument document : list){System.out.println("product_price : " + document.get("product_price"));System.out.println("product_name : " + document.get("product_name"));// 從響應(yīng)對(duì)象中獲得高亮,并處理Map<String, Map<String, List<String>>> map = response.getHighlighting();List<String> lightList = map.get(document.get("id")).get("product_name");if(lightList != null && lightList.size() > 0) {System.out.println("high lighting product_name : " + lightList.get(0));}System.out.println("==================================");}}查詢結(jié)果輸出:
(完)
總結(jié)
以上是生活随笔為你收集整理的Solrj实现增删改查的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 进入pe模式 安装不了怎么办 如何在PE
- 下一篇: ActiveMQ集成Spring