Solr cloud Java API 操作

Solr cloud Java API 操作

1、导入maven的pom依赖

    <dependencies>
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>4.10.2</version>
        </dependency>
         <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging-api</artifactId>
            <version>1.1</version>
        </dependency>
    </dependencies>

2、cloudsolr Server操作

package cn.itcast.solr;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
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.junit.Before;
import org.junit.Test;

public class SolrCloudTest {
    private CloudSolrServer solrServer;

    @Before
    public void createSolrServer() {
        solrServer = new CloudSolrServer("zk01:2181,zk02:2181,zk03:2181");
        solrServer.setDefaultCollection("mycore2");
        solrServer.setZkClientTimeout(3000);
        solrServer.setZkConnectTimeout(3000);
        solrServer.connect();
    }

    // 向solrCloud上创建索引
    @Test
    public void testCreateIndexToSolrCloud() throws SolrServerException, IOException {

        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", "100001");
        document.addField("title", "李四");
        solrServer.add(document);
        solrServer.commit();

    }

    // 搜索索引
    @Test
    public void testSearchIndexFromSolrCloud() throws Exception {

        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        try {
            QueryResponse response = solrServer.query(query);
            SolrDocumentList docs = response.getResults();

            System.out.println("文档个数:" + docs.getNumFound());
            System.out.println("查询时间:" + response.getQTime());

            for (SolrDocument doc : docs) {
                ArrayList title = (ArrayList) doc.getFieldValue("title");
                String id = (String) doc.getFieldValue("id");
                System.out.println("id: " + id);
                System.out.println("title: " + title);
                System.out.println();
            }
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("Unknowned Exception!!!!");
            e.printStackTrace();
        }
    }

    // 删除索引
    @Test
    public void testDeleteIndexFromSolrCloud() throws SolrServerException, IOException {

        // 根据id删除
        UpdateResponse response = solrServer.deleteById("100001");
        // 根据多个id删除
        // cloudSolrServer.deleteById(ids);
        // 自动查询条件删除
        // cloudSolrServer.deleteByQuery("product_keywords:教程");
        // 提交
        solrServer.commit();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值