本文的示例代码参考SolrPractice
目录
环境
- Solr
brew install solr
brew services start solr
brew services list
docker run --name solr -p 8983:8983 -d solr
solr create -c core-practice
- MySQL驱动
wget http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.46.tar.gz
tar xf mysql-connector-java-5.1.46.tar.gz
cp mysql-connector-java-5.1.46/mysql-connector-java-5.1.46-bin.jar /usr/local/Cellar/solr/$(solr -v)/libexec/dist
- solrconfig.xml
vim /usr/local/Cellar/solr/$(solr -v)/server/solr/core-practice/conf/solrconfig.xml
# 添加以下配置
<lib dir="${solr.install.dir}/libexec/dist/" regex="mysql-connector-java-5.1.46-bin.jar" />
<lib dir="${solr.install.dir}/libexec/dist/" regex="solr-dataimporthandler-.*\.jar" />
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
- data-config.xml
vim /usr/local/Cellar/solr/$(solr -v)/server/solr/core-practice/conf/data-config.xml
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://120.55.98.237:3306/zhg"
user="root"
password="***"/>
<document>
<entity
name="article"
pk="id"
query="select * from article"
deltaImportQuery="SELECT * from article WHERE id='${dih.delta.id}'"
deltaQuery="SELECT id FROM article WHERE updated_at > '${dih.last_index_time}'"
>
<field column="id" name="id"/>
<field column="title" name="title"/>
<field column="content" name="content"/>
<field column="updated_at" name="updated_at"/>
</entity>
</document>
</dataConfig>
- managed-schema
vim /usr/local/Cellar/solr/$(solr -v)/server/solr/core-practice/conf/managed-schema
# 添加以下配置
<field name="title" type="string" indexed="true" stored="false" />
<field name="content" type="string" indexed="true" stored="false" />
<field name="updated_at" type="pdate" indexed="true" stored="false" />
brew services restart solr
- 全量导入数据
curl http://localhost:8983/solr/core-practice/dataimport?command=full-import
- 增量导入数据
curl http://localhost:8983/solr/core-practice/dataimport?command=delta-import
- 查询导入数据
curl http://localhost:8983/solr/core-practice/select?q=*:* | json
这里使用nodejs的json工具格式化数据: npm i -g json、
Startup
spring init -dweb,data-solr --build gradle SolrPractice
# cd SolrPractice
mv src/main/resources/application.properties src/main/resources/application.yml
vim src/main/resources/application.yml
spring:
data:
solr:
host: http://127.0.0.1:8983/solr/
vim src/main/java/com/example/SolrPractice/ArticleController.java
package com.example.SolrPractice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/articles")
public class ArticleController {
@GetMapping
public String getArticles() {
return "getArticles";
}
}
- 测试
./gradlew bootrun
curl localhost:8080/articles # 返回"getArticles"
Model
vim src/main/java/com/example/SolrPractice/Article.java
package com.example.SolrPractice;
import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.SolrDocument;
@SolrDocument(solrCoreName = "core-practice")
public class Article {
@Id
@Field
private int id;
@Field
private String title;
@Field
private String content;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
Repository
vim src/main/java/com/example/SolrPractice/ArticleRepository.java
package com.example.SolrPractice;
import org.springframework.data.solr.repository.SolrCrudRepository;
import java.util.List;
public interface ArticleRepository extends SolrCrudRepository<Article, Long> {
List<Article> findAllByContentContains(String query);
}
Controller
vim src/main/java/com/example/SolrPractice/ArticleController.java
package com.example.SolrPractice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/articles")
public class ArticleController {
@Autowired
private ArticleRepository articleRepository;
@GetMapping
public List<Article> getArticles(@RequestParam String query) {
return articleRepository.findAllByContentContains(query);
}
}
- 测试
curl localhost:8080/articles?query=5000 | json
curl localhost:8080/articles?query=%e6%88%aa%e8%87%b3%e7%9b%ae%e5%89%8d | json
上述query=截至目前 urlencode编解码可以参考UrlEncode编码/UrlDecode解码- 站长工具
中文分词
- 浏览器打开http://localhost:8983/solr/#/core-practice/analysis 分析"截至目前" 结果如下
solr_practice_01.png
cp /usr/local/Cellar/solr/$(solr -v)/libexec/contrib/analysis-extras/lucene-libs/lucene-analyzers-smartcn* /usr/local/Cellar/solr/$(solr -v)/server/solr-webapp/webapp/WEB-INF/lib/
vim /usr/local/Cellar/solr/$(solr -v)/server/solr/core-practice/conf/managed-schema
# 添加以下配置
<fieldType name="text_smartcn" class="solr.TextField" positionIncrementGap="0">
<analyzer type="index">
<tokenizer class="org.apache.lucene.analysis.cn.smart.HMMChineseTokenizerFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="org.apache.lucene.analysis.cn.smart.HMMChineseTokenizerFactory"/>
</analyzer>
</fieldType>
# 修改之前配置
<field name="content" type="text_smartcn" indexed="true" stored="false" multiValued="true"/>
brew services restart solr
- 浏览器打开http://localhost:8983/solr/#/core-practice/analysis 分析"截至目前" 结果如下
solr_practice_02.png