• 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("----------------添加结果-------------
    " + 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------------
    " + JSONObject.toJSONString(results));
        }
    
        private static void delete(String id, SolrClient solrClient) throws IOException, SolrServerException {
            UpdateResponse deleteResponse = solrClient.deleteById(id);
            System.out.println("----------------删除结果-------------
    " + JSONObject.toJSONString(deleteResponse));
            solrClient.commit();
        }
    }

    查询Admin页面:

  • 相关阅读:
    Opencv 图像矩
    Opencv Convex Hull (凸包)
    Opencv 发现轮廓 findContours
    Opencv Match Template(轮廓匹配)
    python操作mysql数据库的常用方法使用详解
    mongodb数据库集群及sharding分片配置
    mongodb数据库安装及常见操作
    windows下搭建eclipse关于python的开发环境及初始化参数配置
    python环境下使用tab自动补全命令
    ubuntu系统初始化网络及mysql配置
  • 原文地址:https://www.cnblogs.com/yangzhenlong/p/8251779.html
Copyright © 2020-2023  润新知