当前位置: 首页 > solr, 搜索 > 正文

Solr4开发Demo

1 星2 星3 星4 星5 星 (1 次投票, 评分: 5.00, 总分: 5)
Loading ... Loading ...
baidu_share

下来我们来看看solr4集群在实际开发中的应用。关于solr4集群搭建,大家可以参考以前的文章:
solrcloud在tomcat下安装(一)
solrcloud在tomcat下安装(二)
solrcloud在tomcat下安装(三)
关于solr4中使用ik分词请参考:
solr4的solr-ik插件
测试ik分词是否正确安装,在浏览器输入以下地址:

http://localhost:8080/solr/#/collection1/analysis

操作实例如下图:
solr-analysis

schema.xml配置的关键部分

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 
<field name="title" type="ik_text" indexed="true" stored="true" multiValued="false" termVectors="true" termPositions="true" termOffsets="true" default="" omitNorms="true"/>
 <field name="content" type="ik_text" indexed="true" stored="true" multiValued="false" termVectors="true" termPositions="true" termOffsets="true" default=""/>
 
<fieldType name="ik_text" class="solr.TextField"> 
       <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/> 
</fieldType>

我们搭建了solr集群,当然的得用CloudSolrServer来实现索引添加删除,删除,查询。
1.索引添加

String zkHost = "localhost:9080;localhost:9081;localhost:9082"; 
String defaultCollection = "collection1";  
CloudSolrServer server = new CloudSolrServer(zkHost);  
server.setDefaultCollection(defaultCollection); 
SolrInputDocument doc = new SolrInputDocument();  
doc.addField("id", 1);  
doc.addField("titile", "中国");
doc.addField("content", "中国人民是好样的");
doc.addField("id", 2);  
doc.addField("titile", "爱情来了");
doc.addField("content", "爱就爱了,快乐恋爱");
server.add(doc);  
server.commit();

你也可以使用server.addBeans()方法.

  /**
   * Adds a collection of beans
   * @param beans  the collection of beans
   * @throws IOException If there is a low-level I/O error.
   */
  public UpdateResponse addBeans(Collection<?> beans ) throws SolrServerException, IOException {
    return addBeans(beans, -1);
  }

实例如下:
News类

public class News{
@Field
private String id;
@Field
private String title;
@Field
private String content;
 
//get set 省略
}

添加实例:

List<News> list=new ArrayList<News>();
News news=new News();
news.setId(1);
news.setTitle("中国");
news.setContent("中国人民是好样的");
list.add(news);
news.setId(2);
news.setTitle("爱情来了");
news.setContent("爱就爱了,快乐恋爱");
list.add(news);
server.addBeans(list);
server.commit();

2.solr查询

String zkHost = "localhost:9080;localhost:9081;localhost:9082"; 
String defaultCollection = "collection1"; 		
CloudSolrServer server = new CloudSolrServer(zkHost);  
server.setDefaultCollection(defaultCollection);
SolrQuery params = new SolrQuery();  
        String q="中国人民";
        //the common parameters for all search  
        params.set("q", "content:"+q,"title:"+q);  
        //params.setFields("content","title");
        params.set("fl", "*,score");  // field list  
        params.set("start", "0");  
        params.set("rows", "10");
        params.setSort("score", ORDER.desc);
        params.setSort("updatetime", ORDER.desc);
        params.set("timeAllowed", "30000"); //miliseconds  
        params.set("wt", "json"); // the response writer type
        QueryResponse response = null;  
        try {  
            response = server.query(params);  
            SolrDocumentList results = response.getResults();
            for (int i = 0; i < results.size(); ++i) {
                 SolrDocument document=results.get(i);
    		 System.out.println(document.get("id").toString());
    		 System.out.println(document.get("title").toString());
    		 System.out.println(document.get("content").toString());
            }
        }catch (SolrServerException e) {  
            System.err.println(e.getMessage());  
            e.printStackTrace();  
        } catch (Exception e) {  
            System.err.println(e.getMessage());  
            e.printStackTrace();  
        } finally {  
            server.shutdown();  
        }

索引删除
索引删除很简单,主要有如下方法,不在赘述
solr-delete-method

本文固定链接: http://www.chepoo.com/solr-4-development-demo.html | IT技术精华网

Solr4开发Demo:等您坐沙发呢!

发表评论