solr服务搭建与应用

solr服务搭建与应用

1. 下载solr

版本:8.2.0

下载地址:http://lucene.apache.org/solr/downloads.html

2. 启动服务

解压压缩包至指定路径下,命令行定位到bin目录下,

  • 启动:

solr start

默认端口号为8983,可指定端口号:

solr start -p 8900

  • 关闭:

关闭指定端口solr stop -p 8983

关闭所有solr stop -all

  • 重启:

solr restart -p 8983

  • 创建core

solr create -c oneproject(core名称)

网页访问localhost:8983,跳转到solr主页,左下方可选择刚创建的core

3. 查询索引参数

  • q - 查询字符串

必须的,如果查询所有使用*:*。

https://www.itsource.cn/upload/news/2019-09-11/222f8fa0-9447-4cce-92ce-0314328df5db.png

  • fq - (filter query)过虑查询

作用:在q查询符合结果中同时是fq查询符合的,

例如:

https://www.itsource.cn/upload/news/2019-09-11/b68c8f96-c125-4bd5-aecf-b6181bef0d86.png

过滤查询价格从1到20的记录。

也可以在“q”查询条件中使用product_price:[1 TO 20],如下:

https://www.itsource.cn/upload/news/2019-09-11/3cbe12b2-1201-43e0-95f7-2be8404fed42.png

也可以使用“*”表示无限,例如:

20以上:product_price:[20 TO *]

20以下:product_price:[* TO 20]

  • sort - 排序

格式:

示例:

https://www.itsource.cn/upload/news/2019-09-11/50e44934-8b0a-44e4-9de9-0fc2e6da26b5.png按价格降序

 

  • start - 分页显示使用

开始记录下标,从0开始

  • rows - 指定返回结果最多有多少条记录,配合start来实现分页。

https://www.itsource.cn/upload/news/2019-09-11/7708f1d6-9106-4f0b-aa10-8eee543ebf6e.png

显示前10条。

  • fl - 指定返回那些字段内容,用逗号或空格分隔多个。

https://www.itsource.cn/upload/news/2019-09-11/4a53bf9f-1826-4280-bff9-ce7a021357c3.png

显示商品图片、商品名称、商品价格

  • df-指定一个搜索Field

https://www.itsource.cn/upload/news/2019-09-11/d2a9cb2a-2709-4e1a-9c43-9c989f5f948f.png

  • wt - (writer type)指定输出格式

可以有 xml, json, php, phps, 后面 solr 1.3增加的,要用通知我们,因为默认没有打开。

  • hl 是否高亮 ,设置高亮Field,设置格式前缀和后缀。

https://www.itsource.cn/upload/news/2019-09-11/6a4119ca-f8e3-4517-9c45-9f5e9e7badc2.png

4. Java项目中调用solr

  • 加载solr所有依赖库

  • 加载IK分词器

   (1)先下载solr对应版本的ik分词器,下载地址:

    https://search.maven.org/search?q=com.github.magese

   (2)添加jar包

将下载好的jar包放入solr-8.2.0/server/solr-webapp/webapp/WEB-INF/lib目录中

   (3)添加配置文件

将jar包目录下的5个配置文件(IKAnalyzer.cfg.xml、 ext.dic、 stopword.dic、 ik.conf、 dynamicdic.txt)放入solr服务的Jetty或Tomcat的 solr-8.2.0\server\solr-webapp\webapp\WEB-INF\classes目录下;(如果无classes新建一个)

  • 修改solr配置文件

修改solr配置文件(solr-8.2.0\server\solr\my_db\conf目录中打开managed-schema文件),包括定义域field及定义域的类型fieldType,配置IK分词器,配置同义词等。

  • 代码调用

    /*

     * 创建索引

     * param String url solr服务地址

     * param String coreName 已创建的core名称

     * param Listdocuments 创建索引所需的文件列表

     */

    @Test

    public void onCreateIndexFile(String url,String coreName, Listdocuments) throws SQLException {

        //1.创建 HttpSolrClient.Builder 对象,通过它创建客户端通信

        HttpSolrClient.Builder builder = new HttpSolrClient.Builder(url);

        HttpSolrClient solrClient = builder.build();

        //2.通过 client document 加入索引库

        FileDao dao = new FileDao();

        try {

            //参数1 solr core 的名字

            solrClient.add(coreName, documents);

            solrClient.commit(coreName);

            System.out.println("创建索引库完成");

        } catch (SolrServerException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    /*

     * 搜索索引

     * param String url solr服务地址

     * param String coreName 已创建的core名称

     */

    @Test

    public void OnQueryIndexFile(String url,String coreName) {

        //1.创建 HttpSolrClient.Builder 对象,通过它创建客户端通信

        HttpSolrClient.Builder builder = new HttpSolrClient.Builder(url);

        HttpSolrClient solrClient = builder.build();

        //2.创建一个map封装搜索条件

        MapqueryMap = new HashMap();

       queryMap.put("q", "file_title:知识产权 OR file_keywords:知识产权");// 搜索关键字

       queryMap.put("df", "file_title");// 默认搜索域

       queryMap.put("sort", "id asc");// 结果以 id 升序排列,默认以关联度排序

queryMap.put("rows", "20");// 默认只有十条

        //3.使用map创建 MapSolrParams 对象

        SolrParams solrParams = new MapSolrParams(queryMap);

        try {

            //4.使用客户端进行查询

            QueryResponse response = solrClient.query(coreName, solrParams);

            //5.提取结果

            SolrDocumentList documents = response.getResults();

            System.out.println("一共查询到:" + documents.getNumFound() + "条结果");

            //6.循环输出

            documents.forEach(document ->{

                System.out.println("标题:" + document.get("file_title") );

                System.out.println("关键字:" + document.get("file_keywords") );

            });

        } catch (SolrServerException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值