zoukankan      html  css  js  c++  java
  • Solr

    首先简单介绍Solr的使用流程。

    Solr+IK Analyzer

    Solr是一个开源搜索平台,用于构建搜索应用程序。我们可以把它理解为一个部署的web项目,但是它缺乏对中文词汇的分析(因为solr基于Lucene,而lucene只支持英文和德文)。因此需要一个专业的中文分词工具包,也就是调用相关jar包,这里用IK Analyzer 是一个开源的,基亍 java 语言开发的轻量级的中文分词工具包。这样在搜索一个词汇时,就能检索出相关联的信息。搜索容器,分词工具有了,但是容器里面是空的,所以你还得往里面装东西(搜索内容)才能找到你想要的。所以一般大型项目用这些比较符合实际。

    1、什么是Solr

    大多数搜索引擎应用都必须具有某种搜索功能,问题是搜索功能往往是巨大的资源消耗并且它们由于沉重的数据库加载而拖垮你的应用的性能。

    这就是为什么转移负载到一个外部的搜索服务器是一个不错的主意,Apache Solr是一个流行的开源搜索服务器,它通过使用类似REST的HTTP API,这就确保你能从几乎任何编程语言来使用solr。

    Solr是一个开源搜索平台,用于构建搜索应用程序。 它建立在Lucene(全文搜索引擎)之上。 Solr是企业级的,快速的和高度可扩展的。 使用Solr构建的应用程序非常复杂,可提供高性能。

    为了在CNET网络的公司网站上添加搜索功能,Yonik Seely于2004年创建了Solr。并在2006年1月,它成为Apache软件基金会下的一个开源项目。并于2016年发布最新版本Solr 6.0,支持并行SQL查询的执行。

    Solr可以和Hadoop一起使用。由于Hadoop处理大量数据,Solr帮助我们从这么大的源中找到所需的信息。不仅限于搜索,Solr也可以用于存储目的。像其他NoSQL数据库一样,它是一种非关系数据存储和处理技术。

    总之,Solr是一个可扩展的,可部署,搜索/存储引擎,优化搜索大量以文本为中心的数据。

    补充:什么是lucene

    Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎(英文与德文两种西方语言)。Lucene的目的是为软件开发人员提供一个简单易用的工具包,以方便的在目标系统中实现全文检索的功能,或者是以此为基础建立起完整的全文检索引擎。Lucene是一套用于全文检索和搜寻的开源程式库,由Apache软件基金会支持和提供。Lucene提供了一个简单却强大的应用程式接口,能够做全文索引和搜寻。在Java开发环境里Lucene是一个成熟的免费开源工具。就其本身而言,Lucene是当前以及最近几年最受欢迎的免费Java信息检索程序库。人们经常提到信息检索程序库,虽然与搜索引擎有关,但不应该将信息检索程序库与搜索引擎相混淆.

    2、 Solr安装

    1:安装 Tomcat,解压缩即可。

    2:解压 solr。

    3:把 solr 下的dist目录solr-4.10.3.war部署到 Tomcatwebapps下(去掉版本号)。

    4:启动 Tomcat解压缩 war 包

    5:把solr下example/lib/ext 目录下的所有的 jar 包,添加到 solr 的工程中(WEB-INFlib目录下)。

    6:创建一个 solrhome 。solr 下的/example/solr 目录就是一个 solrhome。复制此目录到D盘改名为solrhome 

    7:关联 solr 及 solrhome。需要修改 solr 工程的 web.xml 文件。

    <env-entry>
    
    <env-entry-name>solr/home</env-entry-name>
    
    <env-entry-value>d:solrhome</env-entry-value>
    
    <env-entry-type>java.lang.String</env-entry-type>
    
    </env-entry>


    启动 Tomcat

    http://IP:8080/solr/

    3、中文分析器IK Analyzer

    3.1 IK Analyzer简介

    IK Analyzer 是一个开源的,基亍 java 语言开发的轻量级的中文分词工具包。从 2006年 12 月推出 1.0 版开始, IKAnalyzer 已经推出了 4 个大版本。最初,它是以开源项目Luence 为应用主体的,结合词典分词和文法分析算法的中文分词组件。从 3.0 版本开始,IK 发展为面向 Java 的公用分词组件,独立亍 Lucene 项目,同时提供了对 Lucene 的默认优化实现。在 2012 版本中,IK 实现了简单的分词歧义排除算法,标志着 IK 分词器从单纯的词典分词向模拟语义分词衍化。

    3.2 IK Analyzer配置

    步骤:

    1、把IKAnalyzer2012FF_u1.jar 添加到 solr 工程的 lib 目录下

    2、在solr/WEB-INF/下创建classes文件夹把扩展词典、停用词词典、配置文件放到 solr 工程的 WEB-INF/classes 目录下。也就是:ext_stopword.dic,IKAnalyzer.cfg.xml,mydict.dic三个文件。

    3、修改 Solrhome/collection1/conf 的 schema.xml 文件,配置一个 FieldType,使用 IKAnalyzer

    <fieldType name="text_ik" class="solr.TextField">
    
    <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
    
    </fieldType>

    4、配置域

    域相当于数据库的表字段,用户存放数据,因此用户根据业务需要去定义相关的Field(域),一般来说,每一种对应着一种数据,用户对同一种数据进行相同的操作。

    域的常用属性:

    • name:指定域的名称
    • type:指定域的类型
    • indexed:是否索引
    • stored:是否存储
    • required:是否必须
    • multiValued:是否多值

    4.1域

    修改solrhome的schema.xml 文件设置业务系统 Field

    <field name="item_goodsid" type="long" indexed="true" stored="true"/>
    <field name="item_title" type="text_ik" indexed="true" stored="true"/>
    <field name="item_price" type="double" indexed="true" stored="true"/>
    <field name="item_image" type="string" indexed="false" stored="true" />
    <field name="item_category" type="string" indexed="true" stored="true" />
    <field name="item_seller" type="text_ik" indexed="true" stored="true" />
    <field name="item_brand" type="string" indexed="true" stored="true" />

    4.2复制域


    复制域的作用在于将某一个Field中的数据复制到另一个域中

    <field name="item_keywords" type="text_ik" indexed="true"stored="false"multiValued="true"/>
    
    <copyField source="item_title" dest="item_keywords"/>
    
    <copyField source="item_category" dest="item_keywords"/>
    
    <copyField source="item_seller" dest="item_keywords"/>
    
    <copyField source="item_brand" dest="item_keywords"/>

     

    4.3动态域

    当我们需要动态扩充字段时,我们需要使用动态域。对于品优购,规格的值是不确定的,所以我们需要使用动态域来实现。需要实现的效果如下:

    可以看到 item_spec 后面的字段都是动态生成的,具备灵活性

    配置:

    dynamicField name="item_spec_*" type="string" indexed="true" stored="true" />    

     

    5、创建工具类,导入搜索数据 内容

    工具类

    package com.smallshop.solrutil;
    
    import com.alibaba.fastjson.JSON;
    import com.pinyougou.mapper.TbItemMapper;
    import model.TbItem;
    import model.TbItemExample;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.data.solr.core.SolrTemplate;
    import org.springframework.stereotype.Component;
    import java.util.List;
    import java.util.Map;
    
    /**
     * @Auther: lanhaifeng
     * @Date: 2019/7/17 0017 14:32
     * @Description: Solr实现商品数据的查询(已审核商品)
     * @statement:
     */
    @Component
    public class SolrUtil {
    
        @Autowired
        private TbItemMapper itemMapper;
        @Autowired
        private SolrTemplate solrTemplate;
    
    
        /**
         * 导入商品数据
         */
        public void importItemData(){
            TbItemExample example=new TbItemExample();
            TbItemExample.Criteria criteria = example.createCriteria();
            criteria.andStatusEqualTo("1");//已审核
            List<TbItem> itemList = itemMapper.selectByExample(example);
            System.out.println("===商品列表===");
            for(TbItem item:itemList){
                Map specMap= JSON.parseObject(item.getSpec());//将spec字段中的json字符串转换为map
                item.setSpecMap(specMap);//给带注解的字段赋值
                System.out.println(item.getTitle());
            }
            solrTemplate.saveBeans(itemList);
            solrTemplate.commit();
            System.out.println("===结束===");
        }
    
        public static void main(String[] args) {
            ApplicationContext context=new ClassPathXmlApplicationContext("classpath*:spring/applicationContext*.xml");
            SolrUtil solrUtil=  (SolrUtil) context.getBean("solrUtil");
            solrUtil.importItemData();
        }
    
    }
    applicationContext—solr.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:solr="http://www.springframework.org/schema/data/solr"
        xsi:schemaLocation="http://www.springframework.org/schema/data/solr 
              http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--扫包-->
        <context:component-scan base-package="com.smallshop.solrutil"></context:component-scan>
    
        <!-- solr服务器地址 -->
        <solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr" />
    
       
        <!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
        <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
            <constructor-arg ref="solrServer" />
        </bean>
    </beans>

    执行工具类,把数据导入到solr容器。这些数据就是存储在你配置的 solrhome 中。

     
  • 相关阅读:
    微信小游戏5.2.2 在子项目中使用EUI制作排行榜报错 wx.getFileSystemManager not function
    Egret5.2.2 微信小游戏行的示例排行榜
    Python翻译
    Python-docx库的使用
    用百度文字识别实现图片文本识别
    基于airtest的朋友圈自动点赞
    使用豆瓣源安装python包
    Appium 环境配置遇到的坑
    使用Pyppeteer进行gmail模拟登录
    异步爬虫
  • 原文地址:https://www.cnblogs.com/zeussbook/p/11200040.html
Copyright © 2011-2022 走看看