java-solr solrj的使用

新建一个maven项目,引入依赖:

<dependencies>
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>6.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.9</version>
        </dependency>
</dependencies>

新建helloworld类:

import com.alibaba.fastjson.JSONObject;
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.UpdateResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;

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

/**
 * SolrHelloWorld类描述:
 *
 * @author yangzhenlong
 * @since 2018/1/9
 */
public class SolrHelloWorld {
    private static String solr_server_url = "http://localhost:8983/solr";
    private static String core_test = "test";//test核心

    public static void main(String[] args) throws IOException, SolrServerException {
        SolrClient solrClient = new HttpSolrClient(solr_server_url + "/" + core_test);

        //添加
        add(solrClient);
        //查询
        query(solrClient);
        //删除
        delete("10", solrClient);

    }


    private static void add(SolrClient solrClient) throws IOException, SolrServerException {
        List<SolrInputDocument> documentList = new ArrayList<SolrInputDocument>(10);
        for(int i = 10; i<=20 ; i++){
            SolrInputDocument document = new SolrInputDocument();
            document.addField("id", i);
            document.addField("name", "测试" + i);
            document.addField("category", i%2 == 0 ? "分类1" : "分类2");
            documentList.add(document);
        }
        UpdateResponse addResponse = solrClient.add(documentList);
        System.out.println(addResponse.toString());
        solrClient.commit();
        System.out.println("----------------添加结果-------------\n" + JSONObject.toJSONString(addResponse));
    }

    private static void query(SolrClient solrClient) throws IOException, SolrServerException {
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("q", "*:*");//q查询
        //solrQuery.addFilterQuery("id:[0 TO 20]");//过滤查询
        //solrQuery.addSort("id", SolrQuery.ORDER.asc);//排序
        solrQuery.setStart(0);//分页 页码
        solrQuery.setRows(20);//分页 数量
       // solrQuery.setHighlight(true);//高亮
        //solrQuery.addHighlightField("name");// 设置高亮的字段
        //solrQuery.setHighlightSimplePre("<font color='red'>");// 设置高亮的样式 开头
        //solrQuery.setHighlightSimplePost("</font>");// 设置高亮的样式 结束

        System.out.println("----------------查询开始-------------,query params=" + JSONObject.toJSONString(solrQuery.getQuery()));
        QueryResponse queryResponse = solrClient.query(solrQuery);
        SolrDocumentList results = queryResponse.getResults();
        System.out.println("----------条数:" + results.getNumFound());
        System.out.println("--------------result------------\n" + JSONObject.toJSONString(results));
    }

    private static void delete(String id, SolrClient solrClient) throws IOException, SolrServerException {
        UpdateResponse deleteResponse = solrClient.deleteById(id);
        System.out.println("----------------删除结果-------------\n" + JSONObject.toJSONString(deleteResponse));
        solrClient.commit();
    }
}

查询Admin页面:

 

posted @ 2018-01-09 15:47  艺言弈行  阅读(437)  评论(0编辑  收藏  举报