天天看點

Solr

Solr

bin:solr的運作腳本

contrib:solr的一些貢獻軟體/插件,用于增強solr的功能。

dist:該目錄包含build過程中産生的war和jar檔案,以及相關的依賴檔案。

docs:solr的API文檔

example:solr工程的例子目錄:

example/solr:

該目錄是一個包含了預設配置資訊的Solr的Core目錄。

example/multicore:

該目錄包含了在Solr的multicore中設定的多個Core目錄。 

example/webapps:

    該目錄中包括一個solr.war,該war可作為solr的運作執行個體工程。

licenses:solr相關的一些許可資訊

solr 需要運作在一個Servlet容器中,Solr4.10.3要求jdk使用1.7以上,Solr預設提供Jetty(java寫的Servlet容器),本教程使用Tocmat作為Servlet容器,環境如下:

Solr:Solr4.10.3

Jdk:jdk1.7.0_72

Tomcat:apache-tomcat- 8.5

Solr整合tomcat 3步

1\solr-4.10.3\example\webapps\solr.war 放到 tomcat 的webapps 裡面 解壓後

2\solr-4.10.3\example\lib\ext\*.jar 放到Tomcat 8.5\webapps\solr\WEB-INF\lib\下

3\solr-4.10.3\example\solr solr資料的根檔案  放到你希望的目錄中

Tomcat 8.5\webapps\solr\WEB-INF\web.xml 

<!--打開注釋-->

<env-entry>

       <env-entry-name>solr/home</env-entry-name>

<!--修改為solr資料的根檔案目錄-->

<env-entry-value>D:\solrHome</env-entry-value>

       <env-entry-type>java.lang.String</env-entry-type>

    </env-entry>

solr 安裝 中文分詞器

1\IKAnalyzer2012FF_u1.jar 将jar 包 放到Tomcat 8.5\webapps\solr\WEB-INF\lib

2\ext.dic,IKAnalyzer.cfg.xml,stopword.dic 配置檔案 放到Tomcat 8.5\webapps\solr\WEB-INF\classes(手動建立這個檔案夾)或者放到 solr的classpath下。

3\在schema.xml中添加一個自定義的fieldType,使用中文分析器。

<!-- IKAnalyzer-->

    <fieldType name="text_ik" class="solr.TextField">

      <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>

    </fieldType>

4\定義field,指定field的type屬性為text_ik

<!--IKAnalyzer Field-->

   <field name="title_ik" type="text_ik" indexed="true" stored="true" />

   <field name="content_ik" type="text_ik" indexed="true" stored="false" multiValued="true"/>

5\重新開機tomcat

solr 

增加

選擇:/collection1/documents

選擇:json 

Document(s):

{"id":"change.me","title":"change.me"}

點選:Submit Document

改 增加相同id 就可以修改

删除索引格式如下:

選擇:xml

1) 删除制定ID的索引 

<delete>

<id>8</id>

</delete>

<commit/>

2) 删除查詢到的索引資料 

<query>product_catalog_name:幽默雜貨</query>

3) 删除所有索引資料

<query>*:*</query>

選項:/collection1/query

添加各種添加

點選:Execute Query

使用dataimport插件批量導入資料。

1、把dataimport插件依賴的jar包添加到solrcore(collection1\lib)中

2、還需要添加mysql驅動

3、配置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>      

4、建立一個data-config.xml,儲存到collection1\conf\目錄下

<?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="hiroot"/>   
<document>   
<!-- name 随意-->
<entity name="product" query="SELECT pid,name,catalog_name,price,description,picture FROM products ">
<!--映射 pid 到 solr 中 id-->
 <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>      

5、如果不使用Solr提供的Field可以針對具體的業務需要自定義一套Field,如下是商品資訊Field

<!--product-->
   <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" />
<!-- 聯合 域  将兩個 域 連接配接在一起-->
   <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"/>      

搭建 solrj

maven:

<!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.3</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>      
package com.stevezong.solrj;
import java.util.List;
import java.util.Map;
import org.apache.solr.client.solrj.SolrQuery;
/**
 * Solrj 管理
 * 增上改查
 * @author zongx
 *
 */
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;
public class SolrJManager {
@Test
public void testAdd() throws  Exception{
String baseURL = "http://localhost:8080/solr/";
SolrServer server = new HttpSolrServer(baseURL);
SolrInputDocument doc = new SolrInputDocument();
doc.setField("id", "haha");
doc.setField("name", "steve");
server.add(doc);
server.commit();
}
@Test
public void testDel() throws Exception{
String baseURL = "http://localhost:8080/solr/";
SolrServer server = new HttpSolrServer(baseURL);
server.deleteByQuery("*:*", 1000);
}
@Test
public void testUpdate() throws Exception{
//更新和添加一樣,Id 相同就是更新 Id 不同就是添加
String baseURL = "http://localhost:8080/solr/";
SolrServer server = new HttpSolrServer(baseURL);
SolrInputDocument doc = new SolrInputDocument();
doc.setField("id", "haha");
doc.setField("name", "zong");
server.add(doc);
server.commit();
}
@Test
public void testSELECT() throws Exception{
String baseURL = "http://localhost:8080/solr/";
SolrServer server = new HttpSolrServer(baseURL);
//查詢條件
SolrQuery solrQuery = new SolrQuery();
//名稱為台燈
solrQuery.set("q", "product_name:台燈");
//過濾條件1
solrQuery.set("fq", "product_catalog_name:幽默雜貨");
//過濾條件2
solrQuery.set("fq", "product_price:[* TO 20]");
//排序
solrQuery.addSort("product_price",ORDER.desc);
//分頁 從0
solrQuery.setStart(0);
//分頁 5行
solrQuery.setRows(5);
//預設域
solrQuery.set("df", "product_name");
//需要的域
solrQuery.set("fl", "id,product_name");
//開啟高亮
solrQuery.setHighlight(true);
//高亮的域
solrQuery.addHighlightField("product_name");
//高亮的域的字首
solrQuery.setHighlightSimplePre("<span style='color:red'>");
//高亮域的字尾
solrQuery.setHighlightSimplePost("</span>");
//普通的資訊
QueryResponse response = server.query(solrQuery);
//高亮的資訊
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
//Map<id,Map<域名,List<info>>>
SolrDocumentList docs = response.getResults();
for (SolrDocument solrDocument : docs) {
System.out.println(solrDocument.get("product_catalog_name"));
System.out.println(solrDocument.get("product_price"));
System.out.println(solrDocument.get("product_name"));
System.out.println(solrDocument.get("id"));
System.out.println(solrDocument.get("product_picture"));
Map<String, List<String>> map = highlighting.get(solrDocument.get("id"));
List<String> list = map.get("product_name");
for (String string : list) {
System.out.println(string);
}
}
}
}      
下一篇: FastDFS

繼續閱讀