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>界的新起之秀