实现一个core里面多个字段的关联查询:
应用场景是: 词, 句子,文章
希望通过查询实现词,句子,文章里面共同有的关键字
private static CloudSolrServer cloudSolrServer; private static String zkHost = "192.168.0.157:2181"; private static CloudSolrServer solrServer = new CloudSolrServer(zkHost); private static synchronized CloudSolrServer getCloudSolrServer( final String zkHost) { if (cloudSolrServer == null) { try { cloudSolrServer = new CloudSolrServer(zkHost); } catch (Exception e) { e.printStackTrace(); } } return cloudSolrServer; } public static Map<String, Object> solrCloudR(String skey, int start, int rows) throws SolrServerException{ Map<String, Object> mapResults = new HashMap<String, Object>(); solrServer.setDefaultCollection("collection1"); SolrQuery solrQuery = new SolrQuery(); solrQuery.set("q","word:"+skey+" OR nickname:"+skey+" OR content:"+skey); solrQuery.setHighlight(true); // 开启高亮组件 solrQuery.addHighlightField(skey);// 高亮字段 solrQuery.setHighlightSimplePre("<font color=‘red‘>");//标记,高亮关键字前缀 solrQuery.setHighlightSimplePost("</font>");//后缀 solrQuery.set("start", start); solrQuery.set("rows", rows); QueryResponse response = solrServer.query(solrQuery); SolrDocumentList results = response.getResults(); long numFound = response.getResults().getNumFound(); mapResults.put("results:", results); System.out.println("numFound=" +numFound); for (int i = 0; i < results.size(); ++i) { System.out.println(results.get(i)); } return mapResults; } public static void main(String[] args) throws Exception { String skey = "厦门"; //solrCloudR(skey); //solrCloudR(skey); //solrSearch(skey, 1, 20); solrCloudR(skey, 0, 1000); }
查询结果:
numFound=5 SolrDocument{createtime=Mon Aug 24 14:58:59 PDT 2015, id=502, articlenum=0, sex=true, username=ad, updatetime=Mon Aug 24 16:19:31 PDT 2015, nickname=厦门, wordnum=0, sentencenum=0, money=0.0, _version_=1510384955694252032} SolrDocument{createtime=Fri Aug 21 11:38:48 PDT 2015, id=132, articlenum=0, username=15622238554, updatetime=Mon Aug 24 18:47:56 PDT 2015, nickname=厦门厦门厦门, wordnum=0, sentencenum=0, money=0.0, password=96E79218965EB72C92A549DD5A330112, _version_=1510384955754020864} SolrDocument{uid=56, feel=1, updatetime=Mon Aug 24 11:15:58 PDT 2015, state=1, createtime=Mon Aug 24 11:16:00 PDT 2015, id=5, content=[听说台风都不来厦门的], articlenum=112, forwardnum=1, supportnum=1, word=厦门厦门, _version_=1510384955107049472} SolrDocument{uid=12, feel=1, updatetime=Mon Aug 24 15:04:20 PDT 2015, state=1, createtime=Mon Aug 24 15:04:24 PDT 2015, id=501, content=[听说厦门又下雨], articlenum=12, forwardnum=1, supportnum=1, word=厦门又下雨, _version_=1510384955013726208} SolrDocument{uid=12, feel=1, updatetime=Mon Aug 24 15:04:20 PDT 2015, state=1, createtime=Mon Aug 24 15:04:24 PDT 2015, id=500, content=[最近天气很不稳定], articlenum=12, forwardnum=1, supportnum=1, word=厦门高温, _version_=1510384954935083008}
时间: 2024-10-13 15:29:03