本文是基於Spring-data對solr的快速搭建,具體文檔請參考下面的官方。
首先安裝Solr,自行百度。
在Solr裏創建文件夾,文件夾名字爲下面要起的Core Admin的名字。這裏起名爲caa-new,創建conf和data兩個文件夾
conf結構如下:
下載中文分詞包:lucene-analyzers-smartcn-5.2.1.jar 放到 solr/server/solr-webapp/webapp/libs 下。
注意:之後修改schema.xml.bak。要改成這個schema.xml,刪掉managed-schema,這樣之後才能生成新的索引。
編輯data-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://10.23.203.34:3306/caa?autoReconnect=true&useUnicode=true&characterEncoding=UTF8&characterSetResults=UTF8"
user="root"
password="root" />
<document name="notice">
<entity name="notice" query="select * from notice where title like '%${dataimporter.request.title}%' and content like '%${dataimporter.request.content}%'">
<field column="id" name="id" />
<field column="title" name="title" />
<field column="content_solr" name="content" />
</entity>
</document>
</dataConfig>
編輯schema.xml
添加中文分詞類
<fieldType name="text_smart" class="solr.TextField" positionIncrementGap="100">
<analyzer type="index">
<tokenizer class="solr.SmartChineseSentenceTokenizerFactory"/>
<!--charFilter class="solr.HTMLStripCharFilterFactory" /-->
<filter class="solr.SmartChineseWordTokenFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="solr.SmartChineseSentenceTokenizerFactory"/>
<filter class="solr.SmartChineseWordTokenFilterFactory"/>
</analyzer>
</fieldType>
添加查詢字段
<field name="content" type="text_smart" indexed="true" stored="true"/>
<field name="title" type="text_smart" indexed="true" stored="true"/>
編輯solrconfig.xml
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">/opt/solr/server/solr/caa-new/conf/data-config.xml</str>
</lst>
</requestHandler>
之後如下圖,添加Core Admin。名字和上面的文件夾保持一致,這裏是caa-new
如下圖測試中文分詞是不是可以正確運作。
之後需要手動生成下索引
然後就可以happy search了……(這裏因爲沒有數據就不展示了。)
下面進行Spring-data-solr的配置。
首先是POM.xml依賴
<!-- solr -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-framework.version}</version>
</dependency>
注意,確保Spring的版本<spring-framework.version>4.1.4.RELEASE</spring-framework.version>
配置文件:solr.properties
solr.host=http://10.23.101.187:8983/solr
之後是讀取配置生成SolrService
@Configuration
@PropertySource("solr.properties")
@EnableSolrRepositories(basePackages={"com.github.tmxin.solr"},multicoreSupport=true)
public class SolrContext {
static final String SOLR_HOST = "solr.host";
@Resource
private Environment environment;
@Bean
public SolrServer solrServer() {
String solrHost = environment.getProperty(SOLR_HOST);
return new HttpSolrServer(solrHost);
}
}
全文檢索的實體類
@SolrDocument(solrCoreName="caa-new")
public class NoticeSolr {
@Id
@Indexed
public String id;
@Indexed
public String title;
@Indexed
public String content;
@Override
public String toString() {
return "NoticeSolr [title=" + title +" content="+content.replaceAll("\n", "") + ", ID=" + id + "]";
}
}
最後是接口定義層,需要
public interface NoticeSolrRepository extends SolrCrudRepository<NoticeSolr, String>{
@Query(value = "*:*")//, filters = {"title北京市"}
Page<NoticeSolr> findByID(Pageable page);
@Highlight(prefix = "<b>", postfix = "</b>")
HighlightPage<NoticeSolr> findByContent(String content, Pageable pageable);
@Highlight(prefix = "null", postfix = "null")
HighlightPage<NoticeSolr> findByTitle (String title, Pageable pageable);
@Highlight(prefix = "<basn>", postfix = "</basn>")
HighlightPage<NoticeSolr> findByTitleOrContentLike(String title, String content, Pageable pageable);
}
這些接口可以通過Spring-data的映射直接實現,不需要自己實現,這就是spring-data的便利之處
@Highlight是高亮顯示查詢的分詞。你可以自己定義標籤。
最後我們來測試一下。
public class Test {
private static final Log LOG = LogFactory.getLog(Test.class);
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(
SolrContext.class);
NoticeSolrRepository repositorys = ctx
.getBean(NoticeSolrRepository.class);
// Page<NoticeSolr> page = repositorys.findByID(new PageRequest(
// 0, 2));
// HighlightPage<NoticeSolr> page = repositorys.findByContent("拍賣", new PageRequest(
// 0, 2));
// HighlightPage<NoticeSolr> page = repositorys.findByTitle ("拍賣", new PageRequest(
// 0, 2));
HighlightPage<NoticeSolr> page = repositorys.findByTitleOrContentLike("s","s", new PageRequest(
0, 2));
LOG.info(page.getContent());
LOG.info("_____________下面是返回高亮的內容,其中page也包含不是高亮內容的其他數據,需要的話可以自行獲取。______________");
for (HighlightEntry<NoticeSolr> he : page.getHighlighted()) {
// A HighlightEntry belongs to an Entity (Book) and may have multiple highlighted fields (description)
for (Highlight highlight : he.getHighlights()) {
// Each highlight might have multiple occurrences within the description
for (String snipplet : highlight.getSnipplets()) {
LOG.info(snipplet.replaceAll("\n", ""));
}
}
}
LOG.info("************************************************");
for (NoticeSolr notice: page) {
LOG.info(notice);
}
}
}
具體代碼參照下載:
http://download.csdn.net/detail/tengmuxin/9707920
常年幫下載CSDN資料,Q819226396,只求好評,不爲錢