Search in sources :

Example 6 with HttpSolrServer

use of org.apache.solr.client.solrj.impl.HttpSolrServer in project e3mall by colg-cloud.

the class TestSolr method queryIndex.

/**
 * 查询索引库 - 简单
 */
@Test
public void queryIndex() throws SolrServerException {
    // 创建一个SolrServer对象
    SolrServer solrServer = new HttpSolrServer(BASE_URL);
    // 创建一个SolrQuery对象
    SolrQuery query = new SolrQuery();
    // 设置查询条件
    query.setQuery("*:*");
    // 执行查询QueryResponse对象
    QueryResponse queryResponse = solrServer.query(query);
    // 取文档列表,取查询结果的总记录数
    SolrDocumentList solrDocumentList = queryResponse.getResults();
    System.out.println("查询结果总记录数:" + solrDocumentList.getNumFound());
    System.out.println("当前记录数:" + solrDocumentList.size());
    // 遍历文档列表,取域的内容
    /*
		for (SolrDocument doc : solrDocumentList) {
			doc.get("id");
			doc.get("item_title");
			doc.get("item_title");
			doc.get("item_sell_point");
			doc.get("item_price");
			doc.get("item_image");
			doc.get("item_category_name");
		}
		*/
    System.out.println(JSON.toJSONString(solrDocumentList));
}
Also used : QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) HttpSolrServer(org.apache.solr.client.solrj.impl.HttpSolrServer) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrServer(org.apache.solr.client.solrj.SolrServer) HttpSolrServer(org.apache.solr.client.solrj.impl.HttpSolrServer) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 7 with HttpSolrServer

use of org.apache.solr.client.solrj.impl.HttpSolrServer in project e3mall by colg-cloud.

the class TestSolr method deleteDocument.

/**
 * 删除文档
 */
@Test
public void deleteDocument() throws SolrServerException, IOException {
    SolrServer solrServer = new HttpSolrServer(BASE_URL);
    // 根据id删除
    // solrServer.deleteById("doc01");
    // 根据查询删除
    solrServer.deleteByQuery("id:doc01");
    solrServer.commit();
}
Also used : HttpSolrServer(org.apache.solr.client.solrj.impl.HttpSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) HttpSolrServer(org.apache.solr.client.solrj.impl.HttpSolrServer) Test(org.junit.Test)

Example 8 with HttpSolrServer

use of org.apache.solr.client.solrj.impl.HttpSolrServer in project e3mall by colg-cloud.

the class TestSolr method queryIndexFuza.

/**
 * 查询索引库 - 复杂
 * @throws SolrServerException
 */
@Test
public void queryIndexFuza() throws SolrServerException {
    SolrServer solrServer = new HttpSolrServer(BASE_URL);
    SolrQuery query = new SolrQuery();
    // 查询条件
    query.setStart(0).setRows(10).setQuery("手机");
    // 默认搜索域
    query.set("df", "item_title");
    // 高亮显示
    query.setHighlight(true);
    // 高亮显示字段
    query.addHighlightField("item_title");
    // 高亮显示标签
    query.setHighlightSimplePre("<em>").setHighlightSimplePost("</em>");
    // 执行查询
    QueryResponse queryResponse = solrServer.query(query);
    SolrDocumentList solrDocumentList = queryResponse.getResults();
    System.out.println(JSON.toJSONString(solrDocumentList));
    // 高亮结果
    Map<String, Map<String, List<String>>> map = queryResponse.getHighlighting();
    System.out.println(JSON.toJSON(map));
}
Also used : QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) HttpSolrServer(org.apache.solr.client.solrj.impl.HttpSolrServer) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrServer(org.apache.solr.client.solrj.SolrServer) HttpSolrServer(org.apache.solr.client.solrj.impl.HttpSolrServer) Map(java.util.Map) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Aggregations

HttpSolrServer (org.apache.solr.client.solrj.impl.HttpSolrServer)8 SolrServer (org.apache.solr.client.solrj.SolrServer)4 Test (org.junit.Test)4 SolrQuery (org.apache.solr.client.solrj.SolrQuery)2 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)2 SolrDocumentList (org.apache.solr.common.SolrDocumentList)2 Map (java.util.Map)1 PostConstruct (javax.annotation.PostConstruct)1 Activate (org.apache.felix.scr.annotations.Activate)1 EmbeddedSolrServer (org.apache.solr.client.solrj.embedded.EmbeddedSolrServer)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1 CoreContainer (org.apache.solr.core.CoreContainer)1 IndexReference (org.apache.stanbol.commons.solr.IndexReference)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1