Solr

什麼是Solr

  大多數搜索引擎應用都必須具有某種搜索功能,問題是搜索功能往往是巨大的資源消耗並且它們由於沉重的數據庫加載而拖垮你的應用的性能。

  這就是爲什麼轉移負載到一個外部的搜索服務器是一個不錯的主意,Apache Solr是一個流行的開源搜索服務器,它通過使用類似REST的HTTP API,這就確保你能從幾乎任何編程語言來使用solr。

  Solr是一個開源搜索平臺,用於構建搜索應用程序。 它建立在Lucene(全文搜索引擎)之上。 Solr是企業級的,快速的和高度可擴展的。 使用Solr構建的應用程序非常複雜,可提供高性能。

  爲了在CNET網絡的公司網站上添加搜索功能,Yonik Seely於2004年創建了Solr。並在2006年1月,它成爲Apache軟件基金會下的一個開源項目。並於2016年發佈最新版本Solr 6.0,支持並行SQL查詢的執行。

  Solr可以和Hadoop一起使用。由於Hadoop處理大量數據,Solr幫助我們從這麼大的源中找到所需的信息。不僅限於搜索,Solr也可以用於存儲目的。像其他NoSQL數據庫一樣,它是一種非關係數據存儲和處理技術。

  總之,Solr是一個可擴展的,可部署,搜索/存儲引擎,優化搜索大量以文本爲中心的數據。

Solr 安裝

  1. 安裝Tomcat,解壓即可。
  2. 解壓Solr。
  3. 把 solr 下的dist目錄solr-4.10.3.war部署到 Tomcat\webapps下(去掉版本號)。
  4. 啓動 Tomcat解壓縮 war 包
  5. 把solr下example/lib/ext 目錄下的所有的 jar 包,添加到 solr 的工程中(\WEB-INF\lib目錄下)。
  6. 創建一個 solrhome 。solr 下的/example/solr 目錄就是一個 solrhome。複製此目錄到D盤改名爲solrhome
  7. 關聯 solr 及 solrhome。需要修改 solr 工程的 web.xml 文件。
 <env-entry>
       <env-entry-name>solr/home</env-entry-name>
       <env-entry-value>d:\solrhome</env-entry-value>
       <env-entry-type>java.lang.String</env-entry-type>
 </env-entry>
  1. 啓動Tomcat :http://IP:8080/solr/
    在這裏插入圖片描述

中文分析器 IK Analyzer

  1. 簡介
      IK Analyzer 是一個開源的,基亍 java 語言開發的輕量級的中文分詞工具包。從 2006年 12 月推出 1.0 版開始, IKAnalyzer 已經推出了 4 個大版本。最初,它是以開源項目Luence 爲應用主體的,結合詞典分詞和文法分析算法的中文分詞組件。從 3.0 版本開始,IK 發展爲面向 Java 的公用分詞組件,獨立亍 Lucene 項目,同時提供了對 Lucene 的默認優化實現。在 2012 版本中,IK 實現了簡單的分詞歧義排除算法,標誌着 IK 分詞器從單純的詞典分詞向模擬語義分詞衍化。
  2. IK Analyzer配置
    (1)把IKAnalyzer2012FF_u1.jar 添加到 solr 工程的 lib 目錄下
    (2)創建WEB-INF/classes文件夾 把擴展詞典、停用詞詞典、配置文件放到 solr 工程的 WEB-INF/classes 目錄下。
    (3)修改 Solrhome 的 schema.xml 文件,配置一個 FieldType,使用 IKAnalyzer
<fieldType name="text_ik" class="solr.TextField">
	<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
  1. 配置域
      域相當於數據庫的表字段,用戶存放數據,因此用戶根據業務需要去定義相關的Field(域),一般來說,每一種對應着一種數據,用戶對同一種數據進行相同的操作。

域的常用屬性:

  • name:指定域的名稱
  • type:指定域的類型
  • ndexed:是否索引
  • indexed:是否索引
  • stored:是否存儲
  • required:是否必須
  • multiValued:是否多值

(1)修改solrhome的schema.xml 文件 設置系統 Field

	<field name="item_goodsid" type="long" indexed="true" stored="true"/>
	<field name="item_title" type="text_ik" indexed="true" stored="true"/>
	<field name="item_price" type="double" indexed="true" stored="true"/>
	<field name="item_image" type="string" indexed="false" stored="true" />
	<field name="item_category" type="string" indexed="true" stored="true" />
	<field name="item_seller" type="text_ik" indexed="true" stored="true" />
	<field name="item_brand" type="string" indexed="true" stored="true" />

  1. 複製域
      複製域的作用在於將某一個Field中的數據複製到另一個域中
