Win下solr5.3使用SolrJ进行CRUD操作

前一篇: Win下Solr5.3从数据库中读取索引

C : 创建索引
R : 删除索引
U : 更新索引
D : 删除索引

本篇文章是另一种创建操作索引的方法,请勿在第二篇文章的基础上修改。

  • 新建一个Java工程,将以下三个目录下的jar包全部导入到工程中(可能有没用上的jar包)
    path\solr-5.3.0\dist\*.jar
    path\solr-5.3.0\dist\solrj-lib\*.jar
    path\solr-5.3.0\server\lib\ext\*.jar
  • 参照上一篇文章,修改schema.xml文件,将相应的字段名、类型修改为你自己想要的类型。本文就以上一篇文章的例子为例。
  • 创建一个实体类,其中的字段和 schema.xml中的字段相对应,如下:
import java.util.Date;

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

public class Goods {

    @Field("id")    // 注入属性
    private int id;
    @Field("name")
    private String name;
    @Field("number")
    private int number;
    @Field("updateTime")
    private Date updateTime;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

}
  • 具体的操作代码如下,仅供参考(举一反三吧)。代码中可能有小错误,多多包涵。
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.SolrPingResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.MapSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.junit.Test;

import com.swh.bean.Goods;

public class SolrJ {
    String url = "http://localhost:8983/solr/s2";

    // 测试连通性
    @Test
    public void createSolrClient() {
        try {
            SolrClient solr = new HttpSolrClient(url);
            SolrPingResponse response = solr.ping();
            // 打印执行时间
            System.out.println(response.getElapsedTime());
            solr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** * 增加索引 * * @throws Exception */
    @Test
    public void addIndex() throws Exception {
        SolrClient solr = new HttpSolrClient(url);

        // 添加方式1
        SolrInputDocument docs = new SolrInputDocument();
        docs.addField("id", "3");
        docs.addField("name", "aaa");
        docs.addField("number", "aaa");
        docs.addField("updateTime", "2016-7-19");
        UpdateResponse response = solr.add(docs);
        System.out.println(response.getElapsedTime());

        // 添加方式2
        Goods goods = new Goods();
        goods.setId(4);
        goods.setName("bbb");
        goods.setNumber(22);
        goods.setUpdateTime(new Date());
        response = solr.addBean(goods);
        System.out.println(response.getElapsedTime());

        solr.commit();
        solr.close();
    }

    /** * 批量添加索引 * * @throws Exception */
    @Test
    public void addIndexSum() throws Exception {
        SolrClient solr = new HttpSolrClient(url);

        List list = new ArrayList();
        Goods goods = null;
        for (int i = 0; i < 25; i++) {
            goods = new Goods();
            goods.setId(i + 20);
            goods.setName("hahahaha");
            list.add(goods);
        }

        UpdateResponse response = solr.addBeans(list);
        System.out.println(response.getElapsedTime());

        solr.commit();
        solr.close();
    }

    /** * 删除索引 */
    @Test
    public void delIndex() throws Exception {
        SolrClient solr = new HttpSolrClient(url);
        UpdateResponse response = solr.deleteById("22");
        System.out.println(response.getElapsedTime());
        solr.commit();
        solr.close();
    }

    /** * 简单查询 * * @throws Exception */
    @Test
    public void query() throws Exception {
        SolrClient solr = new HttpSolrClient(url);
        Map map = new HashMap();
        map.put("q", "*:*");
        SolrParams params = new MapSolrParams(map);
        QueryResponse resp = solr.query(params);
        // 以下是第二种方法
        // String queryString="content:test";
        // MultiMapSolrParams mParams =
        // SolrRequestParsers.parseQueryString("queryString");
        // QueryResponse resp = solr.query(mParams);
        SolrDocumentList docsList = resp.getResults();
        System.out.println(docsList.size());
        System.out.println("---------------------");
        for (SolrDocument doc : docsList) {
            System.out.println(doc.get("id"));
        }
        solr.close();
    }

    @Test
    public void find() throws SolrServerException, IOException {
        SolrClient solr = new HttpSolrClient(url);

        SolrQuery solrQuery = new SolrQuery();
        // solrQuery.setQuery("*:*");
        solrQuery.set("q", "*:*");
        // 设置分页的相关属性
        solrQuery.setStart(3);
        solrQuery.setRows(5);

        QueryResponse queryResponse = solr.query(solrQuery);
        SolrDocumentList list = queryResponse.getResults();

        for (SolrDocument document : list) {
            Object id = document.get("id");
            Object number = document.get("number");
            System.out.println(id + "\t" + number);
        }
    }

}

你可能感兴趣的:(solr,索引,Solr)