solr简单使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class SolrjServiceTest {
 
    private SolrjService solrjService;
 
  @Before
    public void setUp() throws Exception {
        // 在url中指定core名称:jt
        String url = "http://solr.jt.com/jt";
        HttpSolrServer httpSolrServer = new HttpSolrServer(url); //定义solr的server
        httpSolrServer.setParser(new XMLResponseParser()); // 设置响应解析器
        httpSolrServer.setMaxRetries(1); // 设置重试次数,推荐设置为1
        httpSolrServer.setConnectionTimeout(500); // 建立连接的最长时间
 
        this.httpSolrServer = httpSolrServer;
        solrjService = new SolrjService(httpSolrServer);
    }
    新增
运行测试方法cn.jt.solrj.service.SolrjServiceTest. testAdd()
    @Test
    public void testAdd() throws Exception {
        Foo foo = new Foo();
        foo.setId(1425955126820L);
        foo.setTitle("new - 轻量级Java EE企业应用实战(第3版):Struts2+Spring3+Hibernate整合开发(附CD光盘)");
 
        this.solrjService.add(foo);
    }
执行成功后,在solr中可以查询到add的数据。
 
    删除
按id删除。
    @Test
    public void testDelete() throws Exception {
        this.solrjService.delete(Arrays.asList("1433151614240"));
    }
    修改
    按id值,有值就修改,没值就新增。
    @Test
    public void testAdd() throws Exception {
        Foo foo = new Foo();
        foo.setId(1425955126820L);
        foo.setTitle("new - 轻量级Java EE企业应用实战(第3版):Struts2+Spring3+Hibernate整合开发(附CD光盘)");
 
        this.solrjService.add(foo);
    }
    查询
    @Test
    public void testSearch() throws Exception {
        List<Foo> foos = this.solrjService.search("java", 1, 10);
        for (Foo foo : foos) {
            System.out.println(foo);
    

  

 

posted @   爱吃醋的工程师  阅读(117)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示