zoukankan      html  css  js  c++  java
  • 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)serversolr创建文件夹movies
    2)把solor的serversolrconfigsetsasic_configs下的conf拷贝到serversolrmovies下
    创建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()为查询结果总条数;

     代码演示

    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) {
    				
    			}
    		}
    	}
    		
    }
    

      

  • 相关阅读:
    Javascript 智能输入数字且保留小数点后三位
    dedecms 在模版页面获取当前栏目id
    photoshop打开图片显示的是索引,无法编辑解决
    Mac+Apache+PHP 安装 Xdebug 方法
    dedecms 模版里格式化时间标签
    input中只能写入数字int、float
    dedecmsv5.7 前台模版里输出变量
    Dedecms V5.7 关于session
    JQuery 获取select被选中的value和text
    如何使用Anaconda
  • 原文地址:https://www.cnblogs.com/cnki/p/5204842.html
Copyright © 2011-2022 走看看