上一章讲了部署solr,这一章讲如何在asp.net core中实现
1. 用到正式数据,这边直接用正式的数据库和表了.
首先创建一下新的目录 newsInfo
拷贝一下之前core0里面的conf到 newsInfo里面
2. data-config更改一下查询连接及对应列
<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1/kevis_etc" user="root"
password="123456" batchSize="-1"/>
<document name="News">
<entity name="NewsInfo" pk="id" query="SELECT id,Guid,CreateTime,NewsTitle,SubTitle,NewsContent,ClassId FROM tbl_NewsInfo ">
<field column="id" name="id" />
<field column="Guid" name="Guid" />
<field column="NewsTitle" name="NewsTitle" />
<field column="SubTitle" name="SubTitle" />
<field column="NewsContent" name="NewsContent" />
<field column="CreateTime" name="CreateTime" />
<field column="ClassId" name="ClassId" />
</entity>
</document>
</dataConfig>
3. managed-schema 加入NewsInfo需要的列
<field name="id" type="tlongs" indexed="true" stored="true" required="true" multiValued="false" />
<field name="Guid" type="text_general" indexed="true" stored="true" multiValued="false"/>
<field name="NewsTitle" type="text_general" indexed="true" stored="true" multiValued="false"/>
<field name="SubTitle" type="string" indexed="true" stored="true"/>
<field name="NewsContent" type="string" indexed="true" stored="true"/>
<field name="ClassId" type="int" indexed="true" stored="true"/>
<field name="CreateTime" type="tdate" indexed="true" stored="true"/>
4. 回到页面,创建一下 newsInfo
5. 生成一下数据
6. 程序中 先导入一下包
搜索 SolrNet ,导入到 application层
KevisEtcWebModule.cs ----> PreInitialize() 加入
清除上次请求(不清除会导致重复请求报错)我这边不加也没发现错误
//SolrNet.Startup.Container.Clear();
//SolrNet.Startup.InitContainer();
SolrNet.Startup.Init<NewsInfo_Solr>("http://127.0.0.1:8080/solr/newsInfo/");
7. 实现查询方法 ,按如下创建类
SolrBaseAppService.cs
ISolrBaseAppService.cs
NewsInfo_Solr.cs
News_SolrAppService.cs
INews_SolrAppService.cs
8. 前端调用