设为首页 收藏本站
查看: 1943|回复: 0

[经验分享] 全文检索技术

[复制链接]

尚未签到

发表于 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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-425475-1-1.html 上篇帖子: Solr初尝试 下篇帖子: solr 管理页面详解
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表