本文的示例代码参考SolrPractice
目录
-
环境
Solr
MySQL驱动
solrconfig.xml
data-config.xml
managed-schema
Startup
Model
Repository
Controller
中文分词
环境
- 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
# 添加以下配置
data-config.xml
- data-config.xml
vim /usr/local/Cellar/solr/$(solr -v)/server/solr/core-practice/conf/data-config.xml
- managed-schema
vim /usr/local/Cellar/solr/$(solr -v)/server/solr/core-practice/conf/managed-schema
# 添加以下配置
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 {
List 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 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 分析"截至目前" 结果如下
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
# 添加以下配置
# 修改之前配置
brew services restart solr
- 浏览器打开http://localhost:8983/solr/#/core-practice/analysis 分析"截至目前" 结果如下
参考
macOS安装Solr并索引MySQL
Solr【一】:初识Solr
Solr【二】:Spring Boot项目中使用Solr
spring-boot-sample-data-solr
Solr6.5配置中文分词IKAnalyzer和拼音分词pinyinAnalyzer (二)