bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

Solr搜索引擎的安裝配置以及使用

這篇文章為大家分享Solr搜索引擎的安裝配置以及使用。文章不僅介紹了Solr搜索引擎的安裝和部署,還介紹Solr搜索引擎的簡單實用包括增刪改查等操作,希望大家通過這篇文章能有所收獲。

站在用戶的角度思考問題,與客戶深入溝通,找到青島網(wǎng)站設(shè)計與青島網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站制作、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名注冊、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋青島地區(qū)。

1.Solr安裝與配置

1.1Solr簡介

大多數(shù)搜索引擎應(yīng)用都必須具有某種搜索功能,問題是搜索功能往往是巨大的資源消耗并且它們由于沉重的數(shù)據(jù)庫加載而拖垮你的應(yīng)用的性能。

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

Solr是一個開源搜索平臺,用于構(gòu)建搜索應(yīng)用程序。 它建立在Lucene(全文搜索引擎)之上。 Solr是企業(yè)級的,快速的和高度可擴展的。 使用Solr構(gòu)建的應(yīng)用程序非常復(fù)雜,可提供高性能。

為了在CNET網(wǎng)絡(luò)的公司網(wǎng)站上添加搜索功能,Yonik Seely于2004年創(chuàng)建了Solr。并在2006年1月,它成為Apache軟件基金會下的一個開源項目。并于2016年發(fā)布最新版本Solr 6.0,支持并行SQL查詢的執(zhí)行。

Solr可以和Hadoop一起使用。由于Hadoop處理大量數(shù)據(jù),Solr幫助我們從這么大的源中找到所需的信息。不僅限于搜索,Solr也可以用于存儲目的。像其他NoSql數(shù)據(jù)庫一樣,它是一種非關(guān)系數(shù)據(jù)存儲和處理技術(shù)。

總之,Solr是一個可擴展的,可部署,搜索/存儲引擎,優(yōu)化搜索大量以文本為中心的數(shù)據(jù)。

1.2 Solr安裝

1:下載 Tomcat,解壓Tomcat壓縮包即可。
2:解壓 solr。
3:把 solr 下的dist目錄solr-4.10.3.war部署到 Tomcat\webapps下(去掉版本號)。
Solr搜索引擎的安裝配置以及使用
4:啟動 Tomcat解壓縮 war 包,啟動tomcat就是直接來到tomcat'的bin目錄下,雙擊startup,就可啟動
5:把solr下example/lib/ext 目錄下的所有的 jar 包,添加到 solr 的工程中(\WEB-INF\lib目錄下)。
Solr搜索引擎的安裝配置以及使用
6:創(chuàng)建一個 solrhome 。solr 下的/example/solr 目錄就是一個 solrhome。復(fù)制此目錄到D盤改名為solrhome
Solr搜索引擎的安裝配置以及使用
7:關(guān)聯(lián) solr 及 solrhome。需要修改 solr 工程的 web.xml 文件。

Solr搜索引擎的安裝配置以及使用

    <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>

8:啟動 Tomcat
http://IP:8080/solr/
Solr搜索引擎的安裝配置以及使用
到這里solr就安裝成功了,接下來是配置分詞器。

1.3中文分析器IK Analyzer
1.3.1 IK Analyzer簡介

IK Analyzer 是一個開源的,基亍 java 語言開發(fā)的輕量級的中文分詞工具包。從 2006年 12 月推出 1.0 版開始, IKAnalyzer 已經(jīng)推出了 4 個大版本。最初,它是以開源項目Luence 為應(yīng)用主體的,結(jié)合詞典分詞和文法分析算法的中文分詞組件。從 3.0 版本開始,IK 發(fā)展為面向 Java 的公用分詞組件,獨立亍 Lucene 項目,同時提供了對 Lucene 的默認優(yōu)化實現(xiàn)。在 2012 版本中,IK 實現(xiàn)了簡單的分詞歧義排除算法,標志著 IK 分詞器從單純的詞典分詞向模擬語義分詞衍化。

1.3.2 IK Analyzer配置

