Solr6.4.2 API使用

  • maven项目,添加依赖。

  org.apache.solr
  solr-solrj
  6.4.2

  • 编写实体类
package com.jcbase.solr.client.vo;

import java.sql.Timestamp;

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

public class Person{
    @Field("id")
    private String id;
    @Field("money")
    private int number;
    @Field("name")
    private String name;
    @Field("content")
    private String content;
    @Field("updatetime")
    private Timestamp updateTime;


    @Override
    public String toString() {
        return "Person [id=" + id + ", content="+content+", number=" + number + ", name=" + name + ", updateTime=" + updateTime + "]";
    }

    public Person(){}

    public Person(String id, int number, String name, String content, Timestamp updateTime) {
        this.id = id;
        this.number = number;
        this.name = name;
        this.content = content;
        this.updateTime = updateTime;
    }

    public String getId() {
        return id;
    }

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

    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Timestamp getUpdateTime() {
        return updateTime;
    }
    public void setUpdateTime(Timestamp updateTime) {
        this.updateTime = updateTime;
    }
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }


}
  • 操作类
package com.jcbase.solr.client;

import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.Random;

import com.jcbase.solr.client.vo.Person;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrInputDocument;


@SuppressWarnings("deprecation")
public class SolrClient {
    private static HttpSolrClient solrServer;

    public static String[] docs = {"Solr是一个独立的企业级搜索应用服务器",
            "它对外提供类似于Web-service的API接口",
            "用户可以通过http请求",
            "向搜索引擎服务器提交一定格式的XML文件生成索引",
            "也可以通过Http Get操作提出查找请求",
            "并得到XML格式的返回结果"};

    public static String[] name = {"王珣",
            "wangxun",
            "爱谁谁",
            "ass",
            "haoxin",
            "tiantian"};

    static {
        //注意请求地址格式:浏览器中的地址有 ‘#’,需要把‘#’去掉!
        solrServer = new HttpSolrClient("http://localhost:8983/solr/mycollect");
        solrServer.setConnectionTimeout(5000);
    }

    /**
     * 添加单个文档
     */
    public static void insert(Person person) {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", person.getId());
        doc.addField("name", person.getName());
        doc.addField("money", person.getNumber());
        doc.addField("updateTime", person.getUpdateTime());
        doc.addField("content",person.getContent());

        try {
            solrServer.add(doc);
            solrServer.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据文档id删除文档 。
     */
    public static void deleteById(String id) {
        try {
            solrServer.deleteById(id+"");
            solrServer.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除所有文档,为安全起见,使用时再解注函数体 。
     */
    public static void deleteAll() {
        try {
            solrServer.deleteByQuery("*:*");
            solrServer.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新文档,其实也是通过insert操作来完成 。
     *
     */
    public static void update(Person person) {
        insert(person);
    }

    /**
     * 根据文档id查询单个文档 。
     * @return
     */
    public static  T getById(int id, Class clazz) {
        SolrQuery query = new SolrQuery();
        query.setQuery("id:" + id);

        try {
            QueryResponse rsp = solrServer.query(query);
            return rsp.getBeans(clazz).get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param obj 对象索引
     * @param idName 主键名称
     */
    public static void deleteByObject(Object obj, String idName){
        try {
            Class clazz = obj.getClass();
            //将idName的首字母变成大写
            if(Character.isLowerCase(idName.charAt(0))) idName = Character.toUpperCase(idName.charAt(0)) + idName.substring(1);
            Method method = clazz.getMethod("get"+idName);
            String idValue = (String) method.invoke(obj);
            deleteById(idValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        int nameLength = name.length;

        for (int i = 10; i < 100; i++) {
            Random random = new Random();
            StringBuffer nameBuf = new StringBuffer(name[random.nextInt(nameLength)]);
            StringBuffer docBuf = new StringBuffer(docs[random.nextInt(nameLength)]);
            Person person = new Person( String.valueOf(i), random.nextInt(100000), nameBuf.append(String.valueOf(i)).toString(),docBuf.toString(), new Timestamp(System.currentTimeMillis()));
            insert(person);
        }

        //Solr.update(person);
        //System.out.println(Solr.getById(123, Person.class));
        //deleteByObject(person, "id");
    }
}

你可能感兴趣的:(Solr6.4.2 API使用)