package com.jy.util; /** 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 java.io.IOException; import java.util.List; import java.util.Map; */ public class SolrTest { /** solrj 查询示例 */ public void add() throws IOException, SolrServerException { final String solrUrl = ""; HttpSolrClient client = new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(60000).build(); SolrInputDocument document = new SolrInputDocument(); document.addField("id", "c0002"); document.addField("title_ik", "使用solrJ添加的文档"); document.addField("content_ik", "solrj文档的内容"); document.addField("product_name", "solrJ商品名称"); client.add(document); client.commit(); } public void search() throws IOException, SolrServerException { final String solrUrl = ""; HttpSolrClient client = new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(60000).build(); SolrQuery solrQuery = new SolrQuery(); solrQuery.set("q","id:id001"); QueryResponse response = client.query(solrQuery); SolrDocumentList results = response.getResults(); for(SolrDocument document:results){ String id = document.get("id").toString(); String productName = document.get("product_name").toString(); System.out.println(id); System.out.println(productName); } } public void search2() throws IOException, SolrServerException { final String solrUrl = ""; HttpSolrClient client = new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(60000).build(); SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery("精致真皮手表"); solrQuery.setFilterQueries("product_name:手表"); solrQuery.setSort("id",SolrQuery.ORDER.desc); solrQuery.setStart(0); solrQuery.setRows(30); // 设置显得的域的列表 solrQuery.setFields("id", "title_ik", "content_ik","product_name"); // 设置默认搜索域 solrQuery.set("df", "product_name"); // 设置高亮 solrQuery.setHighlight(true); solrQuery.addHighlightField("product_name"); solrQuery.setHighlightSimplePre("<em>"); solrQuery.setHighlightSimplePost("</em>"); QueryResponse response = client.query(solrQuery); // 查询结果 SolrDocumentList results = response.getResults(); // 查询结果总数 long totalCount = results.getNumFound(); System.out.println("查询结果总数:" + totalCount); Map<String, Map<String, List<String>>> maps = response.getHighlighting(); SolrDocumentList documentList = response.getResults(); //7、遍历查询结果 for (SolrDocument solrDocument : documentList) { System.out.print(solrDocument.get("id")+" "); List<String> titleList = maps.get(solrDocument.get("id")).get("item_title"); if (titleList !=null && titleList.size()>0) { System.out.print(titleList.get(0)); } System.out.println(); } } public void delete() throws IOException, SolrServerException { final String solrUrl = ""; HttpSolrClient client = new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(60000).build(); client.deleteById("id001"); client.commit(); } }
solr查询 操作
猜你喜欢
转载自www.cnblogs.com/northern-light/p/10497502.html
今日推荐
周排行