知识点

相关文章

更多

最近更新

更多

使用solrj 对solr进行增删改查操作

2019-03-05 21:16|来源: 网路

使用java调用solr有两种方式
1、使用httpClient对象直接语句solr对外提供的rest接口,接收到的数据再进行解析。
2、使用solr提供的java客户端api,推荐这种方式


创建索引

第一种添加方式,通过添加Field创建索引文档
使用addField添加索引域,然后通过add(document)添加索引文档
package com._656464.solr.test;
import java.io.IOException;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Before;
import org.junit.Test;
/**
 * 使用solrj 来调用solr 的服务
 */
public class SolrTest {
    SolrServer solr = null;
    @Before
    public void init(){
        String urlString = "http://localhost:8983/solr";
        solr = new HttpSolrServer(urlString);
    }
    @Test
    public void addIndex() throws SolrServerException, IOException{
        SolrInputDocument document=new SolrInputDocument();
        //第一种添加方式,通过添加Field创建索引文档
        document.addField("id", "1");
        document.addField("name", "领悟书生");
        solr.add(document);
        solr.commit();
    }
}


第二种添加方式,通过添加bean创建索引文档
创建一个实体,实体的属性使用 Field注解
package com._656464.solr.test;
import org.apache.solr.client.solrj.beans.Field;
public class TestObj {
    @Field(value = "id")
    private int id;
    @Field(value = "name")
    private String name;
    @Field(value = "content")
    private String content;
    @Field(value = "price")
    private double price;
    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 String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
}


对bean初始化并赋值后,使用solr的addBean方法把该对象添加到索引中
//第二种添加方式,通过添加bean创建索引文档
TestObj obj=new TestObj();
obj.setId(2);
obj.setName("领悟书生");
obj.setContent("书生在学solr");
obj.setPrice(1024);
solr.addBean(obj);
solr.commit();


第三种添加方式,通过添加bean列表创建索引文档
//第三种添加方式,通过添加bean列表创建多个索引文档
List<TestObj> list=new ArrayList<TestObj>();
TestObj obj=null;
for(int i=1;i<=25;i++){
    obj=new TestObj();
    obj.setId(i);
    obj.setName("领悟书生"+i);
    obj.setContent("书生在学solr"+i);
    obj.setPrice(1024);
    list.add(obj);
}
solr.addBeans(list);
solr.commit();



删除索引
删除比较简单,直接调用SolrServer 的deleteById即可
@Test
public void delete() throws SolrServerException, IOException {
    String urlString = "http://localhost:8983/solr";
    SolrServer solr = new HttpSolrServer(urlString);
    solr.deleteById("1");
    solr.commit();
}



更新索引

更新索引和创建索引一样,只要ID一样,则覆盖



查询文档
使用SolrQuery 构造查询参数,然后通过solr的query方法返回QueryResponse 查询返回对象,然后获取queryResponse的结果集,最后遍历结果集即可
@Test
public void testFind() throws SolrServerException {
    // 以后参数都是通过这个对象去构造...
    SolrQuery solrParams = new SolrQuery();
    solrParams.setQuery("name:书生");
    QueryResponse queryResponse = solr.query(solrParams);
    // 返回结果,默认只返回10条记录
    SolrDocumentList documentList = queryResponse.getResults();
    for (SolrDocument solrDocument : documentList) {
        Object id = solrDocument.get("id");
        Object name = solrDocument.get("name");
        System.out.println(id);
        System.out.println(name);
    }
}



相关问答

更多
  • 在新版本中有一个SuggesterResponse: https://lucene.apache.org/solr/5_3_1/solr-solrj/org/apache/solr/client/solrj/response/SuggesterResponse.html In new versions have a SuggesterResponse: https://lucene.apache.org/solr/5_3_1/solr-solrj/org/apache/solr/client/solrj/r ...
  • 如果您只需要能够打开与Solr服务器的连接以进行索引(并且不需要将配置文件与SolrJ项目实际集成),那么这很简单。 首先,您需要打开SolrJ连接,这样做是这样的: HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8983/solr"); 另一个选择是利用Spring Data Solr的solr模式,并使Solr服务器bean执行以下操作:
  • 由于RealTime Get是使用备用requestHandler实现的,因此您只需要在SolrQuery上使用setRequestHandler()方法,将"/get"作为处理程序名称。 有关完整示例,请参阅Solr源中此SolrExampleTests.java文件中的testRealTimeGet()方法。 这是该文件的片段: SolrQuery q = new SolrQuery(); q.setRequestHandler("/get"); q.set("id", "DOCID"); ...
  • 您可以在SolrQuery类上使用addFilterQuery()方法 。 只需按原样传递过滤器查询(“ x:[10 TO 100] AND y:[20 TO 300] ”) You can use the addFilterQuery() method on the SolrQuery class. Just pass the filter query as is ("x:[10 TO 100] AND y:[20 TO 300]")
  • 我写了一个开源项目,它完全符合你想要做的事情。 https://github.com/freedev/solr-import-export-json I wrote an open source project that does exactly what you want try to do. https://github.com/freedev/solr-import-export-json
  • import org.apache.solr.client.solrj.impl.CloudSolrServer; import org.apache.solr.common.SolrInputDocument; CloudSolrServer server = new CloudSolrServer("localhost:9983"); server.setDefaultCollection("collection1"); SolrInputDocument doc = new SolrInputDoc ...
  • 谢谢Perikli,但它已经整理好了。 代码如下: SolrServer solrServer = new HttpSolrServer("http://localhost:8983/solr"); QueryResponse qResponse = solrServer.query(queryForFecthing); 实际上在创建QueryResponse对象时得到异常,即qResponse ,我认为这是因为我作为参数传递的查询(即上面是' queryForFecthing ')。 但这是错误的假设 ...
  • 您需要在文档索引过程完成后提交。 因此,如果您重新启动solr,它将执行自动提交。 如果你不想重启Solr。 你可以试试 重新加载核心(请参阅下面的URL,根据您更改URL名称) http://127.0.0.1:8080/solr/admin/cores?action=RELOAD&core=core1 要么 使用solr.commit(); 在你的代码中 You need to commit after document indexing process is done. so if you resta ...
  • 如果要使用@Field(child = true)注释将嵌套文档作为bean插入solr,请参阅下面的URL: SOLRJ-6.0.0:插入一个bean对象,它将bean对象的列表关联给出空指针异常 If you want to insert nested document as a bean to solr using @Field(child=true) annotation then see below URL: SOLRJ-6.0.0: Insertion of a bean object whic ...
  • 在服务器上执行查询之前,客户端不会知道你在服务器端设置了什么,对吧? 所以他们全都是空的并不奇怪。 要实现分页,您需要客户端的两个参数 - 页码和每页的项目数量。 一旦你得到了这两个,你可以在客户端构建你的SolrQuery,如下所示: SolrQuery query = new SolrQuery(searchTerm); query.setStart((pageNum - 1) * numItemsPerPage); query.setRows(numItemsPerPage); // execute ...