zoukankan      html  css  js  c++  java
  • Solr的了解与配置

    一、Solr:一个web服务器工程

      功能:solr类似于缓存,不过是以文档的形式存储数据库中的数据,没有字段名只说,只有四种域,域(field),动态域(dynamicField),主键域(uniqueKey),

    赋值域(copyField)

    每个域中对应的信息

    · name:指定域的名称

    · type:指定域的类型

    · indexed:是否索引

    · stored:是否存储

    · required:是否必须

    · multiValued:是否多值

    是一个开元搜索平台用于构建搜索应用程序不仅限于搜索,Solr也可以用于存储目的。像其他NoSQL数据库一样,它是一种非关系数据存储和处理技术,会将热点搜索东西放到Solr中,当我们查询数据时,先去redis中查,若没有,则去Solr中,再没有,就去数据库中查找,同时存储在solr,redis中,当redis中没有了,会先去solr中查询,减少对数据库的攻击。

    总之,Solr是一个可扩展的,可部署,搜索/存储引擎,优化搜索大量以文本为中心的数据。本身不支持中文,在使用他的时候要配合中文分词工具后面会提

      安装完成后,访问http://IP:8080/solr/,出现下面即表示成功

     

    二、配置域

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

      四种域对象

    域:  修改solrhomeschema.xml 文件  设置业务系统 Field

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

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

    主键域:对应数据库中的主键

    配置域

    schema.xml文件。

    <fieldType name="text_ik" class="solr.TextField">
        <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
    </fieldType>
    <!--什么域对象中能够使用中文分词-->
    <field name="content_ik" type="text_ik" indexed="true" stored="true" />

     一般流程:sor是在服务器上,我们通过配置solr的web.xml文件,solrhome在solr 下的/example/solr 目录文件中

    <env-entry>
           <env-entry-name>solr/home</env-entry-name>
           <env-entry-value>solr地址</env-entry-value>
           <env-entry-type>java.lang.String</env-entry-type>
        </env-entry>

    使用步骤:

      1、关联solrhome(相当于一个文档),通过域,与Java程序中的实体类发生关系,修改配置域中的各种域

      2、导入依赖

    <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-solr</artifactId>
            <version>1.5.5.RELEASE</version>
          </dependency>  

      3、当pojo属性名与配置域文件中不一致时,在对应pojo对象中使用Field注解

     

       4、配置文件

    <?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">
        <!-- solr服务器地址 -->
        <solr:solr-server id="solrServer" url="http://192.168.200.128:8080/solr" />
        <!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
        <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
            <constructor-arg ref="solrServer" />
        </bean>
    </beans>

      5、测试类

    //先存取数据库中的数据,到本地仓库,

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath*:aoolication.xml")
    public class solrTest {
        @Autowired
        private SolrTemplate solrTemplate;
    
    
        //添加
        @Test
        public void test(){
            List<Item> list = new ArrayList<>();
            for (long i = 0; i <100 ; i++) {
                Item item = new Item();
                item.setId(i);
                item.setGoodsId(1L);
                item.setTitle("你好猛");
                item.setPrice(new BigDecimal(12.9));
                item.setImage("http");
                item.setCategory("123");
                item.setSeller("日天");
                item.setBrand("百度");
                item.setUpdateTime(new Date());
                list.add(item);
            }
            //往solr集合。
            solrTemplate.saveBeans(list);
            //存一个数据
    //        solrTemplate.saveBean();
            //solr需要提交数据
            solrTemplate.commit();
        }
        @Test
        public void getOne(){
            SimpleQuery query = new SimpleQuery();
            //声明一个查询条件
            Criteria criteria = new Criteria("item_title").contains("你");
            //添加查询条件到query。
            query.addCriteria(criteria);
            Item item = solrTemplate.queryForObject(query, Item.class);
            System.out.println(item);
        }
        @Test
        public void getList(){
            SimpleQuery query = new SimpleQuery("*:*");
        //获取分页查询列表 ScoredPage
    <Item> maps = solrTemplate.queryForPage(query, Item.class); //从第几条查询 query.setOffset(1); //每页查询多少条数据 query.setRows(99); //总页数 int totalPages = maps.getTotalPages(); //总记录数 long totalElements = maps.getTotalElements(); //获取集合中的数据列表 List<Item> list = maps.getContent(); System.out.println("总记录数"+totalElements+"总页数"+totalPages); show(list); } private void show(List<Item> list) { for (Item item : list) { System.out.println(item.getTitle()); } } @Test public void delete(){ Query query = new SimpleQuery("*:*"); solrTemplate.delete(query); solrTemplate.commit(); } }
  • 相关阅读:
    CDN混战何去何从,史上最全分析
    s3c2440中U-boot移植时执行cp.b提示:Flash not Erased【转】
    ubuntu18.04下挂载网络文件系统失败【学习笔记】
    win10下搭建深度学习--总结【学习笔记】
    零基础入门深度学习(5)
    为pyhon安装opencv扩展包出现distributed 1.21.8 requires msgpack, which is not installed.【转】
    ubuntu18.04下监视显卡的运行情况【学习笔记】
    Windows10下用Anaconda3安装TensorFlow教程【转】
    ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory 【学习笔记】【原创】
    安装tensorflow报ImportError: libcublas.so.9.0: cannot open shared object file的解决方法【转】
  • 原文地址:https://www.cnblogs.com/guanyuehao0107/p/11910244.html
Copyright © 2011-2022 走看看