首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

solr(三)solrj - how to add documents to solr index

2012-07-29 
solr(3)solrj - how to add documents to solr indexsolr(3)solrj - how to add documents to solr index1

solr(3)solrj - how to add documents to solr index
solr(3)solrj - how to add documents to solr index

1. Use Solrj to add documen to index
All the configuration of fields in under the example\solr\conf directory schema.xml file. For example:
<field name="id" type="string" indexed="true" stored="true" required="true" />
<field name="sku" type="text_en_splitting_tight" indexed="true" stored="true" omitNorms="true"/>
<field name="name" type="text_general" indexed="true" stored="true"/>
<field name="alphaNameSort" type="alphaOnlySort" indexed="true" stored="false"/>
<field name="manu" type="text_general" indexed="true" stored="true" omitNorms="true"/>
<field name="cat" type="string" indexed="true" stored="true" multiValued="true"/>

@Test
public void addDoc() {
    //创建doc文档
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", 1);
    doc.addField("name", "Solr Input Document");
    doc.addField("manu", "this is SolrInputDocument content");
   
    try {
        //添加一个doc文档
        UpdateResponse response = embeddedSolrServer.add(doc);
        System.out.println(embeddedSolrServer.commit());//commit后才保存到索引库
        System.out.println(response);
        System.out.println("querying time:" + response.getQTime());
        System.out.println("Elapsed Time:" + response.getElapsedTime());
        System.out.println("status:" + response.getStatus());
    } catch (SolrServerException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    query("name:solr");
}


private void query(String query) {
SolrParams params = new SolrQuery(query);

try {
QueryResponse response = embeddedSolrServer.query(params);

SolrDocumentList list = response.getResults();
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}

2. Use solrj to add multiple documents
@Test
public void addDocs() {
    Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
   
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", 2);
    doc.addField("name", "Solr Input Documents 1");
    doc.addField("manu", "this is SolrInputDocuments 1 content");
   
    docs.add(doc);
   
    doc = new SolrInputDocument();
    doc.addField("id", 3);
    doc.addField("name", "Solr Input Documents 2");
    doc.addField("manu", "this is SolrInputDocuments 3 content");
   
    docs.add(doc);
   
    try {
        //add documents
        UpdateResponse response = embeddedSolrServer.add(docs);
        //commit后才保存到索引库
        System.out.println(embeddedSolrServer.commit());
        System.out.println(response);
    } catch (SolrServerException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    query("solr");
}

There is a parameter collection in API.

3. Use JavaEntity Bean as Parameter to add document
package com.sillycat.easyhunter.model;

import org.apache.solr.client.solrj.beans.Field;

public class Product {

@Field
private String id;

@Field
private String name;

@Field
private String manu;

@Field
private String[] cat;

@Field
private String[] features;

@Field
private float price;

@Field
private int popularity;

@Field
private boolean inStock;
...snip...
getter and setter
...snip...

@Test
public void addBean() {
    //Product需要添加相关的Annotation注解,便于告诉solr哪些属性参与到index中
    Product p4 = new Product();
    p4.setId("4");
    p4.setName("add bean index");
    p4.setManu("index bean manu");
    p4.setCat(new String[] { "category1", "catagory2" });
   
    try {
        //添加Index Bean到索引库
        UpdateResponse response = this.embeddedSolrServer.addBean(p4);
        System.out.println(this.embeddedSolrServer.commit());//commit后才保存到索引库
        System.out.println(response);
    } catch (SolrServerException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    query("name:add bean index");
}

4. Add bean collection to our solr server
@Test
public void addBeans() {
    Product p6 = new Product();
    p6.setId("6");
    p6.setName("add beans index 6");
    p6.setManu("index beans manu 6");
    p6.setCat(new String[] { "a", "b" });
   
    List<Product> products = new ArrayList<Product>();
    products.add(p6);
   
    Product p5 = new Product();
    p5.setId("5");
    p5.setName("add beans index 5");
    p5.setManu("index beans manu 5");
    p5.setCat(new String[] { "aaa", "bbbb" });
    products.add(p5);
    try {
        //添加索引库
        UpdateResponse response = this.embeddedSolrServer.addBeans(products);
        System.out.println(this.embeddedSolrServer.commit());//commit后才保存到索引库
        System.out.println(response);
    } catch (SolrServerException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    query("name:add beans index");
}

references:
http://www.blogjava.net/hoojo/archive/2011/10/21/361747.html
http://wiki.apache.org/solr/Solrj


热点排行