步驟:
1、把IKAnalyzer2012FF_u1.jar 添加到 solr 工程的 lib 目錄下
Solr搜索引擎的安裝配置以及使用
2、創(chuàng)建WEB-INF/classes文件夾  把擴展詞典、停用詞詞典、配置文件放到 solr 工程的 WEB-INF/classes 目錄下。
Solr搜索引擎的安裝配置以及使用
3、修改 Solrhome 的 schema.xml 文件,配置一個 FieldType,使用 IKAnalyzer

Solr搜索引擎的安裝配置以及使用

<fieldType name="text_ik" class="solr.TextField">
     <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>

1.4配置域

域相當于數(shù)據(jù)庫的表字段,用戶存放數(shù)據(jù),因此用戶根據(jù)業(yè)務(wù)需要去定義相關(guān)的Field(域),一般來說,每一種對應(yīng)著一種數(shù)據(jù),用戶對同一種數(shù)據(jù)進行相同的操作。
域的常用屬性:
?   name:指定域的名稱
?   type:指定域的類型
?   indexed:是否索引
?   stored:是否存儲
?   required:是否必須
?   multiValued:是否多值

1.4.1域

修改solrhome的schema.xml 文件  設(shè)置業(yè)務(wù)系統(tǒng) Field

Solr搜索引擎的安裝配置以及使用

    <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.4.2復(fù)制域

復(fù)制域的作用在于將某一個Field中的數(shù)據(jù)復(fù)制到另一個域中

<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.4.3動態(tài)域

當我們需要動態(tài)擴充字段時,我們需要使用動態(tài)域。對于品優(yōu)購,規(guī)格的值是不確定的,所以我們需要使用動態(tài)域來實現(xiàn)。需要實現(xiàn)的效果如下:
Solr搜索引擎的安裝配置以及使用
配置:

<dynamicField name="item_spec_*" type="string" indexed="true" stored="true" />  

2.Spring Data Solr實戰(zhàn)

2.1 Spring Data Solr簡介

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

2.2 Spring Data Solr入門
2.2.1 搭建工程

(1)創(chuàng)建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>

(2)在src/main/resources下創(chuàng)建  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服務(wù)器地址 -->
    <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>
2.2.2 @Field 注解

創(chuàng)建 cn.itcast.pojo 包,將品優(yōu)購的TbItem實體類拷入本工程  ,屬性使用@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;
.......
}
2.2.3 增加(修改)

創(chuàng)建測試類TestTemplate.java

@RunWith(SpringJUnit4Cla***unner.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();
    }
}
2.2.4 按主鍵查詢
    @Test
    public void testFindOne(){
        TbItem item = solrTemplate.getById(1, TbItem.class);
        System.out.println(item.getTitle());
    }
2.2.5 按主鍵刪除
@Test
    public void testDelete(){
        solrTemplate.deleteById("1");
        solrTemplate.commit();
    }
2.2.6 分頁查詢

首先循環(huán)插入100條測試數(shù)據(jù)

    @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);//每頁記錄數(shù)(默認10)
        ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);
        System.out.println("總記錄數(shù):"+page.getTotalElements());
        List<TbItem> list = page.getContent();
        showList(list);
    }   
    //顯示記錄數(shù)據(jù)
    private void showList(List<TbItem> list){       
        for(TbItem item:list){
            System.out.println(item.getTitle() +item.getPrice());
        }       
    }
2.2.7 條件查詢

Criteria 用于對條件的封裝:

@Test
    public void testPageQueryMutil(){   
        Query query=new SimpleQuery("*:*");
        Criteria criteria=new Criteria("item_title").contains("2");
        criteria=criteria.and("item_title").contains("5");      
        query.addCriteria(criteria);
        //query.setOffset(20);//開始索引(默認0)
        //query.setRows(20);//每頁記錄數(shù)(默認10)
        ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);
        System.out.println("總記錄數(shù):"+page.getTotalElements());
        List<TbItem> list = page.getContent();
        showList(list);
    }
2.2.8 刪除全部數(shù)據(jù)

@Test   

public void testDeleteAll(){

       Query query=new SimpleQuery("*:*");        solrTemplate.delete(query);        solrTemplate.commit();    }

看完上述內(nèi)容,你們對Solr搜索引擎有進一步的了解嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

文章題目:Solr搜索引擎的安裝配置以及使用
標題來源:http://vcdvsql.cn/article44/jheeee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器面包屑導(dǎo)航全網(wǎng)營銷推廣網(wǎng)站維護建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化