Java程序员入门技术大全V1(十八) -- Solr样例

    1. Solr样例
      1. 搭建环境

创建Core,名称test

  • cd /opt/soft/solr-8.2.0/bin
  • ./solr start -force
  • ./solr stop -p 8983

 

      1. 调整相关配置文件:

D:\tools\solr-8.2.0\solr-8.2.0\server\solr\test\conf

data-config.xml : 导入数据使用,数据源连接

solrconfig.xml   :

87行添加:

文件最后添加:

        

                  

                            data-config.xml

                  

        

 

managed-schema

验证fieldType  text_ik

 

   

     

     

   

   

     

     

   

 

 

 

 

 

 

 

 

      1. 数据导入

dataimport -- full-import

Java程序员入门技术大全V1(十八) -- Solr样例_第1张图片

 

      1. 页面查询验证:

Java程序员入门技术大全V1(十八) -- Solr样例_第2张图片

 

      1. 代码调用:

   集成solr客户端,solr-solrj:

                  

                       org.apache.solr

                       solr-solrj

                       8.2.0

                  

 

   调用代码:

          public static void main(String[] args) {

                   try {

                            HttpSolrClient.Builder builer = new HttpSolrClient.Builder("http://localhost:8983/solr/test");

                            SolrClient client = builer.build();

                            SolrQuery sq = new SolrQuery("QUESTION_STEM:甲乙两名同学相约周日一起从学校出发去新华书店买书,"

                                               + "假设他们每个人到达学校的时间都是早上9:00至10:00的任意时刻,且约定好最多等待对方15分钟,"

                                               + "他们能够相遇一起出发去新华书店的概率是");

                            sq.setFields("QUESTION_ID","QUESTION_STEM");

                            QueryResponse response = client.query(sq);

                            SolrDocumentList list = response.getResults();SolrUtil.java

                            logger.debug("list:"+list.size());

                            for (SolrDocument solrDocument : list) {

                                     String QUESTION_ID = (String) solrDocument.getFirstValue("QUESTION_ID");

                                     String QUESTION_STEM = (String) solrDocument.getFirstValue("QUESTION_STEM");

                                     logger.debug("QUESTION:"+QUESTION_ID + "-" + QUESTION_STEM);

                            }

                   } catch (SolrServerException e) {

                            e.printStackTrace();

                   } catch (IOException e) {

                            e.printStackTrace();

                   }

         }

你可能感兴趣的:(JAVA程序员入门技术大全V1)