<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="item_title" dest="item_keywords"/>
<copyField source="item_category" dest="item_keywords"/>
<copyField source="item_seller" dest="item_keywords"/>
<copyField source="item_brand" dest="item_keywords"/>
  1. 動態域
      當我們需要動態的擴展字段的時候需要使用動態域,比如說商品的規格。
    在這裏插入圖片描述
    配置:
<dynamicField name="item_spec_*" type="string" indexed="true" stored="true" />	

Spring Data Solr

簡介:
  雖然支持任何編程語言的能力具有很大的市場價值,你可能感興趣的問題是:我如何將Solr的應用集成到Spring中?可以,Spring Data Solr就是爲了方便Solr的開發所研製的一個框架,其底層是對SolrJ(官方API)的封裝。

入門小Demo

  1. 工程搭建
    創建maven工程,pom.xml 中引入依賴
   <dependencies>
	<dependency>
	    <groupId>org.springframework.data</groupId>
	    <artifactId>spring-data-solr</artifactId>
	    <version>1.5.5.RELEASE</version>
	</dependency> 
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-test</artifactId>
		<version>4.2.4.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.9</version>
	</dependency>
  </dependencies>

在src/main/resources下創建 applicationContext-solr.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:solr="http://www.springframework.org/schema/data/solr"
	xsi:schemaLocation="http://www.springframework.org/schema/data/solr 
  		http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- solr服務器地址 -->
	<solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr" />
	<!-- solr模板,使用solr模板可對索引庫進行CRUD的操作 -->
	<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
		<constructor-arg ref="solrServer" />
	</bean>
</beans>

@Field 註解
  創建一個實體類,屬性使用@Field 註解標識,如果屬性與配置文件定義的域名稱不一致,需要在註解中指定域名稱。

public class TbItem implements Serializable{

	@Field
    private Long id;

	@Field("item_title")
    private String title;
	    
    @Field("item_price")
    private BigDecimal price;

    @Field("item_image")
    private String image;

    @Field("item_goodsid")
    private Long goodsId;

    @Field("item_category")
    private String category;

    @Field("item_brand")
    private String brand;

    @Field("item_seller")
    private String seller;
.......
}

增刪改查

創建測試類 TestTemplate.java

增加:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-solr.xml")
public class TestTemplate {

	@Autowired
	private SolrTemplate solrTemplate;
	
	@Test
	public void testAdd(){
		TbItem item=new TbItem();
		item.setId(1L);
		item.setBrand("華爲");
		item.setCategory("手機");
		item.setGoodsId(1L);
		item.setSeller("華爲2號專賣店");
		item.setTitle("華爲Mate9");
		item.setPrice(new BigDecimal(2000));		
		solrTemplate.saveBean(item);
		solrTemplate.commit();
	}
}

按主鍵查詢

	@Test
	public void testFindOne(){
		TbItem item = solrTemplate.getById(1, TbItem.class);
		System.out.println(item.getTitle());
	}

按住鍵刪除

	@Test
	public void testDelete(){
		solrTemplate.deleteById("1");
		solrTemplate.commit();
	}

分頁查詢

首先循環插入100條測試數據

	@Test
	public void testAddList(){
		List<TbItem> list=new ArrayList();
		
		for(int i=0;i<100;i++){
			TbItem item=new TbItem();
			item.setId(i+1L);
			item.setBrand("華爲");
			item.setCategory("手機");
			item.setGoodsId(1L);
			item.setSeller("華爲2號專賣店");
			item.setTitle("華爲Mate"+i);
			item.setPrice(new BigDecimal(2000+i));	
			list.add(item);
		}
		
		solrTemplate.saveBeans(list);
		solrTemplate.commit();
	}

編寫分頁代碼

	
	@Test
	public void testPageQuery(){
		Query query=new SimpleQuery("*:*");
		query.setOffset(20);//開始索引(默認0)
		query.setRows(20);//每頁記錄數(默認10)
		ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);
		System.out.println("總記錄數:"+page.getTotalElements());
		List<TbItem> list = page.getContent();
		showList(list);
	}	
	//顯示記錄數據
	private void showList(List<TbItem> list){		
		for(TbItem item:list){
			System.out.println(item.getTitle() +item.getPrice());
		}		
	}

全部刪除

	@Test
	public void testDeleteAll(){
		Query query=new SimpleQuery("*:*");
		solrTemplate.delete(query);
		solrTemplate.commit();
	}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章