SpringDataSolr运用

邮差的信 提交于 2020-01-18 09:06:58

电商网站中查询商品通常会用到搜索引擎,solr就是其中一种.今天讲一下solr的安装和SpringDataSolr运用,solr是建立在Lucene上的,可以说是它的应用,而SpringDataSolr对Solr进行了封装.
1.下载solr压缩文件,将solr的war(版本4.10.3)放置tomcat的webapp目录下,启动tomcat,会解压solr的jar包.
solr目录如图:
在这里插入图片描述
2.将所需要的扩展包放入到WEB-INF的lib目录(如果没有就新建),扩展包如图:
在这里插入图片描述
3.将压缩文件中的solr文件夹,作为solrhome,目录如下:
在这里插入图片描述
4.修改apache-tomcat-7.0.52\webapps\solr\WEB-INF下的web.xml文件,确定solrhome的位置(有点想maven仓库配置),如图:
在这里插入图片描述
5.启动tomcat,访问http://localhost:8080/solr.如图:
在这里插入图片描述
6.解压中文分词器,目录如下:
在这里插入图片描述
7.将中文分词器jar放置tomcat中solr的WEB-INF/lib目录下,将ext_stopword.dic,IKAnalyzer.cfg.xml,mydict.dic放置WEB-INF/classes目录(没有的话新建该目录)下,如图:
在这里插入图片描述
8.修改solrhome\collection1\conf目录下schema.xml文件,将中文分词器作为一个新的fieldType,如图:
在这里插入图片描述
9.新建我们需要的字段field,字段类型可为text_ik,由于该分词器的jar有点问题,此处不以text_ik测试,如图:
在这里插入图片描述
配置基本完成,接下路新建java项目:
10.java工程目录如下 AppTest可以忽略:
在这里插入图片描述
11.pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>cn.com.study</groupId>
  <artifactId>dandingge</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>dandingge</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <spring.version>5.1.5.RELEASE</spring.version>
  </properties>


  <dependencies>
    <!-- Spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jms</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

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

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

12.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">
	
	<!-- 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>

13.MyItem.java文件:

package testSolr;

import org.apache.solr.client.solrj.beans.Field;
import org.springframework.stereotype.Component;

public class MyItem {
    @Field
    private long id;   //必须有,否则测试时会报错

    @Field("my_id")
    private long myId;
    @Field("my_name")
    private String name;


    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getMyId() {
        return myId;
    }

    public void setMyId(long myId) {
        this.myId = myId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

14.TestSolr.java文件

package testSolr;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.ScoredPage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:applicationContext-solr.xml")
public class TestSolr {

    @Autowired
    private SolrTemplate solrTemplate;

    @Test
    public void testCRUD(){
        MyItem item = new MyItem();
        item.setId(1L);
        item.setName("张三");  //新增

        solrTemplate.saveBean(item);
        solrTemplate.commit();

        //修改
        item.setName("李四");
        solrTemplate.saveBean(item);
        solrTemplate.commit();

        MyItem myItem = solrTemplate.getById(1, MyItem.class);
        System.out.println(myItem.getName());  //李四

        solrTemplate.deleteById("1");  //删除
        solrTemplate.commit();
    }

    @Test
    public void testQueryPage(){
        Query query = new SimpleQuery("*:*");  //查询全部
        query.setOffset(2);  //起始位置
        query.setRows(10); //每页记录数
        ScoredPage<MyItem> items =  solrTemplate.queryForPage(query, MyItem.class);
        System.out.println("总记录数" + items.getTotalElements());
        System.out.println("记录" + items.getContent());
    }

    @Test
    public void testPageQueryMutil(){
        Query query=new SimpleQuery("*:*");
        Criteria criteria=new Criteria("my_name").contains("2");

        query.addCriteria(criteria);
        //query.setOffset(20);//开始索引(默认0)
        //query.setRows(20);//每页记录数(默认10)
        ScoredPage<MyItem> page = solrTemplate.queryForPage(query, MyItem.class);
    }

    @Test
    public void testDeleteAll(){
        Query query = new SimpleQuery("*:*");
        solrTemplate.delete(query);
        solrTemplate.commit();   //提交
    }


}

总结:相应的操作都可以在solr界面查看结果. 关于配置 linux和windows大同小异;其他操作,比喻高亮显示,过滤查询等等,都有对应的api;

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!