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.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class SolrInsertTest {
//solr 服務器地址
public static final String solrServerUrl = "http://localhost:8983/solr";
//solrhome下的core
public static final String solrCroeHome = "mycore";
public static String[] docs = {"Solr是一個獨立的企業級搜索應用服務器",
"它對外提供類似於Web-service的API接口",
"用戶可以通過http請求",
"向搜索引擎服務器提交一定格式的XML文件生成索引",
"也可以通過Http Get操作提出查找請求",
"並得到XML格式的返回結果"};
public static void main(String[] args) {
System.out.println("solr insert test");
// SolrClient client = new HttpSolrClient(solrServerUrl+"/" + solrCroeHome);
SolrClient client = new HttpSolrClient.Builder(solrServerUrl + "/" + solrCroeHome).build();
List<SolrInputDocument> solrDocs = new ArrayList<>();
for (int i = 0; i < docs.length; i++) {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", i);
doc.addField("content_text", docs[i]);
solrDocs.add(doc);
}
try {
client.add(solrDocs);
client.commit();
System.out.println("Insert Done");
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void testSolrQuery() throws IOException, SolrServerException {
SolrClient client = new HttpSolrClient(solrServerUrl + "/" + solrCroeHome);
SolrQuery query = new SolrQuery("content_text:類似");
// 設置查詢關鍵字
/* query.setQuery("Content:* AND Spare3:1 "); */
// 指定查詢返回字段
/* query.setParam("fl", "Content,IndexTime"); */
// 設置高亮
// query.setHighlight(true).setHighlightSimplePre("<span class='red'>")
// .setHighlightSimplePost("</span>");
// query.setParam("hl.fl", "Content");//設置高亮字段
// query.setParam("fl", "ID,Published");
//排除條件 - NOT
//wbQuery.addFilterQuery("OriginType:wb -Spare3:0");
//wbQuery.addFilterQuery("OriginType:wb NOT Spare3:0");
// 時間條件過濾
/* query.addFilterQuery("Content:超哥"); */
/*
* query.addFilterQuery(
* "Published:[1995-12-31T23:59:59.999Z TO 2016-03-06T00:00:00Z]");
**/
// 實現分頁的查詢
query.setStart(0);
query.setRows(10);
// 設定排序,如果需要對field進行排序就必須在schema.xml中對該field配置stored="true"屬性
//set會清空原來的sort條件,add不會清空原來的,會在原來的基礎上添加 sort=Published asc,Author asc(多條件排序)
// query.setSort(IContentCommon.IndexField.Published.getName(),
// SolrQuery.ORDER.asc);
//
// query.addSort(IContentCommon.IndexField.Published.getName(),
// SolrQuery.ORDER.asc);
QueryResponse response = client.query(query);
System.out.println(query);
SolrDocumentList docs = response.getResults();
System.out.println(docs.getNumFound());
for(SolrDocument doc : docs){
Object id = doc.get("id");
System.out.println("id:" + id + ", content:" + doc.get("content_text"));
}
client.close();
}
@Test
public void testSolrDelete() throws IOException, SolrServerException {
SolrClient client = new HttpSolrClient.Builder(solrServerUrl + "/" + solrCroeHome)
.build();
UpdateResponse response = client.deleteById("0");
int status = response.getStatus();
System.out.println("status:" + status);
client.commit();
client.close();
}
}