首先需要在虚拟机上安装jdk,tomcat,和solr
solr:
链接:https://pan.baidu.com/s/1j_I5dR0LByxM4AzlBauxqg
提取码:p8ih
IK Anyalyzer提词器:
链接:https://pan.baidu.com/s/1EOeAaZgSv2z_obQxB3RCdQ
提取码:83n2
以及配置方法:
链接:https://pan.baidu.com/s/12AhacCSgfgMTTpoKxxiM4Q
提取码:stt6
启动服务器放问solr
可以在document中新建:
然后可以在Query查询
左侧的查询条件:
-
1.q:查询条件:
- 星号:星号(*):查询全部
- abc:数据中心(之前在安装IK的时候,里面最后自己定义的属性abc,type=text_ik)(其中数据中心会被拆词器分成:数据和中心)
- abc:“数据中心”,则数据中心不会被拆词查找
- 在schema.xml中配置复合属性。
<field name="anything" type="text_ik" indexed="true" stored="true" multiValue="true"/> <copyField source="abc" dest="anything" /> <copyField source="def" dest="anything" />
-
fq:并列条件
-
sort:排序,例如:id desc 是按照id的降序排序
-
start rows 从start开始显示rows条数据
-
fl:显示哪个属性
-
df:默认查询数据
代码
首先需要导入 solr-solrj,junit以及commons-loggings
package com.bl;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;
/**
* 增加删除修改都需要提交事务
* @author lenovo
*
*/
public class demo1 {
/**
* 新增|修改
* @throws SolrServerException
* @throws IOException
*/
// @Test
public void testInsert() throws SolrServerException, IOException{
SolrClient client = new HttpSolrClient("http://192.168.213.152:8080/solr");
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "001");
doc.addField("abc", "数据中心");
doc.addField("efg", "啦啦啦");
client.add(doc);
client.commit();
}
/**
* 删除
* @throws SolrServerException
* @throws IOException
*/
// @Test
public void testDelete() throws SolrServerException, IOException{
SolrClient client = new HttpSolrClient("http://192.168.213.152:8080/solr");
client.deleteById("001");
client.commit();
}
/**
* 查询
* @throws SolrServerException
* @throws IOException
*/
@Test
public void testQuery() throws SolrServerException, IOException{
SolrClient client = new HttpSolrClient("http://192.168.213.152:8080/solr");
//可视化界面左侧条件
SolrQuery params = new SolrQuery();
//设置q
params.setQuery("anything:数据");
//设置分页
params.setStart(0);//从第几条开始查询
params.setRows(10);//查询几个
//启用高亮
params.setHighlight(true);
//设置高亮列
params.addHighlightField("abc");
//设置前缀
params.setHighlightSimplePre("<span style='color:red;'>");
//设置后缀
params.setHighlightSimplePost("</span>");
//相当于点击了查询按钮,本质上向solr web服务器发送请求,并接收响应
QueryResponse response = client.query(params);
//取出高亮
Map<String, Map<String, List<String>>> hh = response.getHighlighting();
//取出docs{}
SolrDocumentList solrList = response.getResults();
for(SolrDocument doc:solrList){
System.out.println(doc.getFieldValue("id"));
System.out.println("未高亮"+doc.getFieldValue("abc"));
Map<String,List<String>> map = hh.get(doc.getFieldValue("id"));
List<String> list = map.get("abc");
if(list!=null&&list.size()>0){
System.out.println("高亮:"+list.get(0));
}else{
System.out.println("没有高亮内容");
}
System.out.println(doc.getFieldValue("efg"));
}
}
}