ddsdjn 发表于 2017-12-18 18:57:08

全文检索技术

1 package cn.xjy.test ;  

2  
3 import java.io.IOException ;
  
4 import java.util.List ;
  
5 import java.util.Map ;
  
6 import org.apache.solr.client.solrj.SolrQuery ;
  
7 import org.apache.solr.client.solrj.SolrQuery.ORDER ;
  
8 import org.apache.solr.client.solrj.SolrServer ;
  
9 import org.apache.solr.client.solrj.impl.HttpSolrServer ;
  
10 import org.apache.solr.client.solrj.response.QueryResponse ;
  
11 import org.apache.solr.common.SolrDocument ;
  
12 import org.apache.solr.common.SolrDocumentList ;
  
13 import org.apache.solr.common.SolrInputDocument ;
  
14 import org.junit.Before ;
  
15 import org.junit.Test ;
  
16

  
17 public>  
18   
  
19   SolrServer    solrServer    = null ;
  
20   
  
21   @Before
  
22   public void init() {
  
23         // 1、 创建HttpSolrServer对象,通过它和Solr服务器建立连接。
  
24         // 参数:solr服务器的访问地址
  
25         solrServer = new HttpSolrServer("http://localhost:8080/solr/collection2") ;
  
26   }
  
27   
  
28   /**
  
29      * 更新或添加
  
30      * 存在就更新,不存在就添加(根据id判断)
  
31      * @throws Exception
  
32      * @throws IOException
  
33      */
  
34   @Test
  
35   public void testAddandUpdate() throws Exception, IOException {
  
36         
  
37         // 2、 创建SolrInputDocument对象,然后通过它来添加域。
  
38         SolrInputDocument doc = new SolrInputDocument() ;
  
39         
  
40         /* 测试添加 */
  
41         /* doc.addField("id","测试1"); doc.addField("product_name","测试name");
  
42          * doc.addField("product_catalog_name","测试product_catalog_name");
  
43          * doc.addField("product_price",22f);
  
44          * doc.addField("product_description","测试product_description");
  
45          * doc.addField("product_picture","测试product_picture"); */
  
46         
  
47         /* 测试更新 */
  
48         
  
49         // 第一个参数:域的名称,域的名称必须是在schema.xml中定义的
  
50         // 第二个参数:域的值
  
51         // 注意:id的域不能少
  
52         doc.addField("id", "测试1") ;
  
53         doc.addField("product_name", "测试更新name") ;
  
54         doc.addField("product_catalog_name", "测试更新product_catalog_name") ;
  
55         doc.addField("product_price", 23f) ;
  
56         doc.addField("product_description", "测试更新product_description") ;
  
57         doc.addField("product_picture", "测试更新product_picture") ;
  
58         
  
59         // 3、 通过HttpSolrServer对象将SolrInputDocument添加到索引库。
  
60         solrServer.add(doc) ;
  
61         
  
62         // 4、 提交。
  
63         solrServer.commit() ;
  
64   }
  
65   
  
66   /**
  
67      * 删除索引
  
68      * @throws Exception
  
69      */
  
70   @Test
  
71   public void testDelete() throws Exception {
  
72         // 根据id删除
  
73         /* solrServer.deleteById("测试1") ; solrServer.commit(); */
  
74         
  
75         // 根据条件删除
  
76         solrServer.deleteByQuery("product_price:23") ;
  
77         
  
78         // 全部删除
  
79         // solrServer.deleteByQuery("*:*");
  
80         solrServer.commit() ;
  
81   }
  
82   
  
83   /**
  
84      * 简单查询
  
85      * @throws Exception
  
86      */
  
87   @Test
  
88   public void testSimpleQuery() throws Exception {
  
89         
  
90         SolrQuery query = new SolrQuery() ;
  
91         // q是固定的且必须 的
  
92         query.set("q", "id:测试1") ;
  
93         query.set("fl", "id,product_catalog_name") ;
  
94         // query.setFields("id", "product_catalog_name");//效果同上
  
95         
  
96         QueryResponse queryResponse = solrServer.query(query) ;
  
97         
  
98         SolrDocumentList results = queryResponse.getResults() ;
  
99         
  
100         System.out.println("总数:" + results.getNumFound()) ;
  
101         
  
102         for (SolrDocument solrDocument : results) {
  
103             // System.out.println(solrDocument.get("id")) ;
  
104             System.out.println(solrDocument) ;
  
105         }
  
106   }
  
107   
  
108   /**
  
109      * 复杂查询
  
110      * @throws Exception
  
111      */
  
112   @Test
  
113   public void testComplexQuery() throws Exception {
  
114         SolrServer solrServer2 = new HttpSolrServer("http://localhost:8080/solr") ;
  
115         
  
116         SolrQuery query = new SolrQuery() ;
  
117         
  
118         // 查询条件
  
119         query.setQuery("product_catalog_name:幽默杂货") ;
  
120         
  
121         // 过滤条件
  
122         query.setFilterQueries("product_catalog_name:幽默杂货") ;
  
123         
  
124         // 按指定字段排序
  
125         query.setSort("id", ORDER.desc) ;
  
126         
  
127         // 分页
  
128         query.setStart(0) ;
  
129         query.setRows(100) ;
  
130         
  
131         // 设置要显示的字段
  
132         // query.setFields("id,product_catalog_name") ;
  
133         
  
134         // 设置默认搜索域
  
135         // query.set("df", "product_name");
  
136         
  
137         // 给特定字段设置样式,要显示样式的字段必须是查询条件中的字段
  
138         query.setHighlight(true) ;
  
139         query.addHighlightField("product_catalog_name") ;
  
140         query.setHighlightSimplePre("<span color='red'>") ;
  
141         query.setHighlightSimplePost("</sapn>") ;
  
142         
  
143         // 查询
  
144         QueryResponse queryResponse = solrServer2.query(query) ;
  
145         
  
146         // 获得结果集
  
147         SolrDocumentList results = queryResponse.getResults() ;
  
148         
  
149         System.out.println("总数:" + results.getNumFound()) ;
  
150         
  
151         // 遍历结果集
  
152         for (SolrDocument solrDocument : results) {
  
153             Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting() ;
  
154             System.out.println(highlighting.get(solrDocument.get("id"))) ;
  
155             System.out.println(solrDocument) ;
  
156         }
  
157   }
  
158 }
页: [1]
查看完整版本: 全文检索技术