Solr之java操作

参考教程:

http://www.cnblogs.com/xia520pi/p/3625232.html

http://www.cnblogs.com/hujunzheng/p/5647896.html

sorl不提供更新,所有的更新都是先删除后插入

https://www.cnblogs.com/xuyiqing/p/8707966.html

solr的安装

如果没配置环境变量JAVA_HOME和PATH,则先配置,指向JDK1.8环境;解压solr;命令行进入solr的bin目录,执行solr.cmd start,命令窗口不要关;浏览器打开http://127.0.0.1:8983/。这是以集成的jetty服务器方式去运行。还可以部署到tomcat上。
2、创建电驴数据的core
1)server\solr\创建文件夹movies
2)把solor的server\solr\configsets\basic_configs下的conf拷贝到server\solr\movies下
创建core(名字movies),相当于表:
3)浏览器中打开solr控制台“Core Admin”→【Add core】,name和instanceDir都填movies,其他保持默认值。
4)创建字段:打开movies这个core,Add Field,增加相应的字段(id是内置的不用建,string类型): title、 ed2k、 content。stored代表“保存原始数据”(后续搜索的时候可以读取出来)、indexed代表“进行索引保存”(可以根据这个字段进行搜索)。

搜索引擎:插入

1、solrj.jar(在solr的dist下)是solr提供的用来连接solr执行数据插入、查询的开发包。 solrj.jar依赖于httpcore-4.4.4、httpclient-4.5.2、、httpmime-4.5.2、commons-io,位于solrj-lib中
2、创建连接:
HttpSolrClient.Builder builder = new HttpSolrClient.Builder ("http://127.0.0.1:8983/solr/movies");
HttpSolrClient solr = builder.build();
一个文档(相当于数据库的行)对应一个SolrInputDocument 对象,
3、调用SolrInputDocument的setField(“title”, title);来设置字段的值;调用HttpSolrClient 的add方法将SolrInputDocument 加入solr服务器;
4、调用HttpSolrClient 的close方法关闭连接;
5、删除文档:调用HttpSolrClient 的deleteById:根据Id删除; deleteByQuery()根据查询条件删除;

搜索引擎:搜索

1、 SolrQuery query = new SolrQuery();是查询条件
2、 SolrQuery query = new SolrQuery();
query.setQuery(“description:\”王宝强\””);// description字段中包含”王宝强”的
QueryResponse resp = solr.query(query);
SolrDocumentList list = resp.getResults();
3、查询语法,支持AND、OR、NOT(必须是大写的),支持()运算符。
1)、title:杨中科 是只要title中有“杨中科”任何一个的都匹配,如果想完全匹配的就用 title:"杨中科"
2)、范围比较。age在3到5之间的: Age:[3 TO 5]。age大于5的 Age:[5 TO *]
4、排序:
solrQuery.setSort("area", ORDER.desc);
5、分页查询:
solrQuery.setStart(起始行数 0开始);//limit 5,10
solrQuery.setRows(取的条数);
QueryResponse的getResults()为当前页查询的数据;
SolrDocumentList的getNumFound()为查询结果总条数;

 代码演示

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.mf.solrProject;
 
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import javax.xml.transform.SourceLocator;
 
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
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;
 
public class Main1 {
     
    public static void main4(String[] args) throws SolrServerException, IOException {
        HttpSolrClient.Builder builder =
                new HttpSolrClient.Builder("http://127.0.0.1:8983/solr/movies");
        HttpSolrClient solrClient = builder.build();
         
        SolrQuery query = new SolrQuery("content:\"马蓉\" OR title:\"马蓉\"");
        //query.setSort("age", ORDER.desc);
        query.setStart(0);
        query.setRows(10);
        QueryResponse resp = solrClient.query(query);
        SolrDocumentList docList = resp.getResults();
        /*
        docList.getNumFound()//总的查询结果条数
        docList.size();//当前页的条数*/
         
        for(SolrDocument doc : docList)
        {
            String id = (String)doc.get("id");
            String content = (String)doc.get("content");
            String title = (String)doc.get("title");
            String ed2k = (String)doc.get("ed2k");
            System.out.println(title);
        }
        solrClient.close();
    }
     
    public static void main(String[] args) throws ClassNotFoundException, SQLException, SolrServerException, IOException {
        HttpSolrClient.Builder builder =
                new HttpSolrClient.Builder("http://127.0.0.1:8983/solr/movies");
        HttpSolrClient solrClient = builder.build();
         
        Class.forName("org.sqlite.JDBC");
        Connection sqliteConn =
                DriverManager.getConnection("jdbc:sqlite:E:/专业课/自己动手写搜索引擎/verycd.sqlite3.db");
        PreparedStatement ps = sqliteConn.prepareStatement("select * from verycd");
        ResultSet rs =  ps.executeQuery();
        int i=0;
        while(rs.next())
        {
            int id =  rs.getInt("verycdid");
            String title =  rs.getString("title");
            String ed2k =  rs.getString("ed2k");
            String content =  rs.getString("content");
            //System.out.println(title);
            //System.out.println("id="+id);
            System.out.println(i++);
             
            SolrInputDocument doc = new SolrInputDocument();
            doc.setField("id", id);
            doc.setField("title", title);
            doc.setField("ed2k", ed2k);
            doc.setField("content",content);
            solrClient.add(doc);//insert
        }
        sqliteConn.close();//
         
         
        solrClient.commit();
        solrClient.close();
    }
 
    public static void main1(String[] args) {
        HttpSolrClient.Builder builder =
                new HttpSolrClient.Builder("http://127.0.0.1:8983/solr/movies");
        HttpSolrClient solrClient = builder.build();
        try
        {
            SolrInputDocument doc = new SolrInputDocument();
            doc.setField("id", "1");
            doc.setField("title", "泰坦尼克号");
            doc.setField("ed2k", "ed2:///aaaaaaaaaa.avi/fadfsafadsfasfdadsf");
            doc.setField("content", "《泰坦尼克号》是美国20世纪福克斯公司和派拉蒙影业公司共同出资,于1994年拍摄的一部浪漫的爱情灾难电影,由詹姆斯·卡梅隆创作、编辑、制作、导演及监制,莱昂纳多·迪卡普里奥、凯特·温斯莱特主演。影片于1997年11月1日在东京首映。");
            solrClient.add(doc);//insert
            solrClient.commit();
            //solrClient.deleteByQuery("content:\"爱情\"");
        } catch (SolrServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            try {
                solrClient.close();
            } catch (IOException e) {
                 
            }
        }
    }
         
}

  

posted @   ~沐风  阅读(594)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示