Solr 数据源
安装solr
解压直接安装,然后开放端口。
./bin/solr start 启动solr
测试所以没有将solr部署到tomcat上
解压:
tar -zxvf xxx.xx
创建核心
在solr文档中找到solr创建核心的方法。
./bin/solr create -c mytestCore01
使用上述命令创建新的核心(具体还是需要参考solr官方文档)
视图添加核心
使用谷歌浏览器以熟悉视图菜单(翻译)
核心配置
配置数据源
1.先将mysql驱动复制到local/solr/server/solr-webapp/webapp/WEB-INF/lib/
2再将dist文件下solr-dataimporthandler-7.1.0.jar拷贝到上述目录
配置数据源的时候xml 文件中& 是 "&"
创建一个 data-config.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
<dataSource name="source_his_maxx_assay_record" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.0.xxx:3306/mtrans" user="test" password="test" batchSize="-1" />
<dataSource name="source_his_maxx_assay_item_record" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.0.xxx:3306/mtrans" user="test" password="test" batchSize="-1" />
<document name="testDoc">
<entity dataSource="source_his_maxx_assay_record" name="source_his_maxx_assay_record" pk="id" query="select * from source_his_maxx_assay_record">
<field column="id" name="assatId"/>
<field column="id_no" name="idNo"/>
<field column="source_sys_code" name="sourceSysCode"/>
</entity>
</document>
</dataConfig>
注入数据源
在solrconfig.xml 文件中添加下面的代码
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
配置索引
如下:
<field name="tableType" type="plong" indexed="true" stored="true"/>
<field name="testNo" type="string" indexed="false" stored="false"/>
<field name="title" type="string" indexed="false" stored="false"/>
<field name="units" type="string" indexed="false" stored="false"/>
<field name="userName" type="string" indexed="false" stored="false"/>
参考
solr文档
solr 时区问题
搜索了半天发现都是瞎扯淡,所以自己进入solr 启动脚本查看了加载文件部分搜索 /SOLR_TIMEZONE
果然通过更改脚本能够进行时区的更改。
SOLR_TIMEZONE="GMT+08:00"
solr 访问权限(下次更新)
solr 定时增量
使用spring定时任务进行数据增量
在data-config.xml中添加:
deltaimporterQuery="select * from source_his_maxx_examine_record where id = '${dataimporter.delta.id}'"
deltaQuery="select id Ltable where timeIndex > '${dataimporter.last_index_time}'"
使用更新时间进行和上次增量时间进行对比;
url: /dataimport?command=delta-import&clean=false&commit=true
/**
* solr定时增量
* @throws IOException
*/
@Scheduled(cron = "0/30 * * * * ?")
public void deltaImport() throws IOException {
HttpGet httpGet = new HttpGet("url/dataimport?command=delta-import&clean=false&commit=true");
CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
HttpResponse httpResponse = closeableHttpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode()==200){
HttpEntity httpEntity = httpResponse.getEntity();
StringBuffer result = new StringBuffer();
if (httpEntity.getContent()!=null) {
bufferedReader = new BufferedReader(new InputStreamReader(httpEntity.getContent()));
String res;
while ((res = bufferedReader.readLine()) != null) {
result.append(res);
}
bufferedReader.close();
}
JSONObject res = JSONObject.parseObject(result.toString());
System.out.println("结果信息:"+res.getJSONObject("statusMessages").getString(""));
}
}
solrclient 数据访问:
@PostMapping("/get")
public JSONObject getSolrData() throws IOException, SolrServerException {
JSONObject jsonObject = new JSONObject();
Map<String,Object> params = new HashMap<String,Object>();
params.put("q","*:*");
SolrClient solrClient = new LBHttpSolrClient(URL);
SolrQuery solrQuery = new SolrQuery("*:*");
solrQuery.setStart(0);
solrQuery.setRows(20);
solrQuery.setSort("resultTime", SolrQuery.ORDER.desc); solrQuery.setFilterQueries("tableType:10410061000000001","idNo:320302600204281");
QueryResponse queryResponse = solrClient.query(solrQuery);
SolrDocumentList solrDocumentList = queryResponse.getResults();
serr(+solrDocumentList.getNumFound());
serr.("数据:"+JSONObject.toJSONString(solrDocumentList,true));
return null;
}
数据查询:范围查询:字段:[* TO *]