Document操做索引apache
Query查询索引服务器
建立maven项目,在maven项目中导入依赖maven
<dependency>spa
<groupId>org.apache.solr</groupId>.net
<artifactId>solr-solrj</artifactId>server
<version>4.10.1</version>xml
</dependency>对象
<dependency>索引
<groupId>junit</groupId>ci
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
/**
* 使用document来插入index
* @throws SolrServerException
* @throws IOException
*/
public void addIndexBydocuments() throws SolrServerException, IOException{
HttpSolrServer server = new HttpSolrServer("http://127.0.0.1:8983/solr/core_one");
//使用document来操做solr
SolrInputDocument solrInputDocument = new SolrInputDocument();
//设置记录的每一个必须属性,非必须的属性按需设置
solrInputDocument.addField("id", "3");
solrInputDocument.addField("title", "华为手机低价处理");
solrInputDocument.addField("price", 1255);
solrInputDocument.addField("name", "华为手机");
server.add(solrInputDocument); //使用document添加记录
server.commit(); //提交操做
}
使用bean类直接操做index
public class Item {
private String id;
private String name;
@Field
private String title;
@Field
private long price;
}
注:在shema中存在的field必须加上@Field注解,不然会报错
/**
* 使用bean对象添加index
* @throws Exception
*/
@Test
public void addIndexByBean() throws Exception{
HttpSolrServer server = new HttpSolrServer("http://127.0.0.1:8983/solr/wxdlCore");
//封装bean对象
Item item = new Item();
item.setId("6");
item.setName("小米手机");
item.setPrice(999l);
item.setTitle("小米手机热销中");
//根据bean添加索引
server.addBean(item);
server.commit();
}
/**
* 根据id删除index
* @throws Exception
*/
@Test
public void delIndexById() throws Exception{
HttpSolrServer server = new HttpSolrServer("http://127.0.0.1:8983/solr/wxdlCore");
server.deleteById("change.me");
server.commit();
}
/**
* 根据表达式查询结果删除
* @throws Exception
*/
@Test
public void delIndexByExpression() throws Exception{
HttpSolrServer server = new HttpSolrServer("http://127.0.0.1:8983/solr/wxdlCore");
server.deleteByQuery("title:小米*");
server.commit();
}
deleteByQuery(query) 此处的query是一个solrQuery,对应query界面中的q :
/**
* 对index的修改就是对已存在的index进行添加操做,主键不修改
* @throws SolrServerException
* @throws IOException
*/
@Test
public void updateIndex() throws IOException, SolrServerException{
Item item = new Item();
item.setId("6");
item.setTitle("小米2 火热来袭 低价热销");
item.setName("小米2");
server.addBean(item);
server.commit();
}
/**
* 使用document获取查询结果
* @throws Exception
*/
@Test
public void getIndexByDocument() throws Exception{
SolrQuery solrQuery = new SolrQuery("title:*");
QueryResponse query = server.query(solrQuery);
SolrDocumentList results = query.getResults();
for (SolrDocument solrDocument : results) {
System.out.println(solrDocument.getFieldValue("name")+"....."+solrDocument.getFieldValue("title"));
}
}
/**
* 使用bean来直接获取结果
* @throws Exception
*/
@Test
public void selectIndexs() throws Exception{
SolrQuery query = new SolrQuery("title:*");
query.setRows(10);
query.setFields("name" , "title");
QueryResponse query2 = server.query(query);
List<Item> beans = query2.getBeans(Item.class);
for (Item item : beans) {
System.out.println(item.getName()+"......."+item.getTitle());
}
}
怎么样同时查title和content ?
a)采用 <copyField> ,将title和content设置到text的field中
b)根据服务器 solrconfig.xml 配置 df 默认查询字段进行查询
<requestHandler name="/select" class="solr.SearchHandler"> <lst name="defaults"> <str name="echoParams">explicit</str> <int name="rows">10</int> <str name="df">text</str> </lst> </requestHandler> |
注:不推荐用name来设置进copyfield中,查询的结果会空
/**
* 高亮显示
* @throws Exception
*/
@Test
public void getHightlighter() throws Exception{
SolrQuery query = new SolrQuery("title:手机");
//设置高亮相关配置
query.setHighlight(true); //开启高亮
query.addHighlightField("title"); //设置须要高亮的字段,可设置多个
query.addHighlightField("content"); //设置须要高亮的字段,可设置多个
query.addHighlightField("name"); //设置须要高亮的字段,可设置多个
query.setHighlightFragsize(100); //设置摘要的长度
query.setHighlightSimplePre("<font color='red'>"); //设置高亮前缀
query.setHighlightSimplePost("</font>"); //设置高亮后缀
//------------
QueryResponse response = server.query(query); //查询结果
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting(); //高亮内容集合
System.out.println(highlighting);
Set<String> keySet = highlighting.keySet(); //获取的是主键的set集合
//遍历获取全部的高亮内容
for (String string : keySet) {
Map<String, List<String>> map = highlighting.get(string);
Set<String> fieldKeys = map.keySet();
for (String string2 : fieldKeys) {
List<String> list = map.get(string2);
for (String string3 : list) {
System.out.println(string3);
}
}
}
}
结果:
{3={name=[华为<font color='red'>手机</font>], title=[华为<font color='red'>手机</font>低价处理], content=[华为<font color='red'>手机</font>是中国知名品牌的<font color='red'>手机</font>产品]}, 6={name=[小米<font color='red'>手机</font>], title=[小米<font color='red'>手机</font>热销中], content=[小米<font color='red'>手机</font>是<font color='red'>手机</font>界的新起之秀]}}
华为<font color='red'>手机</font>
华为<font color='red'>手机</font>低价处理
华为<font color='red'>手机</font>是中国知名品牌的<font color='red'>手机</font>产品
小米<font color='red'>手机</font>
小米<font color='red'>手机</font>热销中
小米<font color='red'>手机</font>是<font color='red'>手机</font>界的新起之秀